Exemplo n.º 1
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            double penalty;

            if (Double.TryParse(tbPenalty.Text, out penalty))
            {
                Penalty = penalty;
                if (Tree != null)
                {
                    Tree = new RegressionTree(TestDataSet, "Single Tree", Penalty);
                    RegressionTreeVisualizator.ClearChart(RegressionChart);
                    fillRegressionChart();
                }
                if (Forest != null)
                {
                    Forest = new RandomForest(dataSet, AmountOfTreesInForest, Penalty);
                    RegressionTreeVisualizator.ClearChart(RandomForestChart);
                    fillRandomForestChart();
                }
            }
            else
            {
                MessageBox.Show("Input double value in /Penalty on leaves amount/ field!", "Error");
            }
        }
Exemplo n.º 2
0
        public void Train(List <Person> people, int numberOfTrees, int skillSetSize)
        {
            double[][] inputs = _dataPointService.GenerateDataPointsFromPeople(people, skillSetSize);

            int[] expectedResults = _dataPointService.GenerateExpectedResultFromPeople(people);

            // Create the forest learning algorithm
            var teacher = new RandomForestLearning()
            {
                NumberOfTrees = numberOfTrees, // use 10 trees in the forest
            };

            // Finally, learn a random forest from data
            _randomForest = teacher.Learn(inputs, expectedResults);

            // We can estimate class labels using
            trainingPredictions = _randomForest.Decide(inputs);

            // And the classification error (0.0006) can be computed as
            double error = new ZeroOneLoss(expectedResults).Loss(_randomForest.Decide(inputs));

            File.WriteAllLines(
                @"C:\Users\Niall\Documents\Visual Studio 2015\Projects\LinkedInSearchUi\LinkedIn Dataset\XML\random_forest_predictions.txt" // <<== Put the file name here
                , trainingPredictions.Select(d => d.ToString()).ToArray());
        }
Exemplo n.º 3
0
        public ResultExperimentClassification ExperimentClassification(object a)
        {
            InfaForExperimentClassification infa = (InfaForExperimentClassification)a;

            ResultExperimentClassification result = new ResultExperimentClassification();

            result.timeTrain          = new Stopwatch();
            result.timeTest           = new Stopwatch();
            result.timeСlassification = new Stopwatch();

            RandomForest RF = new RandomForest();

            RF.setNumIterations(100); //Количество деревьев

            result.timeTrain.Start();

            RF.buildClassifier(infa.trainDataSet); //Обучние

            result.timeTrain.Stop();
            result.timeTest.Start();

            result.evaluation = evaluateModel(RF, infa.trainDataSet, infa.testDataSet); //Тестирование

            result.timeTest.Stop();
            result.timeСlassification.Start();

            RF.classifyInstance(infa.testDataSet.instance(0)); //Замер времени классификации 1 экземпляра

            result.timeСlassification.Stop();

            return(result);
        }
Exemplo n.º 4
0
        public static double resultPrepareWithCrossFold(string classifierFileName, int baseClasses, string clasifier)
        {
            double performance = 0.0;

            if (clasifier == "svm")
            {
                weka.classifiers.Classifier svm = new SMO();

                performance = Weka.classifyCrossFold_Train_Test(classifierFileName, baseClasses, svm);
            }
            else if (clasifier == "nb")
            {
                weka.classifiers.Classifier nb = new NaiveBayes();
                performance = Weka.classifyCrossFold_Train_Test(classifierFileName, baseClasses, nb);
            }
            else if (clasifier == "rf")
            {
                weka.classifiers.Classifier rf = new RandomForest();
                performance = Weka.classifyCrossFold_Train_Test(classifierFileName, baseClasses, rf);
            }
            else if (clasifier == "j48")
            {
                weka.classifiers.Classifier j48 = new J48();
                performance = Weka.classifyCrossFold_Train_Test(classifierFileName, baseClasses, j48);
            }
            return(performance);
        }
