示例#1
0
        /// <summary>
        /// Test class weights.
        /// </summary>
        public void TestClassWeights()
        {
            Matrix x = DenseMatrix.OfArray(new[, ]
            {
                { -1.0, -1.0 }, { -1.0, 0 }, { -.8, -1.0 },
                { 1.0, 1.0 }, { 1.0, 0.0 }
            });
            var y = new[] { 1, 1, 1, -1, -1 };

            var clf = new RidgeClassifier <int>(classWeightEstimator: null);

            clf.Fit(x, y);
            Assert.AreEqual(
                clf.Predict(DenseMatrix.OfArray(new[, ] {
                { 0.2, -1.0 }
            })),
                new DenseMatrix(1, 1, new double[] { 1 }));

            // we give a small weights to class 1
            clf = new RidgeClassifier <int>(
                classWeightEstimator: ClassWeightEstimator <int> .Explicit(new Dictionary <int, double> {
                { 1, 0.001 }
            }));

            clf.Fit(x, y);

            // now the hyperplane should rotate clock-wise and
            // the prediction on this point should shift
            Assert.AreEqual(
                clf.Predict(DenseMatrix.OfArray(new[, ] {
                { 0.2, -1.0 }
            })),
                new DenseMatrix(1, 1, new double[] { -1 }));
        }
示例#2
0
        public void RidgeClassifierSample()
        {
            var clf = new RidgeClassifier<string>(alpha: 0.5);
            clf.Fit(new[,] { { 0.0, 0.0 }, { 0.0, 0.0 }, { 1.0, 1.0 } }, new[] { "a", "b", "c" });
            Console.WriteLine(clf.Coef);
            Console.WriteLine(clf.Intercept);

            var prediction = clf.Predict(new[] { 5.0, 6.0 });
            Console.WriteLine(prediction);
        }
示例#3
0
        public void RidgeClassifierSample()
        {
            var clf = new RidgeClassifier <string>(alpha: 0.5);

            clf.Fit(new[, ] {
                { 0.0, 0.0 }, { 0.0, 0.0 }, { 1.0, 1.0 }
            }, new[] { "a", "b", "c" });
            Console.WriteLine(clf.Coef);
            Console.WriteLine(clf.Intercept);

            var prediction = clf.Predict(new[] { 5.0, 6.0 });

            Console.WriteLine(prediction);
        }
示例#4
0
        public double?TestRidgeClassifiers(Func <Matrix <double>, Matrix <double> > filter)
        {
            int nClasses  = yIris.Distinct().Count();
            int nFeatures = xIris.ColumnCount;

            var clf = new RidgeClassifier <int>();

            clf.Fit(filter(xIris), yIris);

            Assert.AreEqual(clf.Coef.Shape(), Tuple.Create(nClasses, nFeatures));
            int[] yPred  = clf.Predict(filter(xIris));
            var   result = yIris.Zip(yPred, Tuple.Create).Sum(t => t.Item1 == t.Item2 ? 1.0 : 0.0) / yPred.Length;

            Assert.IsTrue(result >= .79);

            return(null);
        }
示例#5
0
        /// <summary>
        /// Test class weights.
        /// </summary>
        public void TestClassWeights()
        {
            Matrix x = DenseMatrix.OfArray(new[,]
                                           {
                                               {-1.0, -1.0}, {-1.0, 0}, {-.8, -1.0},
                                               {1.0, 1.0}, {1.0, 0.0}
                                           });
            var y = new[] { 1, 1, 1, -1, -1 };

            var clf = new RidgeClassifier<int>(classWeightEstimator: null);
            clf.Fit(x, y);
            Assert.AreEqual(
                clf.Predict(DenseMatrix.OfArray(new[,] { { 0.2, -1.0 } })),
                new DenseMatrix(1, 1, new double[] { 1 }));

            // we give a small weights to class 1
            clf = new RidgeClassifier<int>(
                classWeightEstimator: ClassWeightEstimator<int>.Explicit(new Dictionary<int, double> { { 1, 0.001 } }));

            clf.Fit(x, y);

            // now the hyperplane should rotate clock-wise and
            // the prediction on this point should shift
            Assert.AreEqual(
                clf.Predict(DenseMatrix.OfArray(new[,] { { 0.2, -1.0 } })),
                new DenseMatrix(1, 1, new double[] { -1 }));
        }
示例#6
0
        public double? TestRidgeClassifiers(Func<Matrix<double>, Matrix<double>> filter)
        {
            int nClasses = yIris.Distinct().Count();
            int nFeatures = xIris.ColumnCount;

            var clf = new RidgeClassifier<int>();
            clf.Fit(filter(xIris), yIris);

            Assert.AreEqual(clf.Coef.Shape(), Tuple.Create(nClasses, nFeatures));
            int[] yPred = clf.Predict(filter(xIris));
            var result = yIris.Zip(yPred, Tuple.Create).Sum(t => t.Item1 == t.Item2 ? 1.0 : 0.0) / yPred.Length;
            Assert.IsTrue(result >= .79);

            return null;
        }