private void ControlInit() { this.classifyButton.Enabled = false; if (FeatureHelper.GetFeaturesList().Count == FeatureHelper.GetTestFeaturesList().Count) { this.rbKn.Enabled = false; this.rbnearest.Enabled = false; } }
// 最近邻是否为开测试测试 public bool NearestCheck() { // 判断测试样本与训练样本是否一样多,或者判断关于开闭测试的radioButton选择状态 if (FeatureHelper.GetFeaturesList().Count == FeatureHelper.GetTestFeaturesList().Count) { MessageBox.Show(this, "最近近邻法首先进行开测试进行样本提取", "提示信息", MessageBoxButtons.OK); return(false); } return(true); }
private void KFunctionForm_Load(object sender, EventArgs e) { #region 数据初始化 IList testSampleList = FeatureHelper.GetTestFeaturesList(); //获取原始测试 KnNear kn = new KnNear(); //每个元素都存储在 KeyValuePair<TKey, TValue> 对象中 IDictionary <int, double> map = new Dictionary <int, double>(); //每个元素都存储在DictionaryEntry IDictionary dictionary = new Hashtable(); //DictionaryEntry进行循环遍历 ArrayList klist = new ArrayList(); ArrayList ratelist = new ArrayList(); #endregion #region 获取K和识别率 for (int k = 1; k < 50; k += 2) { int testResult = 0; double correctCount = 0; double correctRate = 0.0; foreach (Features feature in testSampleList) { testResult = kn.DoK_nearest(feature, FeatureHelper.GetFeaturesList(), k); if (testResult > 0) { //检查结果是否正确 if (testResult == feature.classID) { correctCount++; } } } correctRate = (correctCount / Convert.ToDouble(testSampleList.Count)) * 100.0; map.Add(k, correctRate / 10); klist.Add(k); ratelist.Add(correctRate * 2); } #endregion #region 显示点图 PreviewFunctionImage(klist, ratelist); #endregion }
// 判断K值的合法性 public bool KCheck(int k) { if (FeatureHelper.GetFeaturesList().Count == FeatureHelper.GetTestFeaturesList().Count) { MessageBox.Show(this, "Kn近邻法首先进行开测试进行样本提取", "提示信息", MessageBoxButtons.OK); return(false); } if (k % 2 == 0) { MessageBox.Show(this, "K 值必须是奇数!", "提示信息", MessageBoxButtons.OK); return(false); } if (k < 1 || k > 49) { MessageBox.Show(this, "K 值必须在 1 到 49 之间!", "提示信息", MessageBoxButtons.OK); return(false); } return(true); }
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 }
// 获取Dataset数据 public DataSet GetViewDS(out double ccount, out double crate, out int count) { #region 数据初始化 double correctCount = 0; double correctRate = 0.0; int testResult = 0; Nearest nearest = new Nearest(); IList testSampleList = FeatureHelper.GetTestFeaturesList(); //获取原始测试 #endregion #region DataGridView操作 dgv_result.DataSource = null; dgv_result.Rows.Clear(); dgv_result.Refresh(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add("样本路径", typeof(string)); dt.Columns.Add("样本类", typeof(string)); dt.Columns.Add("样本测试结果类", typeof(string)); dt.Columns.Add("正误判断", typeof(string)); foreach (Features feature in testSampleList) { testResult = nearest.Do_Nearest(feature, FeatureHelper.GetFeaturesList()); if (testResult > 0) { DataRow row = dt.NewRow(); string rightOrWrong = "×"; row[0] = feature.Filepath; row[1] = feature.classID; row[2] = string.Format("类{0}", testResult); //检查结果是否正确 if (testResult == feature.classID) { correctCount++; row[3] = " "; } else { row[3] = rightOrWrong; //this.knDataGridView.DefaultCellStyle.ForeColor = Color.Red; } dt.Rows.Add(row); } } ds.Tables.Add(dt); //this.dgv_result.DataSource = ds.Tables[0]; //SetControlPropertyValue(dgv_result, "DataSource", ds.Tables[0]); #endregion #region Kn近邻法性能显示 //correctRate = (correctCount / Convert.ToDouble(testSampleList.Count)) * 100.0; //Constant.kn_Rate = correctRate.ToString("0.000") + "%"; //lbl_result.Text = "测试样本总数 " + testSampleList.Count + " ,Kn近邻法判断正确 " // + correctCount + " 个,正确率为:" + Constant.kn_Rate; #endregion ccount = correctCount; crate = correctRate; count = testSampleList.Count; return(ds); }
// 测试启动 private void knTestButton_Click(object sender, EventArgs e) { int kvalue = Convert.ToInt32(kValueText.Text); Constant.kvalue = kvalue; #region K有效的情况下进行计算 if (KCheck(kvalue)) { #region 数据初始化 double correctCount = 0; double correctRate = 0.0; int testResult = 0; KnNear my_knearest = new KnNear(); IList testSampleList = FeatureHelper.GetTestFeaturesList(); //获取原始测试 #endregion #region DataGridView操作 knDataGridView.DataSource = null; knDataGridView.Rows.Clear(); knDataGridView.Refresh(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); dt.Columns.Add("样本路径", typeof(string)); dt.Columns.Add("样本测试结果类", typeof(string)); dt.Columns.Add("所取K值", typeof(string)); dt.Columns.Add("正误判断", typeof(string)); foreach (Features feature in testSampleList) { testResult = my_knearest.DoK_nearest(feature, FeatureHelper.GetFeaturesList(), kvalue); if (testResult > 0) { DataRow row = dt.NewRow(); string rightOrWrong = "×"; row[0] = feature.Filepath; row[1] = string.Format("类{0}", testResult); row[2] = kvalue; //检查结果是否正确 if (testResult == feature.classID) { correctCount++; row[3] = " "; } else { row[3] = rightOrWrong; //this.knDataGridView.DefaultCellStyle.ForeColor = Color.Red; } dt.Rows.Add(row); } } ds.Tables.Add(dt); this.knDataGridView.DataSource = ds.Tables[0]; #endregion #region Kn近邻法性能显示 correctRate = (correctCount / Convert.ToDouble(testSampleList.Count)) * 100.0; Constant.kn_Rate = correctRate.ToString("0.000") + "%"; resultLabel.Text = "测试样本总数 " + testSampleList.Count + " ,Kn近邻法判断正确 " + correctCount + " 个,正确率为:" + Constant.kn_Rate; #endregion } #endregion }