public void TestLogisticRegressionTest() { var inputs = new double[][] { // age, smokes? new double[] { 55, 0 }, new double[] { 28, 0 }, new double[] { 65, 1 }, new double[] { 46, 0 }, new double[] { 86, 1 }, new double[] { 56, 1 }, new double[] { 85, 0 }, new double[] { 33, 0 }, new double[] { 21, 1 }, new double[] { 42, 1 }, }; var outputs = new double[][] { // Whether each patient had lung cancer or not new double[] { 1, 0 }, new double[] { 1, 0 }, new double[] { 1, 0 }, new double[] { 0, 1 }, new double[] { 0, 1 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 1, 0 }, new double[] { 1, 0 }, new double[] { 0, 1 } }; // Accord.NET var analysis = new MultinomialLogisticRegressionAnalysis(); analysis.Learn(inputs, outputs); var expected = analysis.Regression.Probabilities(inputs); // Estimator var x = DataMap.FromJagged(inputs, new string[] { "age", "smokes" }); var y = DataMap.FromJagged(outputs, new string[] { "no_cancer", "cancer" }); var est = new LogisticRegressionEstimator(); est.Fit(x, y); var model = est.Model; Assert.Equal(analysis.Coefficients.Count, model.Coefficients.Count); Assert.Equal(analysis.Coefficients[0].Value, model.Coefficients[0].Value); Assert.Equal(analysis.Coefficients[1].Value, model.Coefficients[1].Value); Assert.Equal(analysis.Coefficients[2].Value, model.Coefficients[2].Value); var predicted = est.Predict(x).ToJagged <double>(); Assert.Equal(expected[0], predicted[0]); Assert.Equal(expected[1], predicted[1]); Assert.Equal(expected[5], predicted[5]); Assert.Equal(expected[9], predicted[9]); }
public void TestPlattCalibratorTest() { var inputs = DataMap.FromJagged(new double[][] { new double[] { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5 } }, null, true); var outputs = DataMap.FromJagged(new double[][] { new double[] { 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1 } }, null, true); var valid = DataMap.FromJagged(new double[][] { new double[] { 0, 1, 2, 3, 4, 5 } }, null, true); var est = new SigmoidCalibrator(); est.Fit(inputs, outputs); var pred = est.Predict(valid).First; Assert.True((double)pred[0] < (double)pred[1]); Assert.True((double)pred[1] < (double)pred[2]); Assert.True((double)pred[2] < (double)pred[3]); Assert.True((double)pred[3] < (double)pred[4]); Assert.True((double)pred[4] < (double)pred[5]); }
public void TestLinearRegressionTest() { var inputs = new double[][] { // age, smokes? new double[] { 55, 0 }, new double[] { 28, 0 }, new double[] { 65, 1 }, new double[] { 46, 0 }, new double[] { 86, 1 }, new double[] { 56, 1 }, new double[] { 85, 0 }, new double[] { 33, 0 }, new double[] { 21, 1 }, new double[] { 42, 1 }, }; var output = new double[] { // Whether each patient had lung cancer or not 0, 0, 0, 1, 1, 1, 0, 0, 0, 1 }; // Accord.NET var analysis = new MultipleLinearRegressionAnalysis(); analysis.OrdinaryLeastSquares.UseIntercept = true; analysis.OrdinaryLeastSquares.IsRobust = false; analysis.Learn(inputs, output); // Estimator var x = DataMap.FromJagged(inputs, new string[] { "age", "smokes" }); var y = new DataMap(); y.Add("lung_cancer", output); var est = new LinearRegressionEstimator(); est.Fit(x, y); var model = est.Model; Assert.Equal(analysis.Coefficients.Count, model.Coefficients.Count); Assert.Equal(analysis.Coefficients[0].Value, model.Coefficients[0].Value); Assert.Equal(analysis.Coefficients[1].Value, model.Coefficients[1].Value); Assert.Equal(analysis.Coefficients[2].Value, model.Coefficients[2].Value); }