예제 #1
0
        public void TestDecisionFunction()
        {
            // multi class:
            var clf = new Svc <int>(kernel: Kernel.Linear, c: 0.1);

            clf.Fit(iris.Data, iris.Target);

            var dec = (iris.Data * clf.Coef.Transpose()).AddRowVector(clf.Intercept);

            Assert.IsTrue(dec.AlmostEquals(clf.DecisionFunction(iris.Data)));

            // binary:
            clf.Fit(X, Y);
            dec = (X * clf.Coef.Transpose()).AddRowVector(clf.Intercept);
            int[] prediction = clf.Predict(X);
            Assert.IsTrue(dec.AlmostEquals(clf.DecisionFunction(X)));

            var b = clf.DecisionFunction(X).Column(0).Select(v => clf.Classes[v > 0 ? 1 : 0]);

            Assert.IsTrue(prediction.SequenceEqual(b));

            var expected = DenseMatrix.OfArray(new[, ] {
                { -1.0 }, { -0.66 }, { -1.0 }, { 0.66 }, { 1.0 }, { 1.0 }
            });

            Assert.IsTrue(clf.DecisionFunction(X).AlmostEquals(expected, 1E-2));
        }
예제 #2
0
        public void TestSvcWithCallableKernel()
        {
            // create SVM with callable linear kernel, check that results are the same
            // as with built-in linear kernel
            var svmCallable = new Svc <int>(kernel: Kernel.FromFunction((x, y) => x * (y.Transpose())),
                                            probability: true);

            svmCallable.Fit(X, Y);
            var svmBuiltin = new Svc <int>(kernel: Kernel.Linear, probability: true);

            svmBuiltin.Fit(X, Y);

            Assert.IsTrue(svmCallable.DualCoef.AlmostEquals(svmBuiltin.DualCoef));
            Assert.IsTrue(svmCallable.Intercept.AlmostEquals(svmBuiltin.Intercept));
            Assert.IsTrue(svmCallable.Predict(X).SequenceEqual(svmBuiltin.Predict(X)));

            Assert.IsTrue(svmCallable.PredictProba(X).AlmostEquals(svmBuiltin.PredictProba(X), 1));
            Assert.IsTrue(svmCallable.DecisionFunction(X).AlmostEquals(svmBuiltin.DecisionFunction(X), 2));
        }
예제 #3
0
        public void TestSvcWithCallableKernel()
        {
            // create SVM with callable linear kernel, check that results are the same
            // as with built-in linear kernel
            var svmCallable = new Svc<int>(kernel: Kernel.FromFunction((x, y) => x*(y.Transpose())),
                                            probability: true);

            svmCallable.Fit(X, Y);
            var svmBuiltin = new Svc<int>(kernel: Kernel.Linear, probability: true);
            svmBuiltin.Fit(X, Y);

            Assert.IsTrue(svmCallable.DualCoef.AlmostEquals(svmBuiltin.DualCoef));
            Assert.IsTrue(svmCallable.Intercept.AlmostEquals(svmBuiltin.Intercept));
            Assert.IsTrue(svmCallable.Predict(X).SequenceEqual(svmBuiltin.Predict(X)));

            Assert.IsTrue(svmCallable.PredictProba(X).AlmostEquals(svmBuiltin.PredictProba(X), 1));
            Assert.IsTrue(svmCallable.DecisionFunction(X).AlmostEquals(svmBuiltin.DecisionFunction(X), 2));
        }
예제 #4
0
        public void TestDecisionFunction()
        {
            // multi class:
            var clf = new Svc<int>(kernel: Kernel.Linear, c: 0.1);
            clf.Fit(iris.Data, iris.Target);

            var dec = (iris.Data*clf.Coef.Transpose()).AddRowVector(clf.Intercept);

            Assert.IsTrue(dec.AlmostEquals(clf.DecisionFunction(iris.Data)));

            // binary:
            clf.Fit(X, Y);
            dec = (X*clf.Coef.Transpose()).AddRowVector(clf.Intercept);
            int[] prediction = clf.Predict(X);
            Assert.IsTrue(dec.AlmostEquals(clf.DecisionFunction(X)));

            var b = clf.DecisionFunction(X).Column(0).Select(v => clf.Classes[v > 0 ? 1 : 0]);
            Assert.IsTrue(prediction.SequenceEqual(b));

            var expected = DenseMatrix.OfArray(new[,] {{-1.0}, {-0.66}, {-1.0}, {0.66}, {1.0}, {1.0}});
            Assert.IsTrue(clf.DecisionFunction(X).AlmostEquals(expected, 1E-2));
        }