/// <summary> /// Get the default Decision tree training parameters /// </summary> /// <returns>The default Decision tree training parameters</returns> public static MCvDTreeParams GetDefaultParameter() { IntPtr ptr = MlInvoke.CvDTreeParamsCreate(); MCvDTreeParams p = (MCvDTreeParams)Marshal.PtrToStructure(ptr, typeof(MCvDTreeParams)); MlInvoke.CvDTreeParamsRelease(ptr); return(p); }
/// <summary> /// Train the decision tree using the specific traning data /// </summary> /// <param name="trainData">The training data. A 32-bit floating-point, single-channel matrix, one vector per row</param> /// <param name="tflag">data layout type</param> /// <param name="responses">A floating-point matrix of the corresponding output vectors, one vector per row. </param> /// <param name="varIdx">Can be null if not needed. When specified, identifies variables (features) of interest. It is a Matrix>int< of nx1</param> /// <param name="sampleIdx">Can be null if not needed. When specified, identifies samples of interest. It is a Matrix>int< of nx1</param> /// <param name="varType">The types of input variables</param> /// <param name="missingMask">Can be null if not needed. When specified, it is an 8-bit matrix of the same size as <paramref name="trainData"/>, is used to mark the missed values (non-zero elements of the mask)</param> /// <param name="param">The parameters for training the decision tree</param> /// <returns></returns> public bool Train( Matrix<float> trainData, MlEnum.DATA_LAYOUT_TYPE tflag, Matrix<float> responses, Matrix<Byte> varIdx, Matrix<Byte> sampleIdx, Matrix<Byte> varType, Matrix<Byte> missingMask, MCvDTreeParams param) { return MlInvoke.CvDTreeTrain( _ptr, trainData.Ptr, tflag, responses.Ptr, varIdx == null ? IntPtr.Zero : varIdx.Ptr, sampleIdx == null ? IntPtr.Zero : sampleIdx.Ptr, varType == null ? IntPtr.Zero : varType.Ptr, missingMask == null ? IntPtr.Zero : missingMask.Ptr, param); }
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(); }
public static extern bool CvDTreeTrain( IntPtr model, IntPtr trainData, MlEnum.DATA_LAYOUT_TYPE tflag, IntPtr responses, IntPtr varIdx, IntPtr sampleIdx, IntPtr varType, IntPtr missingMask, MCvDTreeParams param);