mojolearn.LinearSVC#

class mojolearn.LinearSVC(*, penalty='l2', loss='squared_hinge', C=1.0, fit_intercept=True, penalized_intercept=False, class_weight=None, tol=0.0001, max_iter=1000, linesearch_max_iter=100, lbfgs_memory=5, multi_class='ovr')[source]#

Bases: _LinearSVMBase

Linear support vector classification.

Minimizes the hinge or squared hinge loss plus an l2 or l1 penalty (see the mojolearn.svm module description for the objective). Two classes fit one machine; more classes fit one machine per class against the rest, in class order. The intercept is never penalized.

X is read as float32. y is a 1-D array of labels; classes_ is the sorted unique labels. Invalid parameter values raise ValueError.

Parameters:
  • penalty ({‘l2’, ‘l1’}, default ‘l2’) – The regularization term; 'l1' uses OWL-QN and gives exact zeros.

  • loss ({‘squared_hinge’, ‘hinge’}, default ‘squared_hinge’) – The data-fit loss.

  • C (float, default 1.0) – Inverse regularization strength, > 0.

  • fit_intercept (bool, default True) – Whether to fit an (unpenalized) intercept.

  • penalized_intercept (bool, default False) – Only False is supported; True raises NotImplementedError.

  • class_weight (None) – Must be None; anything else raises NotImplementedError.

  • tol (float, default 1e-4) – Gradient tolerance, > 0.

  • max_iter (int, default 1000) – Maximum number of solver iterations per machine.

  • linesearch_max_iter (int, default 100) – Maximum number of line-search steps per iteration.

  • lbfgs_memory (int, default 5) – Number of past updates kept by L-BFGS.

  • multi_class ({‘ovr’}, default ‘ovr’) – One-vs-rest, the only supported value.

  • numeric_mode ({‘identical’, ‘fast’} or None, default None) – The numeric mode for this estimator; None uses the process default (see mojolearn.set_numeric_mode). 'fast' is for Apple GPUs.

Variables:
  • classes_ (list) – The sorted class labels.

  • coef_ (array of shape (1, n_features) or (n_classes, n_features), float32) – Coefficients: one row for two classes, one row per class for more.

  • intercept_ (array of shape (1,) or (n_classes,), float32) – Intercepts; zeros when fit_intercept is False.

  • n_iter_ (array of shape (1,), int64) – The largest iteration count over the machines.

  • objective_ (float or list of float) – The final objective value (one per machine when there are more than two classes).

  • retcode_ (int or list of int) – The solver’s return code, as on LogisticRegression (one per machine when there are more than two classes).

  • n_features_in_ (int) – The number of features seen in fit.

  • input_copied_ (bool) – Whether fit had to copy X to float32 C order.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = (X[:, 0] + X[:, 1] > 1.0).astype(np.int64)
>>> clf = mojolearn.LinearSVC(C=10.0).fit(X, y)
>>> labels = clf.predict(X)
__init__(*, penalty='l2', loss='squared_hinge', C=1.0, fit_intercept=True, penalized_intercept=False, class_weight=None, tol=0.0001, max_iter=1000, linesearch_max_iter=100, lbfgs_memory=5, multi_class='ovr')[source]#

Methods

decision_function(X)

Return the linear scores X @ coef_.T + intercept_.

fit(X, y[, sample_weight])

Fit the classifier.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict class labels.

score(X, y)

Return the mean accuracy on the given data.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y, sample_weight=None)[source]#

Fit the classifier.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, read as float32.

  • y (array-like of shape (n_samples,)) – Class labels, at least two distinct values. A 2-D y, a single class or a length mismatch raises ValueError.

  • sample_weight (None) – Must be None; weights raise NotImplementedError.

Returns:

self (LinearSVC) – The fitted estimator.

decision_function(X)[source]#

Return the linear scores X @ coef_.T + intercept_.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, read as float32.

Returns:

scores (array of shape (n_samples,) or (n_samples, n_classes), float32) – For two classes, one score per row (positive means classes_[1]); for more, one score per class.

Raises:

ValueError – If the model is not fitted, or X has a different number of features than the training data.

predict(X)[source]#

Predict class labels.

For two classes a row is classes_[1] when its decision score is positive and classes_[0] otherwise. For more classes it is the class with the highest score; a tie goes to the class that comes first in classes_.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, read as float32.

Returns:

labels (array of shape (n_samples,) or list) – An int64 array for integer labels, a float64 array for float labels, and a Python list for any other label type.

score(X, y)[source]#

Return the mean accuracy on the given data.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Test samples.

  • y (array-like of shape (n_samples,)) – True labels.

Returns:

score (float) – The fraction of rows where predict(X) == y.