Exemplo n.º 5
0
 static void BestClassificator(RandomForest forest, DecisionTree tree, KNearestNeighbors knn, double[] precision)
 {
     if (precision[0] > precision[1])
     {
         Console.WriteLine("Geriausias parinktas klasifikavimo metodas: {0}", "Random Forest");
         Console.Write("Iveskite penkiu kortu rinkini, skaicius atskirdamo tarpu: ");
         string[] val  = Console.ReadLine().Split(' ');
         double[] hand = new double[10];
         for (int i = 0; i < 10; i++)
         {
             hand[i] = Convert.ToDouble(val[i]);
         }
         Console.WriteLine(forest.Decide(hand));
     }
     else
     {
         Console.WriteLine("Geriausias parinktas klasifikavimo metodas: %s", "Decision Tree");
         Console.Write("Iveskite penkiu kortu rinkini, skaicius atskirdamo tarpu: ");
         string[] val  = Console.ReadLine().Split(' ');
         double[] hand = new double[10];
         for (int i = 0; i < 10; i++)
         {
             hand[i] = Convert.ToDouble(val[i]);
         }
         Console.WriteLine(tree.Decide(hand));
     }
 }
Exemplo n.º 6
0
        public ResultExperimentClassification TestClassification(object a)
        {
            InfaForExperimentClassification infa = (InfaForExperimentClassification)a;

            ResultExperimentClassification result = new ResultExperimentClassification();

            result.timeTrain          = new Stopwatch();
            result.timeTest           = new Stopwatch();
            result.timeСlassification = new Stopwatch();

            RandomForest RF = (RandomForest)SerializationHelper.read(infa.fileNameModel);  //Загрузка модели

            //RandomForest RF = new RandomForest();

            result.timeTest.Start();

            result.evaluation = evaluateModel(RF, infa.trainDataSet, infa.testDataSet); //Тестирование

            result.timeTest.Stop();
            result.timeСlassification.Start();

            RF.classifyInstance(infa.testDataSet.instance(0)); //Замер времени классификации 1 экземпляра

            result.timeСlassification.Stop();

            return(result);
        }
        private void ClassifierList_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right && SelectedItem != null)
            {
                Classifier classifier = SelectedItem as Classifier;
                if (classifier == null)
                {
                    throw new NullReferenceException("Failed to cast classifier item from list");
                }

                string updateWindowTitle = "Updating " + classifier.GetType().Name + "...";

                if (classifier is LibLinear)
                {
                    LibLinear   liblinear = classifier as LibLinear;
                    DynamicForm f         = new DynamicForm("Set LibLinear parameters", DynamicForm.CloseButtons.OkCancel);
                    f.AddCheckBox("Run feature selection:", ContentAlignment.MiddleRight, liblinear.RunFeatureSelection, "run_feature_selection");
                    f.AddDropDown("Positive weighting:", Enum.GetValues(typeof(LibLinear.PositiveClassWeighting)), liblinear.Weighting, "positive_weighting", true);
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        liblinear.RunFeatureSelection = f.GetValue <bool>("run_feature_selection");
                        liblinear.Weighting           = f.GetValue <LibLinear.PositiveClassWeighting>("positive_weighting");
                    }
                }
                else if (classifier is SvmRank)
                {
                    SvmRank     svmRank = classifier as SvmRank;
                    DynamicForm f       = new DynamicForm("Set SvmRank parameters", DynamicForm.CloseButtons.OkCancel);
                    f.AddNumericUpdown("c:", (decimal)svmRank.C, 3, decimal.MinValue, decimal.MaxValue, (decimal)0.01, "c");
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        try { svmRank.C = Convert.ToSingle(f.GetValue <decimal>("c")); }
                        catch (Exception ex) { MessageBox.Show("Invalid value for C:  " + ex.Message); }
                    }
                }
                else if (classifier is RandomForest)
                {
                    RandomForest randomForest = classifier as RandomForest;
                    DynamicForm  f            = new DynamicForm("Set RandomForest parameters", DynamicForm.CloseButtons.OkCancel);
                    f.AddNumericUpdown("Number of trees:", randomForest.NumTrees, 0, 1, decimal.MaxValue, 1, "ntree");
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        randomForest.NumTrees = Convert.ToInt32(f.GetValue <decimal>("ntree"));
                    }
                }
                else if (classifier is AdaBoost)
                {
                    AdaBoost    adaBoost = classifier as AdaBoost;
                    DynamicForm f        = new DynamicForm("Set AdaBoost parameters", DynamicForm.CloseButtons.OkCancel);
                    f.AddNumericUpdown("Number of iterations:", adaBoost.Iterations, 0, 1, decimal.MaxValue, 1, "iterations");
                    if (f.ShowDialog() == DialogResult.OK)
                    {
                        adaBoost.Iterations = Convert.ToInt32(f.GetValue <decimal>("iterations"));
                    }
                }
            }
        }
