private static void sparseLogistic(Sparse <double>[] inputs, double[] doubleOutputs) { // The dataset has output labels as 4 and 2. We have to convert them // into negative and positive labels so they can be properly processed. // bool[] outputs = doubleOutputs.Apply(x => x == 2.0 ? false : true); // Create a probabilistic SVM that can output probabilities besides a decision var teacher = new ProbabilisticDualCoordinateDescent <Linear, Sparse <double> >() { Complexity = 1000, Tolerance = 1e-5 }; // Use the learning algorithm to Learn var svm = teacher.Learn(inputs, outputs); // Transform the machine into a dense logistic regression: var lr = LogisticRegression.FromWeights(svm.ToWeights()); // Compute the machine's answers bool[] svmAnswers = svm.Decide(inputs); // Compute the machine probability estimates: double[] svmProbability = svm.Probability(inputs); // Compute the logistic regression's answers: bool[] lrAnswers = lr.Decide(inputs.ToDense()); // Compute the logistic regression probability estimates: double[] lrProbability = lr.Probability(inputs.ToDense()); // They should be equal for both the SVM and the LR }
public void RunTest() { double[][] input = { new double[] { 55, 0 }, // 0 - no cancer new double[] { 28, 0 }, // 0 new double[] { 65, 1 }, // 0 new double[] { 46, 0 }, // 1 - have cancer new double[] { 86, 1 }, // 1 new double[] { 56, 1 }, // 1 new double[] { 85, 0 }, // 0 new double[] { 33, 0 }, // 0 new double[] { 21, 1 }, // 0 new double[] { 42, 1 }, // 1 }; double[] output = { 0, 0, 0, 1, 1, 1, 0, 0, 0, 1 }; int[] labels = output.Apply(x => x > 0 ? +1 : -1); var svm = new SupportVectorMachine(inputs: 2); var teacher = new ProbabilisticNewtonMethod(svm, input, labels); teacher.Tolerance = 1e-10; teacher.Complexity = 1e+10; Assert.IsFalse(svm.IsProbabilistic); double error = teacher.Run(); Assert.IsTrue(svm.IsProbabilistic); Assert.AreEqual(0.2, error); Assert.AreEqual(0.02064511826338301, svm.SupportVectors[0][0], 1e-10); Assert.AreEqual(1.767889310996118, svm.SupportVectors[0][1], 1e-10); Assert.AreEqual(-2.4577464317497455, svm.Threshold, 1e-10); var regression = LogisticRegression.FromWeights(svm.ToWeights()); double[] actual = new double[output.Length]; for (int i = 0; i < actual.Length; i++) { actual[i] = regression.Compute(input[i]); } double ageOdds = regression.GetOddsRatio(1); // 1.0208597028836701 double smokeOdds = regression.GetOddsRatio(2); // 5.8584748789881331 Assert.AreEqual(1.0208597028836701, ageOdds, 1e-4); Assert.AreEqual(5.8584748789881331, smokeOdds, 1e-4); Assert.AreEqual(-2.4577464307294092, regression.Intercept, 1e-8); Assert.AreEqual(-2.4577464307294092, regression.Coefficients[0], 1e-8); Assert.AreEqual(0.020645118265359252, regression.Coefficients[1], 1e-8); Assert.AreEqual(1.7678893101571855, regression.Coefficients[2], 1e-8); }