public void RegressionStackingEnsembleModel_Predict_Multiple()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var learner = new RegressionStackingEnsembleLearner(learners,
                                                                new RegressionDecisionTreeLearner(9),
                                                                new RandomCrossValidation <double>(5, 23), false);

            var sut = learner.Learn(observations, targets);

            var predictions = sut.Predict(observations);

            var metric = new MeanSquaredErrorRegressionMetric();
            var actual = metric.Error(targets, predictions);

            Assert.AreEqual(0.26175213675213671, actual, 0.0000001);
        }
        public void RegressionStackingEnsembleLearner_Learn_Indexed()
        {
            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var sut = new RegressionStackingEnsembleLearner(learners, new RegressionDecisionTreeLearner(9),
                                                            new RandomCrossValidation <double>(5, 23), false);

            var(observations, targets) = DataSetUtilities.LoadDecisionTreeDataSet();

            var indices = Enumerable.Range(0, 25).ToArray();

            var model       = sut.Learn(observations, targets, indices);
            var predictions = model.Predict(observations);

            var evaluator = new MeanSquaredErrorRegressionMetric();
            var actual    = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.133930222950635, actual, 0.0001);
        }
        public void RegressionStackingEnsembleModel_GetRawVariableImportance()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var learner = new RegressionStackingEnsembleLearner(learners,
                                                                new RegressionDecisionTreeLearner(9),
                                                                new RandomCrossValidation <double>(5, 23), false);

            var sut = learner.Learn(observations, targets);

            var actual   = sut.GetRawVariableImportance();
            var expected = new double[] { 0.255311355311355, 0.525592463092463, 0.753846153846154, 0.0128205128205128 };

            Assert.AreEqual(expected.Length, actual.Length);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i], 0.000001);
            }
        }
        public void RegressionStackingEnsembleLearner_CreateMetaFeatures_Then_Learn()
        {
            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var sut = new RegressionStackingEnsembleLearner(learners, new RegressionDecisionTreeLearner(9),
                                                            new RandomCrossValidation <double>(5, 23), false);

            var(observations, targets) = DataSetUtilities.LoadDecisionTreeDataSet();

            var metaObservations = sut.LearnMetaFeatures(observations, targets);
            var model            = sut.LearnStackingModel(observations, metaObservations, targets);

            var predictions = model.Predict(observations);

            var evaluator = new MeanSquaredErrorRegressionMetric();
            var actual    = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.06951934687172627, actual, 0.0001);
        }
Пример #5
0
        public void RegressionStackingEnsembleModel_Predict_Multiple()
        {
            var parser       = new CsvParser(() => new StringReader(Resources.AptitudeData));
            var observations = parser.EnumerateRows(v => v != "Pass").ToF64Matrix();
            var targets      = parser.EnumerateRows("Pass").ToF64Vector();
            var rows         = targets.Length;

            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var learner = new RegressionStackingEnsembleLearner(learners, new RegressionDecisionTreeLearner(9),
                                                                new RandomCrossValidation <double>(5, 23), false);

            var sut = learner.Learn(observations, targets);

            var predictions = sut.Predict(observations);

            var metric = new MeanSquaredErrorRegressionMetric();
            var actual = metric.Error(targets, predictions);

            Assert.AreEqual(0.26175213675213671, actual, 0.0000001);
        }
Пример #6
0
        public void RegressionStackingEnsembleModel_GetRawVariableImportance()
        {
            var parser       = new CsvParser(() => new StringReader(Resources.AptitudeData));
            var observations = parser.EnumerateRows(v => v != "Pass").ToF64Matrix();
            var targets      = parser.EnumerateRows("Pass").ToF64Vector();

            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var learner = new RegressionStackingEnsembleLearner(learners, new RegressionDecisionTreeLearner(9),
                                                                new RandomCrossValidation <double>(5, 23), false);

            var sut = learner.Learn(observations, targets);

            var actual   = sut.GetRawVariableImportance();
            var expected = new double[] { 0.255311355311355, 0.525592463092463, 0.753846153846154, 0.0128205128205128 };

            Assert.AreEqual(expected.Length, actual.Length);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i], 0.000001);
            }
        }