Exemplo n.º 8
0
        private static void Predict()
        {
            var lgrPred = -1;
            var lgrProb = -1.0;

            if (MultinomialLogisticRegression != null)
            {
                lgrPred = MultinomialLogisticRegression.Decide(CurrPredictionPoints);
                lgrProb = MultinomialLogisticRegression.Probability(CurrPredictionPoints);
                PredictedFrequencyClassifiers = lgrPred;
                if (Classification.IsValidation)
                {
                    Console.WriteLine($"Real Frequency: {BrainStorm0.ClassificationShape.Hertz} Logistic Regression Predicted Frequency: {lgrPred} Probability: {lgrProb}");
                }
            }

            var mmdPred  = -1;
            var mmdScore = -1.0;

            if (MinimumMeanDistance != null)
            {
                mmdPred  = MinimumMeanDistance.Decide(CurrPredictionPoints);
                mmdScore = MinimumMeanDistance.Score(CurrPredictionPoints);
                if (Classification.IsValidation)
                {
                    Console.WriteLine(
                        $"Real Frequency: {BrainStorm0.ClassificationShape.Hertz} Minimun Distance Predicted Frequency: {PredictedFrequencyClassifiers}");
                }
                else
                {
                    Console.WriteLine(
                        $"MMD Predicted Frequency: {mmdPred} {mmdScore}");
                }
            }

            var rfPred = -1;

            if (RandomForest != null)
            {
                rfPred = RandomForest.Decide(CurrPredictionPoints);
                if (Classification.IsValidation)
                {
                    Console.WriteLine(
                        $"Real Frequency: {BrainStorm0.ClassificationShape.Hertz} Random Forest Predicted Frequency: {rfPred}");
                }
                else
                {
                    Console.WriteLine(
                        $"Random Forest Predicted Frequency: {rfPred}");
                }
            }
            if (IsTyping)
            {
                UpdateTypingPredictions();
            }
        }
Exemplo n.º 9
0
        public RandomForest TrainClassification(object a)
        {
            InfaForExperimentClassification infa = (InfaForExperimentClassification)a;

            RandomForest RF = new RandomForest();

            RF.setNumIterations(100);                          //Количество деревьев
            RF.buildClassifier(infa.trainDataSet);             //Обучние

            SerializationHelper.write(infa.fileNameModel, RF); //Сохранение модели

            return(RF);
        }
Exemplo n.º 10
0
        public void buildModel()
        {
            var attributes = DecisionVariable.FromData(inputs);
            // Now, let's create the forest learning algorithm
            var teacher = new RandomForestLearning(attributes)
            {
                NumberOfTrees = 1,
                SampleRatio   = 1.0
            };

            // Finally, learn a random forest from data
            this.forest = teacher.Learn(inputs, outputs);
        }
