예제 #1
0
        public void TestSvc()
        {
            var clf = new Svc <int>(kernel: Kernel.Linear, probability: true);

            clf.Fit(X, Y);

            var spClf = new Svc <int>(kernel: Kernel.Linear, probability: true);

            spClf.Fit(SparseMatrix.OfMatrix(X), Y);

            Assert.IsTrue(spClf.Predict(T).SequenceEqual(true_result));

            Assert.IsTrue(spClf.SupportVectors is SparseMatrix);
            Assert.IsTrue(clf.SupportVectors.AlmostEquals(spClf.SupportVectors));

            Assert.IsTrue(spClf.DualCoef is SparseMatrix);
            Assert.IsTrue(clf.DualCoef.AlmostEquals(spClf.DualCoef));

            Assert.IsTrue(spClf.Coef is SparseMatrix);
            Assert.IsTrue(clf.Coef.AlmostEquals(spClf.Coef));
            Assert.IsTrue(clf.Support.SequenceEqual(spClf.Support));
            Assert.IsTrue(clf.Predict(T).SequenceEqual(spClf.Predict(T)));

            // refit with a different dataset

            clf.Fit(X2, Y2);
            spClf.Fit(SparseMatrix.OfMatrix(X2), Y2);
            Assert.IsTrue(clf.SupportVectors.AlmostEquals(spClf.SupportVectors));
            Assert.IsTrue(clf.DualCoef.AlmostEquals(spClf.DualCoef));
            Assert.IsTrue(clf.Coef.AlmostEquals(spClf.Coef));
            Assert.IsTrue(clf.Support.SequenceEqual(spClf.Support));
            Assert.IsTrue(clf.Predict(T2).SequenceEqual(spClf.Predict(T2)));
            Assert.IsTrue(clf.PredictProba(T2).AlmostEquals(spClf.PredictProba(T2), 0.001));
        }
예제 #2
0
        /// <summary>
        /// Make some classification predictions on a toy dataset using a SVC
        ///
        /// If binary is True restrict to a binary classification problem instead of a
        /// multiclass classification problem
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="binary"></param>
        private static Tuple <int[], int[], Matrix <double> > MakePrediction(
            Matrix <double> x = null,
            int[] y           = null,
            bool binary       = false)
        {
            if (x == null && y == null)
            {
                // import some data to play with
                var dataset = IrisDataset.Load();
                x = dataset.Data;
                y = dataset.Target;
            }

            if (binary)
            {
                // restrict to a binary classification task
                x = x.RowsAt(y.Indices(v => v < 2));
                y = y.Where(v => v < 2).ToArray();
            }

            int nSamples  = x.RowCount;
            int nFeatures = x.ColumnCount;
            var rng       = new Random(37);

            int[] p = Shuffle(rng, Enumerable.Range(0, nSamples).ToArray());
            x = x.RowsAt(p);
            y = y.ElementsAt(p);
            var half = nSamples / 2;

            // add noisy features to make the problem harder and avoid perfect results
            rng = new Random(0);
            x   = x.HStack(DenseMatrix.CreateRandom(nSamples, 200, new Normal {
                RandomSource = rng
            }));

            // run classifier, get class probabilities and label predictions
            var clf = new Svc <int>(kernel: Kernel.Linear, probability: true);

            clf.Fit(x.SubMatrix(0, half, 0, x.ColumnCount), y.Take(half).ToArray());
            Matrix <double> probasPred = clf.PredictProba(x.SubMatrix(half, x.RowCount - half, 0, x.ColumnCount));

            if (binary)
            {
                // only interested in probabilities of the positive case
                // XXX: do we really want a special API for the binary case?
                probasPred = probasPred.SubMatrix(0, probasPred.RowCount, 1, 1);
            }

            var yPred = clf.Predict(x.SubMatrix(half, x.RowCount - half, 0, x.ColumnCount));
            var yTrue = y.Skip(half).ToArray();

            return(Tuple.Create(yTrue, yPred, probasPred));
        }
