// 降维处理 private void rdButton_Click(object sender, EventArgs e) { //新得到的样本特征 ArrayList mdSamples = MDAHelper.GetMDSampleList(); MVHelper.SetSampleList(mdSamples); //再用这个新样本,算出每一类的均值和协方差矩阵 ArrayList changeSamples = MVHelper.GetClassFeatureList(mdSamples); ResetRDResultView(mdSamples); }
// 重置相关结果视图 private void ResetResultView() { classChooseComboBox.Items.Clear(); classFeatureList = MVHelper.GetClassFeatureList(FeatureHelper.GetFeaturesList()); //按照类列表中的内容,生成类选择下拉列表的项目 for (int i = 0; i < classFeatureList.Count; i++) { Sample sample = (Sample)classFeatureList[i]; string classIDStr = "000" + sample.ClassID; classChooseComboBox.Items.Add(classIDStr.Substring(classIDStr.Length - 3)); } //初始选择 classChooseComboBox.SelectedIndex = 0; //计算初始选择的结果 ShowResultView(Int32.Parse((string)this.classChooseComboBox.Items[0])); }
//进行最近邻分量 //输入参数testsamples为要进行测试的样本集,trainingSamples为训练样本集 //k_value是设置的K参数 public int Do_Nearest(Features myTestSample, ArrayList trainingSamples) { #region 数据Data int i, j = 0; int index = 0; //index用于最近邻 int result = 0; // class分类结果 Features myTrainingSample; #endregion #region trainingCount 初始化 ArrayList trainlist = MVHelper.GetClassFeatureList(trainingSamples); int trainingCount = 0; if (trainlist.Count == 1) //只选择一类的话 { trainingCount = 1; //((Sample)trainlist[0]).ClassSampleList.Count; } else { trainingCount = trainlist.Count; } #endregion if (trainingCount == 0 || myTestSample == null) { return(-1); } #region 最近邻分类算法 //建立用来记录当前测试样本到每个训练样本的距离以及对应的训练样本类别 //sampleDistance[] myDistance = new sampleDistance[trainingCount]; sampleDistance[] myDimetion = new sampleDistance[Constant.classnumber]; //依次计算当前tmpsample样本与训练样本集中Wi类Ni个样本的欧式距离 for (i = 0; i < trainingCount; i++) { Sample sample = (Sample)trainlist[i]; int n_SampleNum = sample.ClassSampleList.Count; // 这里myDistance的初始化应该与n_SampleNum具体值进行动态改变 sampleDistance[] myDistance = new sampleDistance[n_SampleNum]; for (int k = 0; k < n_SampleNum; k++) { myTrainingSample = (Features)sample.ClassSampleList[k]; myDistance[k].classID = myTrainingSample.classID; myDistance[k].distance = MeasureDistance(myTestSample.feature_vector, myTrainingSample.feature_vector); } //当前tmpsample样本与训练样本集中Wi类Ni个样本的欧式距离 Array.Sort(myDistance, CompareByDistance); //myDimetion[i] = myDistance[0]; //不能这么直接赋值 myDimetion[i].classID = myDistance[0].classID; myDimetion[i].distance = myDistance[0].distance; //index += n_SampleNum; } #endregion #region myDimetion排序时机问题选择 //上面已经得到了当前tmpsample样本与训练样本集中c个类之间的距离,下面对其排序(升序) // 考虑到如果myDimetion长度只为1的话,就会报错 if (trainlist.Count == 1) { result = myDimetion[0].classID; return(result); } else { Array.Sort(myDimetion, CompareByDistance); result = myDimetion[0].classID; return(result); } #endregion }