public void TestDTreesMushroom() { Matrix <float> data, response; ReadMushroomData(out data, out response); //Use the first 80% of data as training sample int trainingSampleCount = (int)(data.Rows * 0.8); Matrix <Byte> varType = new Matrix <byte>(data.Cols + 1, 1); varType.SetValue((byte)MlEnum.VarType.Categorical); //the data is categorical Matrix <byte> sampleIdx = new Matrix <byte>(data.Rows, 1); using (Matrix <byte> sampleRows = sampleIdx.GetRows(0, trainingSampleCount, 1)) sampleRows.SetValue(255); float[] priors = new float[] { 1, 0.5f }; GCHandle priorsHandle = GCHandle.Alloc(priors, GCHandleType.Pinned); MCvDTreeParams param = new MCvDTreeParams(); param.maxDepth = 8; param.minSampleCount = 10; param.regressionAccuracy = 0; param.useSurrogates = true; param.maxCategories = 15; param.cvFolds = 10; param.use1seRule = true; param.truncatePrunedTree = true; param.priors = priorsHandle.AddrOfPinnedObject(); using (DTree dtree = new DTree()) { bool success = dtree.Train( data, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, response, null, sampleIdx, varType, null, param); if (!success) { return; } double trainDataCorrectRatio = 0; double testDataCorrectRatio = 0; for (int i = 0; i < data.Rows; i++) { using (Matrix <float> sample = data.GetRow(i)) { double r = dtree.Predict(sample, null, false).value; r = Math.Abs(r - response[i, 0]); if (r < 1.0e-5) { if (i < trainingSampleCount) { trainDataCorrectRatio++; } else { testDataCorrectRatio++; } } } } trainDataCorrectRatio /= trainingSampleCount; testDataCorrectRatio /= (data.Rows - trainingSampleCount); EmguAssert.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio * 100)); EmguAssert.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio * 100)); } priorsHandle.Free(); }
public void TestDTreesMushroom() { Matrix<float> data, response; ReadMushroomData(out data, out response); //Use the first 80% of data as training sample int trainingSampleCount = (int)(data.Rows * 0.8); Matrix<Byte> varType = new Matrix<byte>(data.Cols + 1, 1); varType.SetValue((byte)MlEnum.VAR_TYPE.CATEGORICAL); //the data is categorical Matrix<byte> sampleIdx = new Matrix<byte>(data.Rows, 1); using (Matrix<byte> sampleRows = sampleIdx.GetRows(0, trainingSampleCount, 1)) sampleRows.SetValue(255); float[] priors = new float[] {1, 0.5f}; GCHandle priorsHandle = GCHandle.Alloc(priors, GCHandleType.Pinned); MCvDTreeParams param = new MCvDTreeParams(); param.maxDepth = 8; param.minSampleCount = 10; param.regressionAccuracy = 0; param.useSurrogates = true; param.maxCategories = 15; param.cvFolds = 10; param.use1seRule = true; param.truncatePrunedTree = true; param.priors = priorsHandle.AddrOfPinnedObject(); using (DTree dtree = new DTree()) { bool success = dtree.Train( data, Emgu.CV.ML.MlEnum.DATA_LAYOUT_TYPE.ROW_SAMPLE, response, null, sampleIdx, varType, null, param); if (!success) return; double trainDataCorrectRatio = 0; double testDataCorrectRatio = 0; for (int i = 0; i < data.Rows; i++) { using (Matrix<float> sample = data.GetRow(i)) { double r = dtree.Predict(sample, null, false).value; r = Math.Abs(r - response[i, 0]); if (r < 1.0e-5) { if (i < trainingSampleCount) trainDataCorrectRatio++; else testDataCorrectRatio++; } } } trainDataCorrectRatio /= trainingSampleCount; testDataCorrectRatio /= (data.Rows - trainingSampleCount); Trace.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio*100)); Trace.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio*100)); } priorsHandle.Free(); }
private void button2_Click_1(object sender, EventArgs e) { watch = Stopwatch.StartNew(); makeDataSet(); watch.Stop(); txtFeatureTime.Text = "" + watch.ElapsedMilliseconds; progressBar1.Value = 100; int trainingSampleCount = (int)(trainCount * 0.75); int testSampleCount = (int)(trainCount * 0.25); watch = Stopwatch.StartNew(); if (chkDtree.Checked) { Matrix <Byte> varType = new Matrix <byte>(data.Cols + 1, 1); varType.SetValue((byte)VAR_TYPE.NUMERICAL); Matrix <byte> sampleIdx = new Matrix <byte>(data.Rows, 1); using (Matrix <byte> sampleRows = sampleIdx.GetRows(0, trainingSampleCount, 1)) sampleRows.SetValue(255); IntPtr priors = new IntPtr(); MCvDTreeParams param = new MCvDTreeParams(); param.maxDepth = 8; param.minSampleCount = 10; param.regressionAccuracy = 0; param.useSurrogates = true; param.maxCategories = 15; param.cvFolds = 2; param.use1seRule = true; param.truncatePrunedTree = true; param.priors = priors; bool success = dtree.Train( data, Emgu.CV.ML.MlEnum.DATA_LAYOUT_TYPE.ROW_SAMPLE, response, null, null, varType, null, param); if (!success) { return; } } else { SVMParams param = new SVMParams(); param.SVMType = SVM_TYPE.C_SVC; param.TermCrit = new MCvTermCriteria(1000); param.C = 4; svm.Train(data, response, null, null, param); } watch.Stop(); txtLearnTime.Text = "" + watch.ElapsedMilliseconds; double trainDataCorrectRatio = 0; double testDataCorrectRatio = 0; Matrix <float> output = new Matrix <float>(1, featureCount); for (int i = 0; i < data.Rows; i++) { using (Matrix <float> sample = data.GetRow(i)) { double r; if (chkDtree.Checked) { r = dtree.Predict(sample, null, true).value; } else { r = svm.Predict(sample); } r = Math.Abs(Math.Round(r) - response[i, 0]); if (r < 1.0e-6) { if (i >= 0 && i < 3 * trainCount / 16) { trainDataCorrectRatio++; } else if (i >= 3 * trainCount / 16 && i < trainCount / 4) { testDataCorrectRatio++; } else if (i >= trainCount / 4 && i < 7 * trainCount / 16) { trainDataCorrectRatio++; } else if (i >= 7 * trainCount / 16 && i < trainCount / 2) { testDataCorrectRatio++; } else if (i >= trainCount / 2 && i < 11 * trainCount / 16) { trainDataCorrectRatio++; } else if (i >= 11 * trainCount / 16 && i < 3 * trainCount / 4) { testDataCorrectRatio++; } else if (i >= 3 * trainCount / 4 && i < 15 * trainCount / 16) { trainDataCorrectRatio++; } else if (i >= 15 * trainCount / 16 && i < trainCount) { testDataCorrectRatio++; } } } } trainDataCorrectRatio /= (trainCount * .75); testDataCorrectRatio /= (trainCount * .25); txtTestRate.Text = (Math.Round(testDataCorrectRatio, 4) * 100).ToString(); txtTrainRate.Text = (Math.Round(trainDataCorrectRatio, 4) * 100).ToString(); }