Example #1
0
        public void RegressionAdaBoostModel_GetVariableImportance()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

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

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

            var actual   = sut.GetVariableImportance(featureNameToIndex);
            var expected = new Dictionary <string, double> {
                { "PreviousExperience_month", 100.0 },
                { "AptitudeTestScore", 33.8004886838701 }
            };

            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);
            }
        }
Example #2
0
        public void RegressionAdaBoostModel_GetVariableImportance()
        {
            var parser             = new CsvParser(() => new StringReader(Resources.AptitudeData));
            var observations       = parser.EnumerateRows(v => v != "Pass").ToF64Matrix();
            var targets            = parser.EnumerateRows("Pass").ToF64Vector();
            var featureNameToIndex = new Dictionary <string, int> {
                { "AptitudeTestScore", 0 },
                { "PreviousExperience_month", 1 }
            };

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

            var actual   = sut.GetVariableImportance(featureNameToIndex);
            var expected = new Dictionary <string, double> {
                { "PreviousExperience_month", 100.0 },
                { "AptitudeTestScore", 33.8004886838701 }
            };

            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);
            }
        }
Example #3
0
        public void RegressionAdaBoostLearner_Learn_AptitudeData_ExponentialLoss()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

            var sut = new RegressionAdaBoostLearner(10, 1, 0, AdaBoostRegressionLoss.Exponential);

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

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

            Assert.AreEqual(0.10370879120879124, actual);
        }
Example #4
0
        public void RegressionAdaBoostModel_Precit_Multiple()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

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

            var predictions = sut.Predict(observations);

            var evaluator = new MeanAbsolutErrorRegressionMetric();
            var error     = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.14185814185814186, error, 0.0000001);
        }
Example #5
0
        public void RegressionAdaBoostLearner_Learn_Glass()
        {
            var(observations, targets) = DataSetUtilities.LoadGlassDataSet();

            var sut = new RegressionAdaBoostLearner(10);

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

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

            Assert.AreEqual(0.54723570404775324, actual);
        }
Example #6
0
        public void RegressionAdaBoostLearner_Learn_AptitudeData_LinearLoss()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

            var sut = new RegressionAdaBoostLearner(10);

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

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

            Assert.AreEqual(0.14185814185814186, actual);
        }
Example #7
0
        public void RegressionAdaBoostModel_Save()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

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

            var writer = new StringWriter();

            sut.Save(() => writer);

            var actual = writer.ToString();

            Assert.AreEqual(RegressionDecisionTreeModelString, actual);
        }
        public void RegressionAdaBoostLearner_Learn_Glass()
        {
            var parser       = new CsvParser(() => new StringReader(Resources.Glass));
            var observations = parser.EnumerateRows(v => v != "Target").ToF64Matrix();
            var targets      = parser.EnumerateRows("Target").ToF64Vector();

            var sut = new RegressionAdaBoostLearner(10);

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

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

            Assert.AreEqual(0.54723570404775324, actual);
        }
        public void RegressionAdaBoostLearner_Learn_AptitudeData_ExponentialLoss()
        {
            var parser       = new CsvParser(() => new StringReader(Resources.AptitudeData));
            var observations = parser.EnumerateRows(v => v != "Pass").ToF64Matrix();
            var targets      = parser.EnumerateRows("Pass").ToF64Vector();

            var sut = new RegressionAdaBoostLearner(10, 1, 0, AdaBoostRegressionLoss.Exponential);

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

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

            Assert.AreEqual(0.10370879120879124, actual);
        }
Example #10
0
        public void RegressionAdaBoostModel_GetRawVariableImportance()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

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

            var actual   = sut.GetRawVariableImportance();
            var expected = new double[] { 5.9121817956101106, 17.49140922459933 };

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

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i], 0.000001);
            }
        }
Example #11
0
        public void RegressionAdaBoostModel_Precit_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 learner = new RegressionAdaBoostLearner(10);
            var sut     = learner.Learn(observations, targets);

            var predictions = sut.Predict(observations);

            var evaluator = new MeanAbsolutErrorRegressionMetric();
            var error     = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.14185814185814186, error, 0.0000001);
        }
Example #12
0
        public void RegressionAdaBoostModel_Save()
        {
            var parser       = new CsvParser(() => new StringReader(Resources.AptitudeData));
            var observations = parser.EnumerateRows(v => v != "Pass").ToF64Matrix();
            var targets      = parser.EnumerateRows("Pass").ToF64Vector();

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

            var writer = new StringWriter();

            sut.Save(() => writer);

            var actual = writer.ToString();

            Assert.AreEqual(RegressionDecisionTreeModelString, actual);
        }
Example #13
0
        public void RegressionAdaBoostModel_GetRawVariableImportance()
        {
            var parser       = new CsvParser(() => new StringReader(Resources.AptitudeData));
            var observations = parser.EnumerateRows(v => v != "Pass").ToF64Matrix();
            var targets      = parser.EnumerateRows("Pass").ToF64Vector();

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

            var actual   = sut.GetRawVariableImportance();
            var expected = new double[] { 5.9121817956101106, 17.49140922459933 };

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

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i], 0.000001);
            }
        }
