Exemplo n.º 1
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.º 2
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();
            }
        }