/// <summary> /// Get the default Decision tree training parameters /// </summary> /// <returns>The default Decision tree training parameters</returns> public static MCvRTParams GetDefaultParameter() { IntPtr ptr = MlInvoke.CvRTParamsCreate(); MCvRTParams p = (MCvRTParams)Marshal.PtrToStructure(ptr, typeof(MCvRTParams)); MlInvoke.CvRTParamsRelease(ptr); return(p); }
/// <summary> /// Train the random 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 random tree</param> /// <returns></returns> public bool Train( Matrix<float> trainData, MlEnum.DATA_LAYOUT_TYPE tflag, Matrix<float> responses, Matrix<int> varIdx, Matrix<int> sampleIdx, Matrix<int> varType, Matrix<int> missingMask, MCvRTParams param) { return MlInvoke.CvRTreesTrain( _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 TestRTreesLetterRecognition() { Matrix<float> data, response; ReadLetterRecognitionData(out data, out response); int trainingSampleCount = (int)(data.Rows * 0.8); Matrix<Byte> varType = new Matrix<byte>(data.Cols + 1, 1); varType.SetValue((byte)MlEnum.VAR_TYPE.NUMERICAL); //the data is numerical varType[data.Cols, 0] = (byte) MlEnum.VAR_TYPE.CATEGORICAL; //the response is catagorical Matrix<byte> sampleIdx = new Matrix<byte>(data.Rows, 1); using (Matrix<byte> sampleRows = sampleIdx.GetRows(0, trainingSampleCount, 1)) sampleRows.SetValue(255); MCvRTParams param = new MCvRTParams(); param.maxDepth = 10; param.minSampleCount = 10; param.regressionAccuracy = 0.0f; param.useSurrogates = false; param.maxCategories = 15; param.priors = IntPtr.Zero; param.calcVarImportance = true; param.nactiveVars = 4; param.termCrit = new MCvTermCriteria(100, 0.01f); param.termCrit.type = Emgu.CV.CvEnum.TERMCRIT.CV_TERMCRIT_ITER; using (RTrees forest = new RTrees()) { bool success = forest.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 = forest.Predict(sample, null); r = Math.Abs(r - response[i, 0]); if (r < 1.0e-5) { if (i < trainingSampleCount) trainDataCorrectRatio++; else testDataCorrectRatio++; } } } trainDataCorrectRatio /= trainingSampleCount; testDataCorrectRatio /= (data.Rows - trainingSampleCount); StringBuilder builder = new StringBuilder("Variable Importance: "); using (Matrix<float> varImportance = forest.VarImportance) { for (int i = 0; i < varImportance.Cols; i++) { builder.AppendFormat("{0} ", varImportance[0, i]); } } Trace.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio*100)); Trace.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio*100)); Trace.WriteLine(builder.ToString()); } }
public static extern bool CvRTreesTrain( IntPtr model, IntPtr trainData, MlEnum.DATA_LAYOUT_TYPE tFlag, IntPtr responses, IntPtr varIdx, IntPtr sampleIdx, IntPtr varType, IntPtr missingMask, MCvRTParams param);