Example #14
0
        public void RegressionAdaBoostModel_Predict_Single()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();
            var rows = targets.Length;

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

            var predictions = new double[rows];

            for (int i = 0; i < rows; i++)
            {
                predictions[i] = sut.Predict(observations.Row(i));
            }

            var evaluator = new MeanAbsolutErrorRegressionMetric();
            var error     = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.14185814185814186, error, 0.0000001);
        }
Example #15
0
        public void RegressionAdaBoostLearner_Learn_Glass_Indexed()
        {
            var(observations, targets) = DataSetUtilities.LoadGlassDataSet();

            var sut = new RegressionAdaBoostLearner(10, 1, 5);

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

            indices.Shuffle(new Random(42));
            indices = indices.Take((int)(targets.Length * 0.7))
                      .ToArray();

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

            var evaluator = new MeanAbsolutErrorRegressionMetric();
            var actual    = evaluator.Error(indexedTargets, indexedPredictions);

            Assert.AreEqual(0.22181054803405248, actual);
        }
        public void RegressionAdaBoostLearner_Learn_Glass_Indexed()
        {
            var parser       = new CsvParser(() => new StringReader(Resources.Glass));
            var observations = parser.EnumerateRows(v => v != "Target").ToF64Matrix();
            var targets      = parser.EnumerateRows("Target").ToF64Vector();

            var sut = new RegressionAdaBoostLearner(10, 1, 5);

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

            indices.Shuffle(new Random(42));
            indices = indices.Take((int)(targets.Length * 0.7))
                      .ToArray();

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

            var evaluator = new MeanAbsolutErrorRegressionMetric();
            var actual    = evaluator.Error(indexedTargets, indexedPredictions);

            Assert.AreEqual(0.22181054803405248, actual);
        }
Example #17
0
        /// <summary>
        /// Predição de Floresta Aleatória e Rede Neural
        /// </summary>
        public void RegressionLearner_Learn_And_Predict()
        {
            #region Treinamento da Floresta Aleatória
            var parser     = new CsvParser(() => new StringReader(treinamento));
            var targetName = "T";

            var observations = parser.EnumerateRows(c => c != targetName)
                               .ToF64Matrix();
            var targets = parser.EnumerateRows(targetName)
                          .ToF64Vector();
            UltimaObservacao = new double[] { observations[observations.RowCount - 1, 0], observations[observations.RowCount - 1, 2], observations[observations.RowCount - 1, 3], observations[observations.RowCount - 1, 4], observations[observations.RowCount - 1, 5], targets[targets.Count() - 1] };

            var learner = new RegressionRandomForestLearner(trees: 500);
            model = learner.Learn(observations, targets);
            #endregion

            #region Teste da Floresta Aleatória
            parser = new CsvParser(() => new StringReader(teste));
            var observationsTeste = parser.EnumerateRows(c => c != targetName)
                                    .ToF64Matrix();
            var targetsTeste = parser.EnumerateRows(targetName)
                               .ToF64Vector();

            // predict the training and test set.
            var trainPredictions = model.Predict(observations);
            var testPredictions  = model.Predict(observationsTeste);

            // create the metric
            var metric = new MeanSquaredErrorRegressionMetric();


            // measure the error on training and test set.
            trainError = metric.Error(targets, trainPredictions);
            testError  = metric.Error(targetsTeste, testPredictions);
            #endregion

            #region Treinamento da Rede Neural
            var net = new NeuralNet();
            net.Add(new InputLayer(6));
            net.Add(new DropoutLayer(0.2));
            net.Add(new DenseLayer(800, Activation.Relu));
            net.Add(new DropoutLayer(0.5));
            net.Add(new DenseLayer(800, Activation.Relu));
            net.Add(new DropoutLayer(0.5));
            net.Add(new SquaredErrorRegressionLayer());

            var learnernet = new RegressionNeuralNetLearner(net, iterations: 500, loss: new SquareLoss());
            modelnet = learnernet.Learn(observations, targets);
            #endregion

            #region Teste da Rede Neural
            trainPredictions = modelnet.Predict(observations);
            testPredictions  = modelnet.Predict(observationsTeste);

            trainErrorNet = metric.Error(targets, trainPredictions);
            testErrorNet  = metric.Error(targetsTeste, testPredictions);
            #endregion

            #region Treinamento Ada
            var learnerada = new RegressionAdaBoostLearner(maximumTreeDepth: 35, iterations: 2000, learningRate: 0.1);
            modelada = learnerada.Learn(observations, targets);
            #endregion

            #region Teste Ada
            trainPredictions = modelada.Predict(observations);
            testPredictions  = modelada.Predict(observationsTeste);

            trainErrorAda = metric.Error(targets, trainPredictions);
            testErrorAda  = metric.Error(targetsTeste, testPredictions);

            string stargets          = "";
            string strainPredictions = "";
            string stargetsTeste     = "";
            string stestPredictions  = "";

            foreach (var i in targets)
            {
                stargets += i + ";";
            }
            foreach (var i in trainPredictions)
            {
                strainPredictions += i + ";";
            }
            foreach (var i in targetsTeste)
            {
                stargetsTeste += i + ";";
            }
            foreach (var i in testPredictions)
            {
                stestPredictions += i + ";";
            }
            #endregion
        }