示例#1
0
        private static void displayConfusionData(ConfusionData cd)
        {
            Console.WriteLine("Confusion data from learning set : ");
            int maxLengthName = cd.UniqueCat.Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur).Length;

            for (int i = 0; i < cd.data.Count; i++)
            {
                Console.Write(cd.UniqueCat[i]);
                for (int k = 0; k < maxLengthName - cd.UniqueCat[i].Length; k++)
                {
                    Console.Write(" ");
                }
                Console.Write(" | ");

                int wellPredicted = cd.data[i][i];
                int badPredicted  = 0;
                for (int j = 0; j < cd.data[0].Count; j++)
                {
                    Console.Write(cd.data[i][j].ToString("000") + " | ");
                    if (j != i)
                    {
                        badPredicted += cd.data[i][j];
                    }
                }
                if (wellPredicted + badPredicted == 0)
                {
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("(" + (wellPredicted * 100 / (wellPredicted + badPredicted)).ToString("00.0") + "%)");
                }
            }
        }
示例#2
0
        static void Main(String[] args)
        {
            //Initialize a new motor
            RandomForest RFmotor = new RandomForest();

            //Enable or disable suspect-detection. Elements with less than 30% of prediction will be classed as "suspect"
            RFmotor.enableOtherClassCreation(30, "suspect");
            RFmotor.disableOtherClassCreation();
            //Do something when a tree is created
            RFmotor._TreeCreated += RFmotor__TreeCreated;

            do
            {
                Console.Clear();

                //Initialize random data to predict
                generateDataToPredict(10);
                //Initialize random learning set
                generateLearningData(50);

                //Evaluate the learning (optional, usefull when creating a new learning)
                modulo = 100;
                Console.Write("Training : ");
                ConfusionData cd = RFmotor.evaluateCrossValidation(5, learningData, learningClasses, 500, 100);
                Console.WriteLine();
                displayConfusionData(cd);
                Console.WriteLine();

                //Train the motor with learning data
                modulo = 10;
                Console.Write("Training : ");
                RFmotor.train(learningData, learningClasses, 500, 100);
                Console.WriteLine();

                //Predict sample's class
                Console.WriteLine("Predicted data : ");
                for (int i = 0; i < dataToPredict.GetLength(0); i++)
                {
                    double[] row = new double[dataToPredict.GetLength(1)];
                    for (int j = 0; j < dataToPredict.GetLength(1); j++)
                    {
                        row[j] = dataToPredict[i, j];
                    }
                    Prediction predicted = RFmotor.predict(row);
                    displayPrediction(predicted, row);
                }
            } while (Console.ReadKey().Key != ConsoleKey.Escape);
            Console.ReadLine();
        }
示例#3
0
        public ConfusionData evaluateCrossValidation(int itterations, double[,] samples, String[] samplesCategories, int forestSize, int percentParameters = 0)
        {
            int           error      = 0;
            ConfusionData cd         = new ConfusionData();
            var           categories = samplesCategories.Distinct().ToList();

            if (this._createOtherClass)
            {
                categories.Add(this._otherClassName);
            }
            if (!PredictCuts)
            {
                categories.Add(CUT_CLASS_NAME);
            }
            cd.UniqueCat = categories;

            //On crée deux sous tableaux constitués d'échantillons tirés aléatoirement
            double[][] samplesArray = ToJaggedArray <double>(samples);
            for (int itt = 0; itt < itterations; itt++)
            {
                //On crée des index aléatoires pour créer les tableaux
                int[] indexes = getRandomSamplesOutOfTrainingSet(samplesArray.GetLength(0), (int)(samplesArray.Length * 0.6));

                int sizeA = (int)(samplesArray.Length * 0.6);
                int sizeB = (int)(samplesArray.Length * 0.4);
                while (sizeA + sizeB != samplesArray.Length)
                {
                    sizeB++;
                }

                double[][] A = new double[sizeA][];
                double[][] B = new double[sizeB][];

                int      j    = 0;
                int      k    = 0;
                String[] ACat = new string[sizeA];
                String[] BCat = new string[sizeB];
                for (int i = 0; i < samplesArray.Length; i++)
                {
                    if (indexes.Contains(i))
                    {
                        ACat[j] = samplesCategories[i];
                        A[j]    = samplesArray[i];
                        j++;
                    }
                    else
                    {
                        BCat[k] = samplesCategories[i];
                        B[k]    = samplesArray[i];
                        k++;
                    }
                }

                //Les deux tableaux sont crées
                train(To2DArray <double>(A), ACat, forestSize, percentParameters);
                for (int s = 0; s < B.Length; s++)
                {
                    Prediction pred = this.predict(B[s]);
                    if (pred.predictedClass != BCat[s])
                    {
                        error++;
                    }

                    cd.data[cd.UniqueCat.IndexOf(BCat[s])][cd.UniqueCat.IndexOf(pred.predictedClass)]++;

                    if (pred.predictedClass != this.OtherClassName)
                    {
                        var percentage = cd.pourcentages.First(p => p.categorie == pred.predictedClass);
                        percentage.predicted += pred.treePredictedClass;
                        percentage.total     += pred.totalTrees;
                    }
                }

                train(To2DArray <double>(B), BCat, forestSize, percentParameters);
                for (int s = 0; s < A.Length; s++)
                {
                    Prediction pred = this.predict(A[s]);
                    if (pred.predictedClass != ACat[s])
                    {
                        error++;
                    }
                    cd.data[cd.UniqueCat.IndexOf(ACat[s])][cd.UniqueCat.IndexOf(pred.predictedClass)]++;

                    if (pred.predictedClass != this.OtherClassName)
                    {
                        var percentage = cd.pourcentages.First(p => p.categorie == pred.predictedClass);
                        percentage.predicted += pred.treePredictedClass;
                        percentage.total     += pred.totalTrees;
                    }
                }
            }
            // ((double)error / (samples.GetLength(0))) / (double)itterations * 100.0;
            return(cd);
        }