예제 #3
0
        /// <summary>
        /// Make some classification predictions on a toy dataset using a SVC
        ///
        /// If binary is True restrict to a binary classification problem instead of a
        /// multiclass classification problem
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="binary"></param>
        private static Tuple<int[], int[], Matrix<double>> MakePrediction(
            Matrix<double> x = null,
            int[] y = null,
            bool binary = false)
        {
            if (x == null && y == null)
            {
                // import some data to play with
                var dataset = IrisDataset.Load();
                x = dataset.Data;
                y = dataset.Target;
            }

            if (binary)
            {
                // restrict to a binary classification task
                x = x.RowsAt(y.Indices(v => v < 2));
                y = y.Where(v => v < 2).ToArray();
            }

            int nSamples = x.RowCount;
            int nFeatures = x.ColumnCount;
            var rng = new Random(37);
            int[] p = Shuffle(rng, Enumerable.Range(0, nSamples).ToArray());
            x = x.RowsAt(p);
            y = y.ElementsAt(p);
            var half = nSamples/2;

            // add noisy features to make the problem harder and avoid perfect results
            rng = new Random(0);
            x = x.HStack(DenseMatrix.CreateRandom(nSamples, 200, new Normal{RandomSource = rng}));

            // run classifier, get class probabilities and label predictions
            var clf = new Svc<int>(kernel: Kernel.Linear, probability: true);
            clf.Fit(x.SubMatrix(0, half, 0, x.ColumnCount), y.Take(half).ToArray());
            Matrix<double> probasPred = clf.PredictProba(x.SubMatrix(half, x.RowCount - half, 0, x.ColumnCount));

            if (binary)
            {
                // only interested in probabilities of the positive case
                // XXX: do we really want a special API for the binary case?
                probasPred = probasPred.SubMatrix(0, probasPred.RowCount, 1, 1);
            }

            var yPred = clf.Predict(x.SubMatrix(half, x.RowCount - half, 0, x.ColumnCount));
            var yTrue = y.Skip(half).ToArray();
            return Tuple.Create(yTrue, yPred, probasPred);
        }
예제 #4
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));
        }
예제 #5
0
        public void TestSvc()
        {
            var clf = new Svc<int>(kernel: Kernel.Linear, probability: true);
            clf.Fit(X, Y);

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

            Assert.IsTrue(spClf.Predict(T).SequenceEqual(true_result));

            Assert.IsTrue(spClf.SupportVectors is SparseMatrix);
            Assert.IsTrue(clf.SupportVectors.AlmostEquals(spClf.SupportVectors));

            Assert.IsTrue(spClf.DualCoef is SparseMatrix);
            Assert.IsTrue(clf.DualCoef.AlmostEquals(spClf.DualCoef));

            Assert.IsTrue(spClf.Coef is SparseMatrix);
            Assert.IsTrue(clf.Coef.AlmostEquals(spClf.Coef));
            Assert.IsTrue(clf.Support.SequenceEqual(spClf.Support));
            Assert.IsTrue(clf.Predict(T).SequenceEqual(spClf.Predict(T)));

            // refit with a different dataset

            clf.Fit(X2, Y2);
            spClf.Fit(SparseMatrix.OfMatrix(X2), Y2);
            Assert.IsTrue(clf.SupportVectors.AlmostEquals(spClf.SupportVectors));
            Assert.IsTrue(clf.DualCoef.AlmostEquals(spClf.DualCoef));
            Assert.IsTrue(clf.Coef.AlmostEquals(spClf.Coef));
            Assert.IsTrue(clf.Support.SequenceEqual(spClf.Support));
            Assert.IsTrue(clf.Predict(T2).SequenceEqual(spClf.Predict(T2)));
            Assert.IsTrue(clf.PredictProba(T2).AlmostEquals(spClf.PredictProba(T2), 0.001));
        }
예제 #6
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));
        }