コード例 #1
0
        public void TestWeight()
        {
            var classification =
                SampleGenerator.MakeClassification(
                    nSamples: 200,
                    nFeatures: 100,
                    weights: new[] { 0.833, 0.167 }.ToList(),
                    randomState: new Random(0));

            var classWeight = ClassWeightEstimator <int> .Explicit(new Dictionary <int, double> {
                { 0, 5 }
            });

            Matrix x = SparseMatrix.OfMatrix(classification.X);

            foreach (var clf in new IClassifier <int>[]
            {
                new LogisticRegression <int>(classWeightEstimator: classWeight),
                //new LinearSvc(classWeight:classWeight, random_state=0),
                //new Svc<int>(classWeight: classWeight)
            })
            {
                clf.Fit(x.SubMatrix(0, 180, 0, x.ColumnCount), classification.Y.Take(180).ToArray());
                var yPred = clf.Predict(x.SubMatrix(180, x.RowCount - 180, 0, x.ColumnCount));

                var matchingN =
                    yPred.Zip(classification.Y.Skip(180), Tuple.Create).Where(t => t.Item1 == t.Item2).Count();
                Assert.IsTrue(matchingN >= 11);
            }
        }
コード例 #2
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 }));
        }
コード例 #3
0
        public void test_weight()
        {
            var clf = new Svc <int>(classWeightEstimator: ClassWeightEstimator <int> .Explicit(new Dictionary <int, double> {
                { 1, 0.1 }
            }));

            // we give a small weights to class 1
            clf.Fit(X, Y);
            // so all predicted values belong to class 2
            Assert.IsTrue(clf.Predict(X).SequenceEqual(Enumerable.Repeat(2, 6)));

            /*
             * X_, y_ = make_classification(n_samples=200, n_features=10,
             *                   weights=[0.833, 0.167], random_state=2)
             *
             * for clf in (linear_model.LogisticRegression(),
             *  svm.LinearSVC(random_state=0), svm.SVC()):
             * clf.set_params(class_weight={0: .1, 1: 10})
             * clf.fit(X_[:100], y_[:100])
             * y_pred = clf.predict(X_[100:])
             * assert_true(f1_score(y_[100:], y_pred) > .3)
             * */
        }