Exemplo n.º 11
0
        public void test_building_predictions_lines()
        {
            Runtime      rt  = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv"));
            RandomForest cls = rt.Classifiers.Trees.RandomForest.
                               NumExecutionSlots(4).
                               NumFeatures(5).
                               NumTrees(50);

            Runtime testset = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_test.csv"), preprocessor: TestLinePreproc);
            int     count   = testset.NumInstances;

            System.Collections.Generic.List <string> lines = testset.GeneratePredictions(GeneratePredictionLine, cls);
            Assert.AreEqual(count, lines.Count);
        }
        public RandomForestClassifier(FeatureValueTypes featureDefaultsValueTypes, FeatureGranularities featureGranularities, string serializedClassifierPath = null)
        {
            var featureKinds = new List <DecisionVariable>();

            foreach (var entry in featureDefaultsValueTypes)
            {
                var featureName = entry.Key.ToString();
                var featureType = entry.Value;

                var featureGranularity = featureGranularities[entry.Key];

                if (featureGranularity == FeatureGranularity.Continuous)
                {
                    featureKinds.Add(new DecisionVariable(featureName, DecisionVariableKind.Continuous));
                }
                else if (featureGranularity == FeatureGranularity.Discrete)
                {
                    var decisionVar = new DecisionVariable(featureName, DecisionVariableKind.Discrete);

                    // TODO: Fix uint, there is no Accord.UIntRange
                    if (featureType == (typeof(int)) || featureType == (typeof(int?)) || featureType == (typeof(uint)) || featureType == (typeof(uint?)))
                    {
                        decisionVar.Range = new Accord.IntRange(min: int.MinValue, max: int.MaxValue);
                    }
                    else if (featureType == (typeof(byte)) || featureType == (typeof(byte?)))
                    {
                        decisionVar.Range = new Accord.IntRange(min: byte.MinValue, max: byte.MaxValue);
                    }

                    featureKinds.Add(decisionVar);
                }
                else
                {
                    throw new ArgumentException("Unknown feature granularity");
                }
            }

            var featureKindsArr = featureKinds.ToArray <DecisionVariable>();

            this.RandomForestLearner = new RandomForestLearning(featureKindsArr)
            {
                NumberOfTrees = 10
            };

            if (serializedClassifierPath != null)
            {
                this.RandomForest = Serializer.Load <RandomForest>(serializedClassifierPath);
            }
        }
Exemplo n.º 13
0
        static void Dirbam()
        {
            DownloadTrainingAndTestingData();
            List <int[]> trainingData = ReadData("poker-hand-training-true.data");
            List <int[]> testingData  = ReadData("poker-hand-testing.data");

            double[]          precision    = new double[3];
            RandomForest      ranForest    = RandomForestClassification(trainingData, testingData, out precision[0]);
            DecisionTree      decisionTree = DecisionTreeClassification(trainingData, testingData, out precision[1]);
            KNearestNeighbors knn          = kNearestNeighbours(trainingData, testingData, out precision[2]);

            BestClassificator(ranForest, decisionTree, knn, precision);

            Console.ReadKey();
        }
 public ICU()
 {
     InitializeComponent();
     current_frame_num1 = 0;
     current_frame_num2 = 0;
     F_E = new FeaturesExtraction();
     knn = Serializer.Load <KNearestNeighbors>(Path.Combine(path, "knn7.bin"));
     RF  = Serializer.Load <RandomForest>(Path.Combine(path, "RF7.bin"));
     LR  = Serializer.Load <LogisticRegression>(Path.Combine(path, "LR7.bin"));
     SVM = Serializer.Load <SupportVectorMachine <Gaussian> >(Path.Combine(path, "SVM7.bin"));
     NB  = Serializer.Load <NaiveBayes>(Path.Combine(path, "NB7.bin"));
     HMM = Serializer.Load <HiddenMarkovModel>(Path.Combine(path, "HMM_seq7.bin"));
     dataGridView1.RowTemplate.Height = 120;
     ((DataGridViewImageColumn)dataGridView1.Columns[0]).ImageLayout = DataGridViewImageCellLayout.Stretch;
     dataGridView1.Columns[1].Visible = false;
 }
