mojolearn.SVC#
- class mojolearn.SVC(*, C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, tol=0.001, cache_size=1024.0, max_iter=-1, nochange_steps=1000, verbose=False, output_type=None, random_state=None, class_weight=None, decision_function_shape='ovo', probability=False, break_ties=False)[source]#
Bases:
NumericModeMixinC-support vector classification.
Follows scikit-learn’s
SVC, with cuML’sSVCas the reference implementation: an SMO solver with the kernel matrices computed on the GPU. Two classes train one machine; more train one machine per class pair (one-vs-one, libsvm’s scheme) and predict by vote, ties going to the lowest class.Xis converted to float32 (sparse input is densified), and non-finite values inXraise an error naming the first bad cell. Supports the'identical'(default) and'fast'numeric modes;'fast'is available on Apple GPUs only.Defaults that differ from scikit-learn:
gamma='auto'(scikit-learn uses ‘scale’);decision_function_shape='ovo'(scikit-learn uses ‘ovr’);cache_sizeis the prediction buffer in MiB and does not affect training. There is noshrinkingparameter. Every parameter is keyword-only. A parameter that would be ignored is refused rather than accepted:degreewith any kernel but ‘poly’,coef0with any kernel but ‘poly’ or ‘sigmoid’,random_statewithoutprobability=True, and a truthyverboseor anyoutput_typeall raiseNotImplementedError.- Parameters:
C (float, default 1.0) – Regularization penalty. Must be finite and positive.
kernel ({‘linear’, ‘rbf’, ‘poly’, ‘sigmoid’, ‘precomputed’}, default ‘rbf’) – The kernel. ‘poly’ is
(gamma * <x, y> + coef0) ** degree; ‘sigmoid’ istanh(gamma * <x, y> + coef0). With ‘precomputed’,Xis the n x n kernel matrix at fit and the q x n kernel against the training rows at predict;support_vectors_then holds the support rows of the kernel matrix (scikit-learn’s is empty). Other names, including ‘polynomial’ and ‘tanh’, raise.degree (int, default 3) – Degree of the ‘poly’ kernel, an integer in [0, 32]. Must stay 3 with any other kernel.
gamma (float, ‘auto’ or ‘scale’, default ‘auto’) – Kernel coefficient, a finite float >= 0. ‘auto’ is
1 / n_features; ‘scale’ is1 / (n_features * X.var()), computed from the exact variance of the float32 data, so it can differ from scikit-learn’s float32X.var()in the last bits.coef0 (float, default 0.0) – Independent term of the ‘poly’ and ‘sigmoid’ kernels, finite. Must stay 0.0 with any other kernel.
tol (float, default 1e-3) – Stopping tolerance. Must be finite and positive.
cache_size (float, default 1024.0) – Size in MiB of the kernel tile buffer used at prediction, which sets the prediction batch size; results do not depend on it. It has no training-time kernel cache role here. Must be positive and finite.
max_iter (int, default -1) – Cap on the total inner SMO iterations (cuML’s meaning); -1 means no limit. 0 and values below -1 are refused.
nochange_steps (int, default 1000) – Follows cuML’s
nochange_stepsconvergence rule. Must be >= 0.verbose (bool, default False) – Only False is accepted; this estimator prints no log lines.
output_type (None, default None) – Only None is accepted; results are mojolearn Arrays.
random_state (int or None, default None) – With
probability=True, the seed of the cross-validation fold shuffle (None means 0); an int in [0, 2**64). Refused otherwise.class_weight (dict, ‘balanced’ or None, default None) – Per-class weights; row i’s penalty becomes
C * class_weight[y_i] * sample_weight_i.decision_function_shape ({‘ovo’, ‘ovr’}, default ‘ovo’) – With more than two classes,
decision_functionreturns the pairwise (n, K(K-1)/2) form (‘ovo’) or scikit-learn’s voted (n, K) form (‘ovr’). No effect with two classes.probability (bool, default False) – Fit Platt scaling (libsvm’s method: a seeded 5-fold cross-validation per class pair, then a sigmoid fit and, for more than two classes, pairwise coupling) so that
predict_probaandpredict_log_probaare available. The fold shuffle differs from libsvm’s, so the probabilities are not bit-equal to libsvm’s.break_ties (bool, default False) – With
decision_function_shape='ovr'and more than two classes, predict the argmax of the ‘ovr’ decision function instead of the vote. Must be False with ‘ovo’ (scikit-learn’s rule).numeric_mode ({‘identical’, ‘fast’} or None, default None) – Numeric mode for this estimator; None uses the process default.
- Variables:
classes_ (list of shape (K,)) – The distinct labels, sorted ascending, as Python scalars.
classes_[1]is the positive class of the binary decision function.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), float32) – The support vectors.
dual_coef_ (Array of shape (1, n_SV) or (K - 1, n_SV), float32) – Dual coefficients, in scikit-learn’s layout.
intercept_ (Array of shape (1,) or (K(K-1)/2,), float32) – Intercepts of the decision functions.
coef_ (Array of shape (1, n_features) or (K(K-1)/2, n_features), float32) – Primal weights
dual_coef_ @ support_vectors_, only forkernel='linear'; raisesAttributeErrorotherwise, as scikit-learn does.n_support_ (int, or Array of shape (K,), int32) – Number of support vectors. With two classes this is a single count (cuML’s form; scikit-learn returns a per-class array); with more, the per-class counts.
n_iter_ (int, or Array of shape (K(K-1)/2,), int32) – Total inner SMO iterations, per class pair with more than two classes.
probB_ (probA_,) – Platt sigmoid parameters per class pair, when
probability=True.n_features_in_ (int) – Number of features seen during
fit.
Notes
With more than two classes the fitted arrays follow scikit-learn’s layout and orientation: a pair’s decision is positive toward its lower class,
support_is grouped by class (ascending within a class),dual_coef_is (K - 1, n_SV) andintercept_is (K(K-1)/2,).predictreturns an int64 or float64 Array for integer or float labels, and a Python list for other label types.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) >>> model = mojolearn.SVC(C=1.0, kernel="rbf").fit(X, y) >>> labels = model.predict(X[:5])
- __init__(*, C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, tol=0.001, cache_size=1024.0, max_iter=-1, nochange_steps=1000, verbose=False, output_type=None, random_state=None, class_weight=None, decision_function_shape='ovo', probability=False, break_ties=False)[source]#
Methods
Evaluate the decision function for the rows of
X.fit(X, y[, sample_weight])Fit the support vector classifier.
get_params([deep])load(path)Load a model written by
save.numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Predict class labels for the rows of
X.Natural log of
predict_proba(X).Class probabilities from the fitted Platt scaling.
save(path)Save the fitted model to
pathas an.npzfile.score(X, y)Mean accuracy of
predict(X)againsty.set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
Attributes
Primal weights, only for
kernel='linear'.- fit(X, y, sample_weight=None)[source]#
Fit the support vector classifier.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data, converted to float32. With
kernel='precomputed', the square n x n kernel matrix.y (array-like of shape (n_samples,)) – Class labels; two or more distinct values.
sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights; row i’s penalty becomes
C * w_i(times its class weight). None weights every sample equally.
- Returns:
self (SVC) – The fitted estimator.
- property coef_#
Primal weights, only for
kernel='linear'.Shape (1, n_features) for two classes and (K(K-1)/2, n_features) for K > 2 classes, float32. Raises
AttributeErrorfor other kernels or beforefit.
- decision_function(X)[source]#
Evaluate the decision function for the rows of
X.- Parameters:
X (array-like of shape (n_rows, n_features)) – Query data. With
kernel='precomputed', the kernel between the queries and the training rows, shape (n_rows, n_samples).- Returns:
Array – Two classes: shape (n_rows,), the raw
sum_j K(x, sv_j) dual_j + b, positive towardclasses_[1]. More classes withdecision_function_shape='ovo': shape (n_rows, K(K-1)/2), float32, one column per class pair, positive toward the pair’s lower class. With ‘ovr’: shape (n_rows, K), float64, scikit-learn’s vote-plus-confidence transform of the pairwise values.
- predict(X)[source]#
Predict class labels for the rows of
X.Two classes:
classes_[1]where the decision function is >= 0, otherwiseclasses_[0]. More classes: one-vs-one vote, the most votes winning and ties going to the lowest class; withbreak_ties=True(anddecision_function_shape='ovr'), the argmax of the ‘ovr’ decision function, the first maximum winning.- Parameters:
X (array-like of shape (n_rows, n_features)) – Query data (the cross-kernel with
kernel='precomputed').- Returns:
Array or list of shape (n_rows,) – Labels from
classes_: an int64 or float64 Array for integer or float labels, a Python list otherwise.
- predict_proba(X)[source]#
Class probabilities from the fitted Platt scaling.
Follows libsvm: each class pair’s decision value goes through its fitted sigmoid (clamped to [1e-7, 1 - 1e-7]); with more than two classes the pairwise probabilities are then coupled.
- Parameters:
X (array-like of shape (n_rows, n_features)) – Query data.
- Returns:
Array of shape (n_rows, K), float64 – Probabilities, columns in
classes_order.- Raises:
AttributeError – If the model was fitted with
probability=False.
- predict_log_proba(X)[source]#
Natural log of
predict_proba(X).- Parameters:
X (array-like of shape (n_rows, n_features)) – Query data.
- Returns:
Array of shape (n_rows, K), float64 – Log probabilities, columns in
classes_order.- Raises:
AttributeError – If the model was fitted with
probability=False.
- score(X, y)[source]#
Mean accuracy of
predict(X)againsty.- Parameters:
X (array-like of shape (n_rows, n_features)) – Query data.
y (array-like of shape (n_rows,)) – True labels.
- Returns:
float – Fraction of rows predicted correctly.
- save(path)[source]#
Save the fitted model to
pathas an.npzfile.The file holds the fitted arrays, the hyperparameters, the gamma resolved at fit and the numeric mode. Load it with
SVC.load, or predict from it on a CPU without a GPU withmojolearn.host_model(path).- Parameters:
path (str or path-like) – Destination file.
- Raises:
RuntimeError – If the model has not been fitted.
- classmethod load(path)[source]#
Load a model written by
save.Every array is read at its saved dtype, never cast.
- Parameters:
path (str or path-like) – File written by
SVC.save.- Returns:
SVC – A fitted model, ready to predict, with the saved numeric mode.
- Raises:
ValueError – If the file is not a valid saved
SVC.