Ejemplo n.º 1
0
        public void TestAdaBoost()
        {
            var models = new List <IAdaBootModel <double, bool> >()
            {
                new RegionalDivision(o => o.ToList()[0] > -0.5),
                new RegionalDivision(o => o.ToList()[0] <= -0.5),
                new RegionalDivision(o => o.ToList()[0] <= 0.5),
                new RegionalDivision(o => o.ToList()[0] > 0.5),
                new RegionalDivision(o => o.ToList()[1] > -0.5),
                new RegionalDivision(o => o.ToList()[1] <= -0.5),
                new RegionalDivision(o => o.ToList()[1] <= 0.5),
                new RegionalDivision(o => o.ToList()[1] > 0.5)
            };

            var tuples = new List <LabeledTuple <double, bool> >()
            {
                new LabeledTuple <double, bool>(true, -1, 0),
                new LabeledTuple <double, bool>(true, 1, 0),
                new LabeledTuple <double, bool>(false, 0, -1),
                new LabeledTuple <double, bool>(false, 0, 1)
            };

            var adaBoost = new AdaBoost <double, bool>(models);

            adaBoost.Train(tuples, tuples.Count);

            foreach (var tuple in tuples)
            {
                var expected = tuple.Target;
                var actual   = adaBoost.Predict(tuple.Properties);
                Assert.AreEqual(expected, actual);
            }
        }
Ejemplo n.º 2
0
        public void AdaBoost_Test()
        {
            Logger.Info("AdaBoost_Test");

            var problem = ProblemFactory.CreateClassificationProblem(ClassificationProblemType.ChessBoard);

            var tSet = problem.TrainingSet;
            var vSet = problem.ValidationSet;

            var classifier = new AdaBoost(tSet, 1000, problem.Dimension);

            classifier.Train();

            var hit = (from e in vSet.Examples let iResult = classifier.Predict(e.X) where e.Label.Id == iResult select e).Count();

            var correctRatio = 1.0 * hit / vSet.Count;

            Logger.Info("CorrectRatio: {0}", correctRatio);

            Assert.IsTrue(correctRatio > 0.900, string.Format("AdaBoost (2-class) Correct Ratio, expected: greater than 0.900, actual: {0}.", correctRatio));
        }
Ejemplo n.º 3
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();
            }
        }