Exemplo n.º 15
0
        private void PreprocessToWorkTreeRandomForest(TrainData trainedData, TrainData checkData,
                                                      Dictionary <int, string> analysParameter)
        {
            int numberOfForest = Convert.ToInt32(numberOfForests.Value);

            RandForests = new List <DM.DecisionTree.TreeNode>();
            //double accuracy = 0.0;
            //do
            //{
            //Random rand = new Random((int) (DateTime.Now.Ticks));
            //DivideSet(rand, trainData, trainedData, checkData);
            List <DM.DecisionTree.TreeNode> rndForests = new List <DM.DecisionTree.TreeNode>();
            int parameterID = XMLWork.FindIDWithName(parameterCondition.SelectedItem.ToString(), Properties.Settings.Default.Languages);
            int restrictCountOfParameters          = Convert.ToInt32(Math.Sqrt(analysParameter.Count)) + 1;
            Dictionary <int, string> newParameters = new Dictionary <int, string>();


            for (int forestsCount = 0; forestsCount < numberOfForest;)
            {
                DM.DecisionTree.RandomForest random = new RandomForest();
                List <int> parametersName           = new List <int>();
                foreach (KeyValuePair <int, string> keyValuePair in analysParameter)
                {
                    if (!keyValuePair.Value.StartsWith("Def"))
                    {
                        parametersName.Add(keyValuePair.Key);
                    }
                }
                for (int i = 0; i < restrictCountOfParameters; i++)
                {
                    int j = rand.Next(0, analysParameter.Count);
                    if (!newParameters.ContainsKey(parametersName[j]))
                    {
                        newParameters.Add(parametersName[j], analysParameter[parametersName[j]]);
                    }
                }
                List <OneRow> trainData = new List <OneRow>();
                trainData = GetRandomData(TrainData.Train, newParameters, parameterID);
                _root     = random.MountTree(trainData, newParameters, parameterID, Convert.ToInt32(textEdit1.Value) + 1);
                if (_root.attributeName != "False")
                {
                    RandForests.Add(_root);
                    forestsCount++;
                }
                newParameters.Clear();
            }
        }
Exemplo n.º 16
0
 /// <summary>
 /// Invoke weka jar to create an arff from a csv file
 /// </summary>
 protected override RandomForestFromWekaOutput Perform(RandomForestFromWekaInput input)
 {
     s_logger.Debug($"RandomForestFromWeka starts");
     try
     {
         var          outputTreeFile = $"{input.TrainingSetCsv}.wtree";
         var          outputArffFile = $"{input.TrainingSetCsv}.arff";
         RandomForest forest         = null;
         if (!File.Exists(outputTreeFile))
         {
             // lets analyze this in here
             try
             {
                 var arffExitCode = WriteArff(input, outputArffFile);
                 s_logger.Debug($"ArffWrite returns exitCode={arffExitCode}");
                 if (File.Exists(outputArffFile))
                 {
                     var exitCode = RunWeka(input, outputArffFile, outputTreeFile);
                     s_logger.Debug($"RandomForestFromWeka returns exitCode={exitCode}");
                 }
                 else
                 {
                     s_logger.Error($"Could not write arff file for [{input.TrainingSetCsv}]");
                 }
             }
             catch (Exception e)
             {
                 s_logger.Error(e, "An exception ocurred when writting forest from weka");
             }
             finally
             {
                 if (!File.Exists(outputTreeFile))
                 {
                     throw new Exception($"RandomForestFromWeka task failed to write output to [{outputTreeFile}]");
                 }
             }
         }
         // and now load the forest
         forest = RandomForest.FromWekaFile(outputTreeFile);
         // done
         return(new RandomForestFromWekaOutput(forest, input.TrainingSetCsv, outputTreeFile));
     }
     finally
     {
         s_logger.Debug($"RandomForestFromWeka ends in {Stopwatch.ElapsedMilliseconds}ms");
     }
 }
Exemplo n.º 17
0
        public void Uczenie(string[] naglowki, string[][] dane)
        {
            Codification kody = new Codification(naglowki, dane);

            int[][] symbole       = kody.Transform(dane);
            int[][] daneWejsciowe = symbole.Get(null, 0, -1);

            KolumnaWynikow = symbole.GetColumn(-1);

            RandomForestLearning nauczyciel = new RandomForestLearning()
            {
                SampleRatio = IloscDanychModelu
            };

            RandomForest las = nauczyciel.Learn(daneWejsciowe, KolumnaWynikow);

            Rezultaty = las.Decide(daneWejsciowe);
        }
