mojolearn.OneClassSVM#

class mojolearn.OneClassSVM(*, kernel='rbf', degree=3, gamma='scale', coef0=0.0, tol=0.001, nu=0.5, shrinking=True, cache_size=200, verbose=False, max_iter=-1)[source]#

Bases: _XNeighbors

Unsupervised outlier detection with the nu one-class SVM.

Learns a boundary around the training data with libsvm’s SMO solver for the one-class problem, following scikit-learn’s OneClassSVM. Computation is float32 (libsvm uses float64). No shrinking heuristic is used and the whole kernel matrix is formed, so shrinking and cache_size change nothing.

Parameters:
  • kernel ({‘rbf’, ‘linear’, ‘poly’, ‘sigmoid’, ‘precomputed’}, default ‘rbf’) – Kernel. With 'precomputed', fit takes the n x n Gram matrix and later calls take the n_test x n_train kernel against the training rows. Callable kernels and other names raise NotImplementedError.

  • degree (int, default 3) – Degree of the 'poly' kernel.

  • gamma ({‘scale’, ‘auto’} or float, default ‘scale’) – Kernel coefficient. 'scale' is 1 / (n_features * X.var()) (1.0 when the variance is zero); 'auto' is 1 / n_features.

  • coef0 (float, default 0.0) – Independent term of the 'poly' and 'sigmoid' kernels.

  • tol (float, default 1e-3) – Stopping tolerance of the solver.

  • nu (float, default 0.5) – Upper bound on the fraction of training errors and lower bound on the fraction of support vectors, in (0, 1].

  • shrinking (bool, default True) – Accepted for scikit-learn compatibility; unused.

  • cache_size (float, default 200) – Accepted for scikit-learn compatibility; unused.

  • verbose (bool, default False) – Accepted for scikit-learn compatibility; unused.

  • max_iter (int, default -1) – Iteration limit of the solver; -1 means 10,000,000.

  • numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this estimator ('fast' is Apple only). None uses the process default.

Variables:
  • support_ (Array of shape (n_SV,), int32) – Indices of the support vectors in the training data.

  • support_vectors_ (Array of shape (n_SV, n_features)) – Support vectors (shape (n_SV, 0) for a precomputed kernel).

  • dual_coef_ (Array of shape (1, n_SV)) – Coefficients of the support vectors.

  • intercept_ (Array of shape (1,)) – Constant in the decision function.

  • offset_ (float) – Offset subtracted from score_samples to give decision_function.

  • n_iter_ (int) – Solver iterations run.

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

  • fit_status_ (int) – 0 when fitted.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> model = mojolearn.OneClassSVM(nu=0.1).fit(X)
>>> labels = model.predict(X)
__init__(*, kernel='rbf', degree=3, gamma='scale', coef0=0.0, tol=0.001, nu=0.5, shrinking=True, cache_size=200, verbose=False, max_iter=-1)[source]#

Methods

decision_function(X)

Signed distance to the boundary: score_samples(X) - offset_.

fit(X[, y, sample_weight])

Fit the one-class SVM.

fit_predict(X[, y])

Fit on X and label X: fit(X).predict(X).

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Label samples as inliers (1) or outliers (-1).

score_samples(X)

Raw scoring function of the samples (not shifted by offset_).

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 one-class SVM.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Training data, or the (n_samples, n_samples) Gram matrix for kernel='precomputed'. Sparse input is densified.

  • y (None) – Ignored.

  • sample_weight (array-like of shape (n_samples,) or None, default None) – Per-sample weights that scale each sample’s bound, as libsvm does. Samples of weight 0 are dropped; negative weights raise ValueError, and so do all-zero weights.

Returns:

self (OneClassSVM) – The fitted estimator.

score_samples(X)[source]#

Raw scoring function of the samples (not shifted by offset_).

Parameters:

X (array-like of shape (n_queries, n_features)) – Samples, or the (n_queries, n_train) kernel for kernel='precomputed'.

Returns:

scores (Array of shape (n_queries,), float32)

decision_function(X)[source]#

Signed distance to the boundary: score_samples(X) - offset_.

Positive for inliers, negative for outliers.

Parameters:

X (array-like of shape (n_queries, n_features)) – Samples (or kernel rows for kernel='precomputed').

Returns:

scores (Array of shape (n_queries,), float32)

predict(X)[source]#

Label samples as inliers (1) or outliers (-1).

Parameters:

X (array-like of shape (n_queries, n_features)) – Samples (or kernel rows for kernel='precomputed').

Returns:

labels (Array of shape (n_queries,), int64)

fit_predict(X, y=None)[source]#

Fit on X and label X: fit(X).predict(X).

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

  • y (None) – Ignored.

Returns:

labels (Array of shape (n_samples,), int64) – 1 for inliers, -1 for outliers.