Exemple #1
0
 public static extern bool CvRTreesTrain(
     IntPtr model,
     IntPtr trainData,
     MlEnum.DataLayoutType tFlag,
     IntPtr responses,
     IntPtr varIdx,
     IntPtr sampleIdx,
     IntPtr varType,
     IntPtr missingMask,
     ref MCvRTParams param);
Exemple #2
0
 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);
Exemple #3
0
 /// <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&lt;int&gt; of nx1</param>
 /// <param name="sampleIdx">Can be null if not needed. When specified, identifies samples of interest. It is a Matrix&lt;int&gt; 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 <Byte> varIdx,
     Matrix <Byte> sampleIdx,
     Matrix <Byte> varType,
     Matrix <Byte> 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));
 }
Exemple #4
0
        public void TestERTreesLetterRecognition()
        {
            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.VarType.Numerical);         //the data is numerical
            varType[data.Cols, 0] = (byte)MlEnum.VarType.Categorical; //the response is catagorical

            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.TermCritType.Iter;

            using (ERTrees forest = new ERTrees())
            {
                bool success = forest.Train(
                    data.GetRows(0, trainingSampleCount, 1),
                    Emgu.CV.ML.MlEnum.DataLayoutType.RowSample,
                    response.GetRows(0, trainingSampleCount, 1),
                    null,
                    null,
                    varType,
                    null,
                    param);

                if (!success)
                {
                    return;
                }

#if !NETFX_CORE
                String fileName = Path.Combine(Path.GetTempPath(), "ERTree.xml");
                forest.Save(fileName);
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
#endif

                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);

                EmguAssert.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio * 100));
                EmguAssert.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio * 100));
            }
        }
Exemple #5
0
        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.VarType.Numerical);         //the data is numerical
            varType[data.Cols, 0] = (byte)MlEnum.VarType.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.TermCritType.Iter;

            using (RTrees forest = new RTrees())
            {
                bool success = forest.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 = 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]);
                    }
                }

                EmguAssert.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio * 100));
                EmguAssert.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio * 100));
                EmguAssert.WriteLine(builder.ToString());
            }
        }
Exemple #6
0
        public void TestERTreesLetterRecognition()
        {
            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

            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 (ERTrees forest = new ERTrees())
            {
                bool success = forest.Train(
                    data.GetRows(0, trainingSampleCount, 1),
                    Emgu.CV.ML.MlEnum.DATA_LAYOUT_TYPE.ROW_SAMPLE,
                    response.GetRows(0, trainingSampleCount, 1),
                    null,
                    null,
                    varType,
                    null,
                    param);

                forest.Save("ERTree.xml");

                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);

                Trace.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio * 100));
                Trace.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio * 100));
            }
        }