Exemplo n.º 18
0
        public void TestTrain()
        {
            var randomForest          = new RandomForest();
            var randomForestParameter = new RandomForestParameter(1, 100, 35);

            randomForest.Train(iris.GetInstanceList(), randomForestParameter);
            Assert.AreEqual(2.00, 100 * randomForest.Test(iris.GetInstanceList()).GetErrorRate(), 0.01);
            randomForest.Train(bupa.GetInstanceList(), randomForestParameter);
            Assert.AreEqual(42.03, 100 * randomForest.Test(bupa.GetInstanceList()).GetErrorRate(), 0.01);
            randomForest.Train(dermatology.GetInstanceList(), randomForestParameter);
            Assert.AreEqual(2.46, 100 * randomForest.Test(dermatology.GetInstanceList()).GetErrorRate(), 0.01);
            randomForest.Train(car.GetInstanceList(), randomForestParameter);
            Assert.AreEqual(0.0, 100 * randomForest.Test(car.GetInstanceList()).GetErrorRate(), 0.01);
            randomForest.Train(tictactoe.GetInstanceList(), randomForestParameter);
            Assert.AreEqual(0.0, 100 * randomForest.Test(tictactoe.GetInstanceList()).GetErrorRate(), 0.01);
            randomForest.Train(nursery.GetInstanceList(), randomForestParameter);
            Assert.AreEqual(0.0, 100 * randomForest.Test(nursery.GetInstanceList()).GetErrorRate(), 0.01);
        }
Exemplo n.º 19
0
        static RandomForest RandomForestClassification(List <int[]> trainingData, List <int[]> testingData, out double precision)
        {
            int    testingCount      = testingData.Count / 10;
            int    trainingCount     = testingData.Count - testingCount;
            double errorAverage      = 0;
            int    indexTestingStart = testingData.Count - testingCount;
            int    indexTestingEnd   = testingData.Count;
            double prec = 0;

            Console.WriteLine("Random Forest Classification");
            RandomForest bestforest = null;

            for (int i = 0; i < iterations; i++)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();
                Console.WriteLine("Testing from: {0} to {1}", indexTestingStart, indexTestingEnd);
                int[][] inputData, testinputData;
                int[]   outputData, testoutputData;

                PrepareInputOutput(out inputData, out outputData, out testinputData, out testoutputData, trainingData, testingData, indexTestingStart, indexTestingEnd);
                var RanForest = new RandomForestLearning()
                {
                    NumberOfTrees = 100,
                };
                var forest = RanForest.Learn(inputData, outputData);
                Console.WriteLine("Medis sukurtas - ismokta");
                double er = new ZeroOneLoss(testoutputData).Loss(forest.Decide(testinputData));
                Console.WriteLine("Apmokymo tikslumas: {0}", 1 - er);
                if (1 - er > prec)
                {
                    prec       = 1 - er;
                    bestforest = forest;
                }
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Iteracija baigta per: {0}ms", elapsedMs);
                indexTestingEnd    = indexTestingStart;
                indexTestingStart -= testingCount;
                errorAverage      += er;
                Console.WriteLine("------------------------------------------------------------------------------");
            }
            precision = 1 - (errorAverage / iterations);
            return(bestforest);
        }
Exemplo n.º 20
0
        private static void randomForest(double[][] inputs, int[] outputs)
        {
            var teacher = new RandomForestLearning()
            {
                NumberOfTrees = 100, // Use 100 decision trees to cover this problem
            };

            // Use the learning algorithm to induce the tree
            RandomForest rf = teacher.Learn(inputs, outputs);

            // Classify the samples using the RF
            int[] predicted = rf.Decide(inputs);

            // Create a confusion matrix to check the quality of the predictions:
            var cm = new ConfusionMatrix(predicted: predicted, expected: outputs);

            // Check the accuracy measure:
            double accuracy = cm.Accuracy; // (should be 1.0 or 100%)
        }