Пример #7
0
        public void RegressionStackingEnsembleLearner_Learn_Keep_Original_Features()
        {
            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var sut = new RegressionStackingEnsembleLearner(learners, new RegressionDecisionTreeLearner(9),
                                                            new RandomCrossValidation <double>(5, 23), true);

            var parser       = new CsvParser(() => new StringReader(Resources.DecisionTreeData));
            var observations = parser.EnumerateRows("F1", "F2").ToF64Matrix();
            var targets      = parser.EnumerateRows("T").ToF64Vector();

            var model       = sut.Learn(observations, targets);
            var predictions = model.Predict(observations);

            var evaluator = new MeanSquaredErrorRegressionMetric();
            var actual    = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.066184865331534531, actual, 0.0001);
        }
Пример #8
0
        public void RegressionStackingEnsembleLearner_CreateMetaFeatures_Then_Learn()
        {
            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var sut = new RegressionStackingEnsembleLearner(learners, new RegressionDecisionTreeLearner(9),
                                                            new RandomCrossValidation <double>(5, 23), false);

            var parser       = new CsvParser(() => new StringReader(Resources.DecisionTreeData));
            var observations = parser.EnumerateRows("F1", "F2").ToF64Matrix();
            var targets      = parser.EnumerateRows("T").ToF64Vector();

            var metaObservations = sut.LearnMetaFeatures(observations, targets);
            var model            = sut.LearnStackingModel(observations, metaObservations, targets);

            var predictions = model.Predict(observations);

            var evaluator = new MeanSquaredErrorRegressionMetric();
            var actual    = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.06951934687172627, actual, 0.0001);
        }
Пример #9
0
        public void RegressionStackingEnsembleLearner_Learn_Indexed()
        {
            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var sut = new RegressionStackingEnsembleLearner(learners, new RegressionDecisionTreeLearner(9),
                                                            new RandomCrossValidation <double>(5, 23), false);

            var parser       = new CsvParser(() => new StringReader(Resources.DecisionTreeData));
            var observations = parser.EnumerateRows("F1", "F2").ToF64Matrix();
            var targets      = parser.EnumerateRows("T").ToF64Vector();
            var indices      = Enumerable.Range(0, 25).ToArray();

            var model       = sut.Learn(observations, targets, indices);
            var predictions = model.Predict(observations);

            var evaluator = new MeanSquaredErrorRegressionMetric();
            var actual    = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.133930222950635, actual, 0.0001);
        }
        public void RegressionStackingEnsembleLearner_Learn_Keep_Original_Features()
        {
            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var sut = new RegressionStackingEnsembleLearner(learners, new RegressionDecisionTreeLearner(9),
                                                            new RandomCrossValidation <double>(5, 23), true);

            var(observations, targets) = DataSetUtilities.LoadDecisionTreeDataSet();

            var model       = sut.Learn(observations, targets);
            var predictions = model.Predict(observations);

            var evaluator = new MeanSquaredErrorRegressionMetric();
            var actual    = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.066184865331534531, actual, 0.0001);
        }
        public void RegressionStackingEnsembleModel_GetVariableImportance()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

            var featureNameToIndex = new Dictionary <string, int> {
                { "AptitudeTestScore", 0 },
                { "PreviousExperience_month", 1 }
            };

            var learners = new IIndexedLearner <double>[]
            {
                new RegressionDecisionTreeLearner(2),
                new RegressionDecisionTreeLearner(5),
                new RegressionDecisionTreeLearner(7),
                new RegressionDecisionTreeLearner(9)
            };

            var learner = new RegressionStackingEnsembleLearner(learners,
                                                                new RegressionDecisionTreeLearner(9),
                                                                new RandomCrossValidation <double>(5, 23), false);

            var sut = learner.Learn(observations, targets);

            var actual   = sut.GetVariableImportance(featureNameToIndex);
            var expected = new Dictionary <string, double> {
                { "RegressionDecisionTreeModel_2", 100 }, { "RegressionDecisionTreeModel_1", 69.7214491857349 }, { "RegressionDecisionTreeModel_0", 33.8678328474247 }, { "RegressionDecisionTreeModel_3", 1.70068027210884 }
            };

            Assert.AreEqual(expected.Count, actual.Count);
            var zip = expected.Zip(actual, (e, a) => new { Expected = e, Actual = a });

            foreach (var item in zip)
            {
                Assert.AreEqual(item.Expected.Key, item.Actual.Key);
                Assert.AreEqual(item.Expected.Value, item.Actual.Value, 0.000001);
            }
        }