define bin model
This commit is contained in:
parent
f87e506417
commit
aec1984ab8
|
|
@ -35,7 +35,12 @@ namespace BotSharp.Algorithm.Bayes
|
|||
|
||||
public List<Tuple<string, double[]>> FeatureSet { get; set; }
|
||||
|
||||
public double Alpha { get; set; }
|
||||
private double alpha { get; set; }
|
||||
|
||||
public MultinomiaNaiveBayes(double alpha = 0.5)
|
||||
{
|
||||
this.alpha = alpha;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// prior probability
|
||||
|
|
@ -48,7 +53,29 @@ namespace BotSharp.Algorithm.Bayes
|
|||
int k = LabelDist.Count;
|
||||
int Nyk = LabelDist.First(x => x.Value == Y).Freq;
|
||||
|
||||
return (Nyk + Alpha) / (N + k * Alpha);
|
||||
return (Nyk + alpha) / (N + k * alpha);
|
||||
}
|
||||
|
||||
public double CalCondProb(int x, string Y, double feature)
|
||||
{
|
||||
// posterior probability P(X1,...,Xn|Y) = Sum(P(X1|Y) +...+ P(Xn|Y)
|
||||
var featuresIfY = FeatureSet.Where(fd => fd.Item1 == Y).ToList();
|
||||
var matrix = ConstructMatrix(featuresIfY);
|
||||
|
||||
int freq = 0;
|
||||
for (int y = 0; y < featuresIfY.Count; y++)
|
||||
{
|
||||
if (matrix[y, x] == feature)
|
||||
{
|
||||
freq++;
|
||||
}
|
||||
}
|
||||
|
||||
int Nyk = featuresIfY.Count;
|
||||
int n = featuresIfY.Count;
|
||||
int Nykx = freq;
|
||||
|
||||
return Math.Log((Nykx + alpha) / (Nyk + n * alpha));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -57,35 +84,17 @@ namespace BotSharp.Algorithm.Bayes
|
|||
/// P(X1,...,Xn|Y) = Sum(P(X1|Y) +...+ P(Xn|Y)
|
||||
/// P(X, Y) = P(Y|X)P(X) = P(X|Y)P(Y) => P(Y|X) = P(Y)P(X|Y)/P(X)
|
||||
/// </summary>
|
||||
public double PosteriorProb(string Y, double[] features, double priorProb)
|
||||
public double CalPosteriorProb(string Y, double[] features, double priorProb, Dictionary<string, double> condProbDictionary)
|
||||
{
|
||||
Alpha = 0.5;
|
||||
|
||||
int featureCount = features.Length;
|
||||
|
||||
double postProb = priorProb;
|
||||
|
||||
// posterior probability P(X1,...,Xn|Y) = Sum(P(X1|Y) +...+ P(Xn|Y)
|
||||
var featuresIfY = FeatureSet.Where(fd => fd.Item1 == Y).ToList();
|
||||
var matrix = ConstructMatrix(featuresIfY);
|
||||
|
||||
// loop features
|
||||
for (int x = 0; x < featureCount; x++)
|
||||
{
|
||||
int freq = 0;
|
||||
for (int y = 0; y < featuresIfY.Count; y++)
|
||||
{
|
||||
if(matrix[y, x] == features[x])
|
||||
{
|
||||
freq++;
|
||||
}
|
||||
}
|
||||
|
||||
int Nyk = featuresIfY.Count;
|
||||
int n = featureCount;
|
||||
int Nykx = freq;
|
||||
|
||||
postProb += Math.Log((Nykx + Alpha) / (Nyk + n * Alpha));
|
||||
string key = $"{Y} f{x} {features[x]}";
|
||||
postProb += condProbDictionary[key];
|
||||
}
|
||||
|
||||
return Math.Pow(2, postProb);
|
||||
|
|
|
|||
14
BotSharp.Algorithm/Bayes/MultinomiaNaiveBayesModel.cs
Normal file
14
BotSharp.Algorithm/Bayes/MultinomiaNaiveBayesModel.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using BotSharp.Algorithm.Statistics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.Algorithm.Bayes
|
||||
{
|
||||
public class MultinomiaNaiveBayesModel
|
||||
{
|
||||
public List<Probability> LabelDist { get; set; }
|
||||
|
||||
public Dictionary<string, double> CondProbDictionary { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -129,7 +129,7 @@ namespace BotSharp.Core.Engines.BotSharp
|
|||
ClassifyOptions classifyOptions = new ClassifyOptions();
|
||||
classifyOptions.ModelFilePath = Path.Combine(Settings.ModelDir, "svm_classifier_model");
|
||||
classifyOptions.TransformFilePath = Path.Combine(Settings.ModelDir, "transform_obj_data");
|
||||
svmClassifier.Train(featureSetList, classifyOptions);
|
||||
// svmClassifier.Train(featureSetList, classifyOptions);
|
||||
|
||||
meta.Meta = new JObject();
|
||||
meta.Meta["compiled at"] = "Aug 31, 2018";
|
||||
|
|
|
|||
|
|
@ -32,9 +32,9 @@ namespace BotSharp.NLP.UnitTest
|
|||
{
|
||||
newSentences[i].Label = sentences[i].Label;
|
||||
}
|
||||
sentences = newSentences.Take(10).ToList();
|
||||
sentences = newSentences.ToList();
|
||||
|
||||
//sentences.Shuffle();
|
||||
// sentences.Shuffle();
|
||||
|
||||
var encoder = new OneHotEncoder();
|
||||
encoder.Sentences = sentences;
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ namespace BotSharp.NLP.Classify
|
|||
|
||||
var sents = sentences.Select(x => new Tuple<string, double[]>(x.Label, x.Vector)).ToList();
|
||||
|
||||
_classifier.Train(sents, _options);
|
||||
_classifier.Train(sents, new double[] { 0, 1 }, _options);
|
||||
}
|
||||
|
||||
public List<Tuple<string, double>> Classify(Sentence sentence)
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace BotSharp.NLP.Classify
|
|||
/// </summary>
|
||||
/// <param name="featureSets"></param>
|
||||
/// <param name="options"></param>
|
||||
void Train(List<Tuple<string, double[]>> featureSets, ClassifyOptions options);
|
||||
void Train(List<Tuple<string, double[]>> featureSets, double[] values, ClassifyOptions options);
|
||||
|
||||
/// <summary>
|
||||
/// Predict by feature vector
|
||||
|
|
|
|||
|
|
@ -44,12 +44,9 @@ namespace BotSharp.NLP.Classify
|
|||
|
||||
private MultinomiaNaiveBayes nb = new MultinomiaNaiveBayes();
|
||||
|
||||
/// <summary>
|
||||
/// Cache all categories' prior probability
|
||||
/// </summary>
|
||||
private Dictionary<string, double> PriorPropDictionary = new Dictionary<string, double>();
|
||||
private Dictionary<string, double> condProbDictionary = new Dictionary<string, double>();
|
||||
|
||||
public void Train(List<Tuple<string, double[]>> featureSets, ClassifyOptions options)
|
||||
public void Train(List<Tuple<string, double[]>> featureSets, double[] values, ClassifyOptions options)
|
||||
{
|
||||
labelDist = featureSets.GroupBy(x => x.Item1)
|
||||
.Select(x => new Probability
|
||||
|
|
@ -66,7 +63,27 @@ namespace BotSharp.NLP.Classify
|
|||
labelDist.ForEach(l => l.Prob = nb.CalPriorProb(l.Value));
|
||||
|
||||
// calculate posterior prob
|
||||
// loop features
|
||||
var featureCount = nb.FeatureSet[0].Item2.Length;
|
||||
|
||||
labelDist.ForEach(label =>
|
||||
{
|
||||
for (int x = 0; x < featureCount; x++)
|
||||
{
|
||||
for (int v = 0; v < values.Length; v++)
|
||||
{
|
||||
string key = $"{label.Value} f{x} {values[v]}";
|
||||
condProbDictionary[key] = nb.CalCondProb(x, label.Value, values[v]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// save the model
|
||||
var model = new MultinomiaNaiveBayesModel
|
||||
{
|
||||
LabelDist = labelDist,
|
||||
CondProbDictionary = condProbDictionary
|
||||
};
|
||||
}
|
||||
|
||||
public List<Tuple<string, double>> Classify(double[] features, ClassifyOptions options)
|
||||
|
|
@ -76,7 +93,7 @@ namespace BotSharp.NLP.Classify
|
|||
// calculate prop
|
||||
labelDist.ForEach(lf =>
|
||||
{
|
||||
var prob = nb.PosteriorProb(lf.Value, features, lf.Prob);
|
||||
var prob = nb.CalPosteriorProb(lf.Value, features, lf.Prob, condProbDictionary);
|
||||
results.Add(new Tuple<string, double>(lf.Value, prob));
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -32,11 +32,6 @@ namespace BotSharp.NLP.Classify
|
|||
/// </summary>
|
||||
public class SVMClassifier : IClassifier
|
||||
{
|
||||
public List<Tuple<string, double>> Classify(List<Feature> features, ClassifyOptions options)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public double[][] Predict(FeaturesWithLabel featureSet, ClassifyOptions options)
|
||||
{
|
||||
Problem predict = new Problem();
|
||||
|
|
@ -53,9 +48,14 @@ namespace BotSharp.NLP.Classify
|
|||
return Prediction.PredictLabelsProbability(options.Model, scaled);
|
||||
}
|
||||
|
||||
public void Train(List<FeaturesWithLabel> featureSets, ClassifyOptions options)
|
||||
public void Train(List<Tuple<string, double[]>> featureSets, double[] values, ClassifyOptions options)
|
||||
{
|
||||
SVMClassifierTrain(featureSets, options);
|
||||
// SVMClassifierTrain(featureSets, options);
|
||||
}
|
||||
|
||||
public List<Tuple<string, double>> Classify(double[] features, ClassifyOptions options)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SVMClassifierTrain(List<FeaturesWithLabel> featureSets, ClassifyOptions options, SvmType svm = SvmType.C_SVC, KernelType kernel = KernelType.RBF, bool probability = true, string outputFile = null)
|
||||
|
|
@ -154,17 +154,5 @@ namespace BotSharp.NLP.Classify
|
|||
|
||||
return labeledFeatureSet;
|
||||
}
|
||||
|
||||
public void Train(List<Tuple<string, double[]>> featureSets, ClassifyOptions options)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<Tuple<string, double>> Classify(double[] features, ClassifyOptions options)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue