/// <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 })); }
internal SvcBase( LibSvmImpl impl, Kernel kernel, int degree, double gamma, double coef0, double tol, double c, double nu, double epsilon, bool shrinking, bool probability, int cacheSize, ClassWeightEstimator <TLabel> classWeightEstimator, bool verbose) : base( impl, kernel, degree, gamma, coef0, tol, c, nu, epsilon, shrinking, probability, cacheSize, verbose) { this.ClassWeightEstimator = classWeightEstimator; }
/// <summary> /// Initializes a new instance of the Svc class. /// </summary> /// <param name="c">Penalty parameter C of the error term.</param> /// <param name="kernel">Specifies the kernel type to be used in the algorithm.</param> /// <param name="degree"> Degree of kernel function. /// It is significant only in <see cref="Kernel.Poly"/>.</param> /// <param name="gamma"> /// Kernel coefficient for <see cref="Kernel.Rbf"/>, <see cref="Kernel.Poly"/> and <see cref="Kernel.Sigmoid"/>. /// If gamma is 0.0 then 1/nFeatures will be used instead. /// </param> /// <param name="coef0"> /// Independent term in kernel function. /// It is only significant in <see cref="Kernel.Poly"/> and <see cref="Kernel.Sigmoid"/>. /// </param> /// <param name="shrinking"> Whether to use the shrinking heuristic.</param> /// <param name="probability"> /// Whether to enable probability estimates. This must be enabled prior /// to calling <see cref="SvcBase{TLabel}.PredictProba"/>. /// </param> /// <param name="tol">Tolerance for stopping criterion.</param> /// <param name="cacheSize">Size of the kernel cache (in MB).</param> /// <param name="classWeightEstimator"> Set the parameter C of class i to class_weight[i]*C for /// Svc. If not given, all classes are supposed to have /// weight one. The 'auto' mode uses the values of y to /// automatically adjust weights inversely proportional to /// class frequencies.</param> /// <param name="verbose">Enable verbose output. Note that this setting takes advantage of a /// per-process runtime setting in libsvm that, if enabled, may not work /// properly in a multithreaded context.</param> public Svc( double c = 1.0, Kernel kernel = null, int degree = 3, double gamma = 0.0, double coef0 = 0.0, bool shrinking = true, bool probability = false, double tol = 1e-3, int cacheSize = 200, ClassWeightEstimator <TLabel> classWeightEstimator = null, bool verbose = false) : base( LibSvmImpl.c_svc, kernel ?? Svm.Kernel.Rbf, degree, gamma, coef0, tol, c, 0.0, 0.0, shrinking, probability, cacheSize, classWeightEstimator, verbose) { }
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); } }
/// <summary> /// Initializes a new instance of the LogisticRegression class. /// </summary> /// <param name="penalty">Used to specify the norm used in the penalization.</param> /// <param name="dual"> /// Dual or primal formulation. Dual formulation is only /// implemented for l2 penalty. Prefer dual=false when /// nSamples > nFeatures. /// </param> /// <param name="tol"></param> /// <param name="c">Inverse of regularization strength; must be a positive float. /// Like in support vector machines, smaller values specify stronger /// regularization. /// </param> /// <param name="fitIntercept"> /// Specifies if a constant (a.k.a. bias or intercept) should be /// added the decision function. /// </param> /// <param name="interceptScaling"> /// when fitIntercept is true, instance vector x becomes /// [x, self.intercept_scaling], /// i.e. a "synthetic" feature with constant value equals to /// interceptScaling is appended to the instance vector. /// The intercept becomes intercept_scaling * synthetic feature weight /// Note! the synthetic feature weight is subject to l1/l2 regularization /// as all other features. /// To lessen the effect of regularization on synthetic feature weight /// (and therefore on the intercept) interceptScaling has to be increased /// </param> /// <param name="classWeightEstimator">Set the parameter C of class i to class_weight[i]*C for /// SVC. If not given or ClassWeight.Uniform is used, all classes are supposed to have /// weight one. ClassWeight.Auto uses the values of y to /// automatically adjust weights inversely proportional to class frequencies.</param> /// <param name="random"> /// The seed of the pseudo random number generator to use when /// shuffling the data. /// </param> public LogisticRegression( Norm penalty = Norm.L2, bool dual = false, double tol = 1e-4, double c = 1.0, bool fitIntercept = true, double interceptScaling = 1, ClassWeightEstimator <TLabel> classWeightEstimator = null, Random random = null) : base(fitIntercept, penalty, Loss.LogisticRegression, dual, tol, c, interceptScaling: interceptScaling, random: random, classWeightEstimator: classWeightEstimator) { }
/// <summary> /// Initializes a new instance of the RidgeClassifier class. /// </summary> /// <param name="alpha"> Small positive values of alpha improve the conditioning of the problem /// and reduce the variance of the estimates. Alpha corresponds to /// ``(2*C)^-1`` in other linear models such as LogisticRegression or /// LinearSVC.</param> /// <param name="fitIntercept">Whether to calculate the intercept for this model. If set to false, no /// intercept will be used in calculations (e.g. data is expected to be /// already centered).</param> /// <param name="normalize">If True, the regressors X will be normalized before regression.</param> /// <param name="maxIter">Maximum number of iterations for conjugate gradient solver. /// The default value is determined by Math.Net.</param> /// <param name="tol">Precision of the solution.</param> /// <param name="classWeightEstimator">Weights associated with classes in the form /// {class_label : weight}. If not given, all classes are /// supposed to have weight one.</param> /// <param name="solver">Solver to use in the computational.</param> public RidgeClassifier( double alpha = 1.0, bool fitIntercept = true, bool normalize = false, int?maxIter = null, double tol = 1e-3, ClassWeightEstimator <TLabel> classWeightEstimator = null, RidgeSolver solver = RidgeSolver.Auto) : base(fitIntercept, alpha, normalize, maxIter, tol, solver) { if (classWeightEstimator == ClassWeightEstimator <TLabel> .Auto) { throw new ArgumentException("ClassWeight.Auto is not supported."); } this.ClassWeightEstimator = classWeightEstimator ?? ClassWeightEstimator <TLabel> .Uniform; }
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) * */ }
internal LibLinearBase( bool fitIntercept, Norm norm = Norm.L2, Loss loss = Loss.L2, bool dual = true, double tol = 1e-4, double c = 1.0, Multiclass multiclass = Multiclass.Ovr, double interceptScaling = 1, ClassWeightEstimator <TLabel> classWeightEstimator = null, int verbose = 0, Random random = null) : base(fitIntercept) { this.solverType = GetSolverType(norm, loss, dual, multiclass); //this.fitIntercept = fitIntercept; this.tol = tol; this.C = c; this.interceptScaling = interceptScaling; this.ClassWeightEstimator = classWeightEstimator ?? ClassWeightEstimator <TLabel> .Uniform; this.verbose = verbose; this.random = random; this.Dual = dual; }