mojolearn.SGDOneClassSVM#

class mojolearn.SGDOneClassSVM(nu=0.5, fit_intercept=True, max_iter=1000, tol=0.001, shuffle=True, verbose=0, random_state=None, learning_rate='optimal', eta0=0.0, power_t=0.5, warm_start=False, average=False, batch_size=None)[source]#

Bases: NumericModeMixin

Linear one-class SVM trained by stochastic gradient descent.

Follows scikit-learn’s SGDOneClassSVM for outlier detection. predict returns +1 for inliers and -1 for outliers. random_state=None means seed 0, so a fit is reproducible by default.

Parameters:
  • nu (float, default 0.5) – Upper bound on the fraction of training errors, in (0, 1].

  • fit_intercept (bool, default True) – Whether to fit the offset.

  • max_iter (int, default 1000) – Maximum number of passes over the data.

  • tol (float or None, default 1e-3) – Stopping tolerance, as in scikit-learn; None disables it.

  • shuffle (bool, default True) – Whether to shuffle the rows each pass.

  • verbose (int, default 0) – Accepted for compatibility; ignored.

  • random_state (int or None, default None) – Non-negative seed for shuffling. None means seed 0.

  • learning_rate ({‘constant’, ‘optimal’, ‘invscaling’, ‘adaptive’}, default ‘optimal’) – Step-size schedule.

  • eta0 (float, default 0.0) – Initial step size; must be > 0 for ‘constant’, ‘invscaling’ and ‘adaptive’.

  • power_t (float, default 0.5) – Exponent of the ‘invscaling’ schedule.

  • warm_start (bool, default False) – Not supported: True raises ValueError.

  • average (bool, default False) – Not supported: True raises ValueError.

  • batch_size (int or None, default None) – Rows per minibatch step. None chooses per numeric mode: minibatches of 4096 rows in 'identical' mode, per-sample updates otherwise. 0 asks for per-sample updates.

  • numeric_mode (str or None, default None) – Numeric mode for this model ('identical', 'deterministic' or 'fast', which runs on Apple GPUs only); None uses the process default (see mojolearn.set_numeric_mode()).

Variables:
  • coef_ (float32 array of shape (n_features,)) – Weights.

  • offset_ (float32 array of shape (1,)) – Offset; decision_function = score_samples - offset_.

  • n_iter_ (int) – Number of passes run.

  • t_ (float) – Number of weight updates, n_iter_ * n_samples + 1.

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

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> model = mojolearn.SGDOneClassSVM(nu=0.1).fit(X)
>>> labels = model.predict(X)
__init__(nu=0.5, fit_intercept=True, max_iter=1000, tol=0.001, shuffle=True, verbose=0, random_state=None, learning_rate='optimal', eta0=0.0, power_t=0.5, warm_start=False, average=False, batch_size=None)[source]#

Methods

decision_function(X)

Signed score of each sample: positive for inliers.

fit(X[, y, sample_weight])

Fit the model.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Label each sample as an inlier (+1) or an outlier (-1).

score_samples(X)

Raw score of each sample, X @ coef_.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

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

Fit the model.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, converted to float32; values must be finite.

  • y (ignored) – Present for API compatibility.

  • sample_weight (array-like of shape (n_samples,) or float, optional) – Non-negative weights with a positive sum.

Returns:

self (SGDOneClassSVM) – The fitted model.

Raises:

ValueError – If nu is not in (0, 1], learning_rate is not supported, average or warm_start is True, or the weights are invalid.

decision_function(X)[source]#

Signed score of each sample: positive for inliers.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, converted to float32.

Returns:

scores (float32 array of shape (n_samples,)) – X @ coef_ - offset_.

score_samples(X)[source]#

Raw score of each sample, X @ coef_.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, converted to float32.

Returns:

scores (float32 array of shape (n_samples,)) – Raw scores; higher is more normal.

predict(X)[source]#

Label each sample as an inlier (+1) or an outlier (-1).

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, converted to float32.

Returns:

labels (int64 array of shape (n_samples,)) – +1 where decision_function is >= 0, -1 elsewhere.