/// <summary> /// Phần train cho K-SVMeans /// </summary> private void TrainKSVMeans(bool isBatchMode) { string strTrainFile = null; if (isBatchMode) { strTrainFile = _trainFilePath; } else { strTrainFile = tbxTrainFilePath.Text; } int iNumCluster = (int)nmNumCluster.Value; int iPos = strTrainFile.LastIndexOf('_'); string strMutualPath = strTrainFile.Remove(iPos + 1); string strClusterModelFile = strMutualPath + "_clusterModel.txt"; string[] strClusterResultFiles = new string[iNumCluster]; string[] strSVMModelFiles = new string[iNumCluster]; for (int i = 0; i < iNumCluster; i++) { strClusterResultFiles[i] = strMutualPath + "cluster" + (i + 1).ToString() + ".txt"; strSVMModelFiles[i] = strMutualPath + "model" + (i + 1).ToString() + ".txt"; } // Thực hiện cluster SampleDataBUS samDataBUS = new SampleDataBUS(); samDataBUS.Read(strTrainFile); Clustering clustering = new Clustering(iNumCluster, samDataBUS.Samples, DistanceType.Manhattan); clustering.Run(strClusterModelFile, false); samDataBUS.WriteIntoCluster(strClusterResultFiles, clustering.SampleData.ClusterIndices); // Thực hiện train SVM int iProgressBaseline = 0; for (int i = 0; i < iNumCluster; i++) { Problem prob = Problem.Read(strClusterResultFiles[i]); Parameter param = new Parameter(); iProgressBaseline = i * 100 / iNumCluster; if (cmbModelSelection.SelectedItem.ToString() == "Grid search") { string strLogFile = strMutualPath + "GridCluster" + (i + 1).ToString() + ".txt"; double dblC; double dblGamma; ParameterSelection paramSel = new ParameterSelection(); paramSel.NFOLD = Int32.Parse(tbxNumFold.Text); paramSel.EndEpochEvent += new CrossEpochEventHandler( delegate(object senderNetwork, CrossEpochEventArgs args) { tlsProgressBar.Value = iProgressBaseline + (int)(args.TrainingIteration * 100d / (args.Cycles * iNumCluster)); tlsStatus.Text = "Cluster: " + (i + 1) + " | Current parameter set: " + args.TrainingIteration; Application.DoEvents(); }); paramSel.Grid(prob, param, strLogFile, out dblC, out dblGamma); param.C = dblC; param.Gamma = dblGamma; param.Probability = ckbProbEstimate.Checked; Model model = Training.Train(prob, param); Model.Write(strSVMModelFiles[i], model); } else if (cmbModelSelection.SelectedItem.ToString() == "Use default values") { if (tbxC.Text == "" || tbxGamma.Text == "") { MessageBox.Show("Please fill in parameters!"); return; } param.C = double.Parse(tbxC.Text); param.Gamma = double.Parse(tbxGamma.Text); param.Probability = ckbProbEstimate.Checked; Model model = Training.Train(prob, param); Model.Write(strSVMModelFiles[i], model); } } tlsProgressBar.Value = 0; }
/// <summary> /// Phần test cho K-SVMeans /// </summary> private void TestKSVMeans(bool isBatchMode) { string strModelFile = null; string strTestFile = null; if (isBatchMode) { strModelFile = _modelFilePath; strTestFile = _testFilePath; } else { strModelFile = tbxModelFilePath.Text; strTestFile = tbxTestFilePath.Text; } int iPos = strTestFile.LastIndexOf('_'); string strMutualPath = strTestFile.Remove(iPos + 1); int iNumCluster = (int)nmNumCluster.Value; string strClusterModelFile = strMutualPath + "_clusterModel.txt"; string[] strClusterResultFiles = new string[iNumCluster]; string[] strSVMModelFiles = new string[iNumCluster]; string[] strPredictedFiles = new string[iNumCluster]; string strStatisticFile = strMutualPath + "statistic.txt"; for (int i = 0; i < iNumCluster; i++) { strClusterResultFiles[i] = strMutualPath + "testcluster" + (i + 1).ToString() + ".txt"; strSVMModelFiles[i] = strMutualPath + "model" + (i + 1).ToString() + ".txt"; strPredictedFiles[i] = strMutualPath + "predict" + (i + 1).ToString() + ".txt"; } // Thực hiện cluster SampleDataBUS samDataBUS = new SampleDataBUS(); samDataBUS.Read(strTestFile); Clustering clustering = new Clustering(samDataBUS.Samples, DistanceType.Manhattan); clustering.Run(strClusterModelFile, true); samDataBUS.WriteIntoCluster(strClusterResultFiles, clustering.SampleData.ClusterIndices); // Thực hiện test SVM StreamWriter writer = new StreamWriter(strMutualPath + "performance.txt"); double dblTotalPrecision = 0; for (int i = 0; i < iNumCluster; i++) { Problem prob = Problem.Read(strClusterResultFiles[i]); Model model = Model.Read(strSVMModelFiles[i]); double dblPrecision = Prediction.Predict(prob, strPredictedFiles[i], model, ckbProbEstimate.Checked); writer.WriteLine("Cluster " + (i + 1).ToString() + ": " + dblPrecision); if (clustering.Clusters[i].NumSample > 0) { dblTotalPrecision += dblPrecision * clustering.Clusters[i].NumSample; } } writer.WriteLine("All: " + dblTotalPrecision / samDataBUS.DataLines.Length); writer.Close(); _resultShow += "<font color = \"blue\">Accuracy = " + (dblTotalPrecision / samDataBUS.DataLines.Length).ToString() + "%</font><br>"; StatisticTrend2File(strPredictedFiles, strStatisticFile); }