Exemplo n.º 21
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            int amountOfT;

            if (Int32.TryParse(tbAmountOfTrees.Text, out amountOfT))
            {
                AmountOfTreesInForest = Math.Abs(amountOfT);
                Forest = new RandomForest(dataSet, AmountOfTreesInForest, Penalty);
                addForestPermission();
                fillRandomForestChart();
                addItemsInDrawComboBox();
                MessageBox.Show("Forest was build.");
                FillDataGridLearn();
            }
            else
            {
                MessageBox.Show("Input integer value in /Amount of trees/ field!", "Error");
            }
        }
        /// <summary>
        /// Trains the classifier and computes the training error if option provided.
        /// </summary>
        /// <param name="trainingData">The training data that will be used to train classifier.</param>
        /// <param name="trainingLabels">The training labels related to provided training data.</param>
        /// <param name="calculateError">The boolean check to tell if the training error should be calculated.</param>
        public override void Train(List <double[]> trainingData, List <int> trainingLabels, bool calculateError = true)
        {
            LearningAlgorithm = new RandomForestLearning();
            if (NumTrees > 0)
            {
                LearningAlgorithm.NumberOfTrees = NumTrees;
            }

            if (SamplePropotion > 0)
            {
                LearningAlgorithm.SampleRatio = SamplePropotion;
            }

            Model = LearningAlgorithm.Learn(trainingData.ToArray(), trainingLabels.ToArray());
            if (calculateError == true)
            {
                TrainingError = new ZeroOneLoss(trainingLabels.ToArray()).Loss(Model.Decide(trainingData.ToArray()));
            }
        }
Exemplo n.º 23
0
        private async void button7_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "model файлы (*.model)|*.model|Все файлы (*.*)|*.*";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                InfaForExperimentClassification infa = new InfaForExperimentClassification(); //Инфа для второго потока

                trainDataSet.setClassIndex(trainDataSet.numAttributes() - 1);                 //Установка указателя на атрибут с классами

                infa.trainDataSet  = trainDataSet;
                infa.fileNameModel = saveFileDialog1.FileName;

                labelStatus.Text = "Проводится обучение...";

                RandomForest RF = await Task.Factory.StartNew(() => TrainClassification(infa));

                labelStatus.Text = "Обучение прошло успешно.";
            }
        }
Exemplo n.º 24
0
        public void test_making_single_predictions_from_trained_model()
        {
            Runtime      rt         = Runtime.LoadFromFile <TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv"));
            RandomForest classifier = rt.Classifiers.Trees.RandomForest.
                                      NumExecutionSlots(4).
                                      NumFeatures(5).
                                      NumTrees(50);

            TitanicDataRow row = new TitanicDataRow
            {
                age      = 10,
                pclass   = "1",
                sex      = "male",
                embarked = "C"
            };
            double prediction = classifier.ClassifyRow(row);
            double proba      = classifier.ClassifyRowProba(row);

            Assert.AreEqual(0, prediction);
            Assert.IsTrue(proba < 0.5);
        }
Exemplo n.º 25
0
        public override void Train(List <double[]> trainingData, List <double> trainingLabels, bool calculateError = true)
        {
            LearningAlgorithm = new RandomForestLearning();
            if (NumTrees > 0)
            {
                LearningAlgorithm.NumberOfTrees = NumTrees;
            }

            if (SamplePropotion > 0)
            {
                LearningAlgorithm.SampleRatio = SamplePropotion;
            }
            int[][] TrainingData   = TypeCasters.DoubleMultiArrayToInt(trainingData).ToArray();
            int[]   TrainingLabels = TypeCasters.DoubleArrayToInt(trainingLabels).ToArray();

            Model = LearningAlgorithm.Learn(TrainingData, TrainingLabels);
            if (calculateError == true)
            {
                TrainingError = new ZeroOneLoss(TrainingLabels).Loss(Model.Decide(TrainingData));
            }
        }
