public async void TrainAndFilter() { var trainer1 = new BayesTrain { Class = "Spam" }; foreach (var s in spam) { await trainer1.HandleMessage(new StringMessage { Value = s }); } var trainer2 = new BayesTrain { Class = "Ham" }; foreach (var h in ham) { await trainer2.HandleMessage(new StringMessage { Value = h }); } var bayes1 = new Bayes { Class = "Spam" }; var msg1 = new StringMessage { Value = spamSample }; //var likelySpam1 = Bayes.Prediction(msg1.Tokens, "Spam"); //var likelyHam1 = Bayes.Prediction(msg1.Tokens, "Ham"); //Assert.True(likelySpam1 > 0); //Assert.True(likelyHam1 > 0); //Assert.True(likelySpam1 > likelyHam1); //var msg2 = new StringMessage //{ // Value = hamSample //}; //var likelySpam2 = Bayes.Prediction(msg2.Tokens, "Spam"); //var likelyHam2 = Bayes.Prediction(msg2.Tokens, "Ham"); //Assert.True(likelySpam2 > 0); //Assert.True(likelyHam2 > 0); //Assert.True(likelySpam2 < likelyHam2); }
static void Main(string[] args) { var bayes = new Bayes(); bayes.LoadData(@"D:\Politechnika\Semestr4\ArtificialIntelligencePOLSL\BayesAndKeyPoints\BayesAndKeyPoints\GoForAWalk.txt"); bayes.Learn(); bayes.Predict("Sunny", "Cool", "Weak"); bayes.Predict("Rainy", "Hot", "Strong"); var keyPoints = new KeyPoints(); keyPoints.AddMask(); keyPoints.PotencialPoints(5000); Console.ReadKey(); }
public Parlogike() { corrector = new SymSpell(); BayesClassifiers = new Bayes(ref corrector); externFunctors = new Dictionary <string, Func <Parlogike, string, List <Variable>, char, bool, Pattern, string, string, Result> >(); internFunctors = new Dictionary <string, Func <Parlogike, List <Variable>, int, bool> >(); MarkovGenerators = new Dictionary <string, Markov>(); GlobalVariables = new Dictionary <string, Dictionary <string, Variable> >(); LocalStack = new List <string>(); Residues = new List <string>(); Groups = new Dictionary <string, List <string> >(); knowledge = new List <Pattern>(); GlobalVariables[""] = new Dictionary <string, Variable>(); if (!init) { Functors.populate(); init = true; } }
public void CrossValidation(int howManyValidationFolds) { var dataCount = youtubeChannel.Count; var range = dataCount / howManyValidationFolds; var values = youtubeChannel.Values.ToList(); Console.WriteLine("Full Data: " + dataCount + " inputs. For testing we take 1/" + howManyValidationFolds + " of data. That is: " + range + "\n"); int start = 0; Bayes bayes = new Bayes(); List <string> knnResults = new List <string>(); List <string> bayesResults = new List <string>(); for (int i = 0; i < howManyValidationFolds; i++) { var testData = values.GetRange(start, range).ToDictionary(x => x.channelName); //paimamas range kiekis duomeų - testavimui var trainData = values.GetRange(0, start).Concat(values.GetRange(start + range, dataCount - start - range)).ToDictionary(x => x.channelName); //paimami likusieji duomenys mokymuisi start += range; var fullData = DivideData(youtubeChannel, howManyValidationFolds); var dividedData = DivideData(trainData, howManyValidationFolds); //čia kviečiam algoritmo magijas KNN knn = new KNN(fullData, dividedData, howManyValidationFolds); bayes.Train(dividedData); bayesResults.Add(bayes.Test(fullData, testData)); knnResults.Add(knn.Test(testData)); } Console.WriteLine(new string('-', 40)); foreach (var item in knnResults) { Console.WriteLine(item); } Console.WriteLine(new string('-', 40)); foreach (var item in bayesResults) { Console.WriteLine(item); } Console.WriteLine(new string('-', 40)); }
// 分类运算 private void classifyButton_Click(object sender, EventArgs e) { //前提检验 if (SelectedPCXHelper.GetSelPCXFromLB().Count == 0 || SelectedPCXHelper.GetUnselPCXList().Count == 0) { MessageBox.Show(this, "您还未提取样本特征,或者还未设置测试样本集!", "提示信息", MessageBoxButtons.OK); } else { string correctRate = null; //正确率 string myfilepath = filepathText.Text.ToString(); Features feature; #region 这里做一个文件名为unknown.pcx的判断 string filename = FeatureHelper.GetUnknownName(myfilepath); if (filename.ToLower().Equals("unknown")) { feature = new Features(myfilepath); } else { int classID = Convert.ToInt32(FeatureHelper.GetUpperFoldername(myfilepath)); feature = new Features(myfilepath, classID); } #endregion #region Bayes分类法 if (rbBayes.Checked) { #region 数据初始化 CheckInit(); double correctCount = 0.0; #endregion IList sampleList = FeatureHelper.GetFeaturesList(); //获取原始训练样本 //从降维器获取降维后的新样本 IList newSampleList = MDAHelper.GetMDSampleList(); MVHelper.SetSampleList((ArrayList)newSampleList); Bayes bayes = Bayes.GetInstance(); bayes.TrainSampleList = newSampleList; //向贝叶斯分类器注入降维后的训练样本 //int classID = Convert.ToInt32(FeatureHelper.GetUpperFoldername(myfilepath)); //Features feature = new Features(myfilepath, classID); feature = MDAHelper.MDSample(feature); //测试样本降维 int testClassID = bayes.DecisionFunction(feature); //用贝叶斯决策进行测试样本分类 //结果显示 lblunknownclassify.Text = testClassID.ToString("000"); if (feature.classID == testClassID) { lblerrorinfo.Text = "Bayes分类法分类正确"; lblerrorinfo.ForeColor = Color.Green; } //unknown.pcx处理 else if (feature.classID == -1) { } else { lblerrorinfo.Text = "Bayes分类法分类失败"; lblerrorinfo.ForeColor = Color.Green; } } #endregion #region Kn近邻法 if (rbKn.Checked) { #region 相关数据初始化 CheckInit(); int testResult = -1; double correctCount = 0.0; int kvalue = Constant.kvalue; #endregion #region 效的情况下进行计算 if (KCheck(kvalue)) { KnNear my_knearest = new KnNear(); //int classID = Convert.ToInt32(FeatureHelper.GetUpperFoldername(myfilepath)); //Features currfeature = new Features(myfilepath, classID); testResult = my_knearest.DoK_nearest(feature, FeatureHelper.GetFeaturesList(), kvalue); //testResult为K近邻的分类结果 // 其实testResult的结果直接就是result求的值 string result = testResult.ToString("000"); lblunknownclassify.Text = result; result = ResultConvert(result); int testID = Convert.ToInt32(result); if (testID > 0 && testID == feature.classID) { //correctRate = "分类正确率: " + Constant.kn_Rate; lblerrorinfo.Text = "Kn近邻法分类正确"; lblerrorinfo.ForeColor = Color.Green; } //unknown.pcx处理 else if (feature.classID == -1) { } else { lblerrorinfo.Text = "Kn近邻法分类失败!"; lblerrorinfo.ForeColor = Color.Green; } } #endregion } #endregion #region 最近邻法 if (rbnearest.Checked) { #region 初始化 CheckInit(); int testResult = -1; double correctCount = 0.0; #endregion #region 最近邻分类 if (NearestCheck()) { Nearest nearest = new Nearest(); //int classID = Convert.ToInt32(FeatureHelper.GetUpperFoldername(myfilepath)); //Features currfeature = new Features(myfilepath, classID); testResult = nearest.Do_Nearest(feature, FeatureHelper.GetFeaturesList()); string result = testResult.ToString("000"); lblunknownclassify.Text = result; if (testResult > 0 && testResult == feature.classID) { lblerrorinfo.Text = "最近邻法分类正确"; lblerrorinfo.ForeColor = Color.Green; } //unknown.pcx处理 else if (feature.classID == -1) { } else { lblerrorinfo.Text = "最近邻法分类失败!"; lblerrorinfo.ForeColor = Color.Green; } } #endregion } #endregion } }
private void testButton_Click(object sender, EventArgs e) { #region 数据初始化 double correctCount = 0.0; double correctRate = 0.0; IList sampleList = FeatureHelper.GetFeaturesList(); //获取原始训练样本 //从降维器获取降维后的新样本 IList newSampleList = MDAHelper.GetMDSampleList(); MVHelper.SetSampleList((ArrayList)newSampleList); Bayes bayes = Bayes.GetInstance(); bayes.TrainSampleList = newSampleList; //向贝叶斯分类器注入降维后的训练样本 IList testSampleList = FeatureHelper.GetTestFeaturesList(); //获取测试样本 #endregion #region DataGridView操作 bayesDataGridView.DataSource = null; bayesDataGridView.Rows.Clear(); bayesDataGridView.Refresh(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); // 或者直接将arr作为参数传入 //FeatureHelper.GetSamplesFeatures(); //初始化训练和测试样本 dt.Columns.Add("文件夹", typeof(string)); dt.Columns.Add("所属类别", typeof(string)); dt.Columns.Add("测试类别", typeof(string)); dt.Columns.Add("正误判断", typeof(string)); for (int i = 0; i < testSampleList.Count; i++) { DataRow row = dt.NewRow(); string rightOrWrong = "×"; Features feature = (Features)testSampleList[i]; row[0] = feature.Filepath; row[1] = feature.classID; feature = MDAHelper.MDSample(feature); //测试样本降维 int testClassID = bayes.DecisionFunction(feature); //用贝叶斯决策进行测试样本分类 // 用StringBuilder加快字符串处理速度。【值类型和堆类型】 StringBuilder sb = new StringBuilder(); sb.Append("类"); sb.Append(testClassID.ToString()); sb.ToString(); row[2] = sb; if (feature.classID == testClassID) { correctCount++; row[3] = " "; } else { row[3] = rightOrWrong; //this.bayesDataGridView.DefaultCellStyle.ForeColor = Color.Red; } dt.Rows.Add(row); } ds.Tables.Add(dt); this.bayesDataGridView.DataSource = ds.Tables[0]; #endregion #region Bayes分类性能显示 correctRate = (correctCount / Convert.ToDouble(testSampleList.Count)) * 100.0; Constant.bayes_Rate = correctRate.ToString("0.000") + "%"; dataShowLabel.Text = "测试样本总数 " + testSampleList.Count + " ,Bayes判断正确 " + correctCount + " 个,正确率为:" + Constant.bayes_Rate; #endregion }
private void UpdateText() { TextBox_BPM.Text = Bayes.ToString(); }