Exemplo n.º 26
0
        private static void EvaluateForests(Args arguments)
        {
            Contract.Requires(arguments.InputDirectory != null, "You must specify an input directory");
            Contract.Requires(Directory.Exists(arguments.InputDirectory), "The input directory must exist");
            // we have a bunch of forests and we will compare them by classifying against
            // unknown samples. The forests are in weka format (wtree)
            s_logger.Info("Evaluating random forests...");
            // create the tree from the sample csvs...
            var treeEvaluationBlock = new ActionBlock <string>(i =>
            {
                var classifier = RandomForest.FromWekaFile(i);
                s_logger.Info($"Evaluating forest from [{i}] against universe");
                // and now evaluate
                foreach (var evaluationFile in Directory.EnumerateFiles(arguments.InputDirectory, "*.csv"))
                {
                    if (!evaluationFile.Contains("-sample"))
                    {
                        classifier.EvaluateOnTrainingSet(evaluationFile, true, false, 0);
                    }
                }
            },
                                                               new ExecutionDataflowBlockOptions()
            {
                MaxDegreeOfParallelism = 1
            }
                                                               );

            // post each action
            foreach (var treeFile in Directory.EnumerateFiles(arguments.InputDirectory, "*.wtree"))
            {
                treeEvaluationBlock.Post(treeFile);
            }
            // complete
            treeEvaluationBlock.Complete();
            // wait
            treeEvaluationBlock.Completion.Wait();
            // done...
        }
 public void Train(double[][] trainingInputs, int[] trainingOutputs)
 {
     this.RandomForest = this.RandomForestLearner.Learn(trainingInputs, trainingOutputs);
 }
Exemplo n.º 28
0
        private void bwLoadData_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Classifier    classif = null;
                Datas.Useable train   = null;
                Datas.Useable test    = null;

                ConfusionMatrix conf_train, conf_test;

                if (e.Argument is ToBackgroundWorkerArgsTree)
                {
                    var args = e.Argument as ToBackgroundWorkerArgsTree;
                    train   = args._Train;
                    test    = args._Test;
                    classif = new DecisionTree(args._MaxDepth);
                    classif.Train(train);
                    conf_train = new ConfusionMatrix(classif.Compile, train);
                    conf_test  = new ConfusionMatrix(classif.Compile, test);
                }
                else if (e.Argument is ToBackgroundWorkerArgsForest)
                {
                    var args = e.Argument as ToBackgroundWorkerArgsForest;
                    train   = args._Train;
                    test    = args._Test;
                    classif = new RandomForest(args._MaxDepth, args._TreeCount);
                    classif.Train(train);
                    conf_train = new ConfusionMatrix(classif.Compile, train);
                    conf_test  = new ConfusionMatrix(classif.Compile, test);
                }
                else if (e.Argument is ToBackgroundWorkerArgsAdaBoost)
                {
                    var args = e.Argument as ToBackgroundWorkerArgsAdaBoost;
                    train   = args._Train;
                    test    = args._Test;
                    classif = new AdaBoost(args._Factory, args._Boosts);
                    classif.Train(train);
                    conf_train = new ConfusionMatrix(classif.Compile, train);
                    conf_test  = new ConfusionMatrix(classif.Compile, test);
                }
                else if (e.Argument is ToBackgroundWorkerkNN)
                {
                    var args = e.Argument as ToBackgroundWorkerkNN;
                    train   = args._Train;
                    test    = args._Test;
                    classif = new kNN(args._kNN);
                    classif.Train(train);
                    conf_train = null;
                    conf_test  = new ConfusionMatrix(classif.Compile, test);
                }
                else
                {
                    throw new Exception("Not recognized stuff");
                }

                if (this.bwLoadData.CancellationPending)
                {
                    e.Result = null;
                }
                else
                {
                    e.Result = new TrainerReturn(
                        conf_train,
                        conf_test,
                        classif);
                }
            }
            catch (Exception exc)
            {
                e.Result = exc.ToString();
            }
        }
 /// <summary>
 /// Loads the trained model.
 /// </summary>
 /// <param name="path">The location from where to load the trained model.</param>
 public override void Load(string path)
 {
     Model = Accord.IO.Serializer.Load <RandomForest>(path);
 }
Exemplo n.º 30
0
 /// <summary>
 /// Constructor
 /// </summary>
 public RandomForestFromWekaOutput(RandomForest p, string ifn, string pfn)
 {
     Predictor = p;
     PredictorInputFileName = ifn;
     PredictorFileName      = pfn;
 }