mojolearn.SVR#
- class mojolearn.SVR(*, kernel='rbf', degree=3, gamma='auto', coef0=0.0, tol=0.001, C=1.0, epsilon=0.1, cache_size=1024.0, verbose=False, max_iter=-1, nochange_steps=1000, output_type=None)[source]#
Bases:
NumericModeMixinEpsilon-support vector regression.
Follows scikit-learn’s
SVR, with cuML’sSVRas the reference implementation: an SMO solver with the kernel matrices computed on the GPU. Rows whose prediction falls withinepsilonof the target cost nothing and never become support vectors, so raisingepsiloncan only remove rows from the support set.Xis converted to float32 (sparse input is densified); non-finite values inXoryraise an error naming the first bad cell. Supports the'identical'(default) and'fast'numeric modes;'fast'is available on Apple GPUs only.The parameters keep scikit-learn’s order and meaning, with two defaults that differ:
gamma='auto'(scikit-learn uses ‘scale’) andcache_size=1024.0, which here is the prediction buffer in MiB and does not affect training (scikit-learn’s 200 is a training cache). There is noshrinkingparameter; passing it raisesTypeError.nochange_stepsis cuML’s. Every parameter is keyword-only. A parameter that would be ignored is refused:degreewith any kernel but ‘poly’,coef0with any kernel but ‘poly’ or ‘sigmoid’, and a truthyverboseor anyoutput_typeraiseNotImplementedError.- Parameters:
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. 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.
C (float, default 1.0) – Regularization penalty. Must be finite and positive.
epsilon (float, default 0.1) – Half-width of the epsilon-insensitive tube. Must be finite and >= 0; an invalid value is refused when
fitruns.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. Must be positive and finite.
verbose (bool, default False) – Only False is accepted; this estimator prints no log lines.
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.output_type (None, default None) – Only None is accepted; results are mojolearn Arrays.
numeric_mode ({‘identical’, ‘fast’} or None, default None) – Numeric mode for this estimator; 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), float32) – The support vectors (rows of the kernel matrix with
kernel='precomputed').dual_coef_ (Array of shape (1, n_SV), float32) –
alpha_i - alpha*_ifor each support vector, in scikit-learn’s layout.intercept_ (Array of shape (1,), float32) – Intercept of the decision function.
coef_ (Array of shape (1, n_features), float32) – Primal weights
dual_coef_ @ support_vectors_, only forkernel='linear'; raisesAttributeErrorotherwise, as scikit-learn does.n_support_ (int) – Number of support vectors (cuML’s scalar form; scikit-learn’s is an array).
n_iter_ (int) – Total inner SMO iterations.
n_features_in_ (int) – Number of features seen during
fit.
Examples
>>> import numpy as np, mojolearn >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = X.sum(axis=1) >>> model = mojolearn.SVR(C=1.0, epsilon=0.1).fit(X, y) >>> pred = model.predict(X[:5])
- __init__(*, kernel='rbf', degree=3, gamma='auto', coef0=0.0, tol=0.001, C=1.0, epsilon=0.1, cache_size=1024.0, verbose=False, max_iter=-1, nochange_steps=1000, output_type=None)[source]#
Methods
fit(X, y[, sample_weight])Fit the support vector regressor.
get_params([deep])load(path)Load a model written by
save.numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Predict target values for the rows of
X.save(path)Save the fitted model to
pathas an.npzfile.score(X, y)Coefficient of determination R^2 of
predict(X)againsty.set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
Attributes
Primal weights, shape (1, n_features), float32.
- fit(X, y, sample_weight=None)[source]#
Fit the support vector regressor.
- 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,)) – Finite target values.
sample_weight (array-like of shape (n_samples,), default None) – Per-sample weights; row i’s penalty becomes
C * w_i. None weights every sample equally.
- Returns:
self (SVR) – The fitted estimator.
- property coef_#
Primal weights, shape (1, n_features), float32.
Only for
kernel='linear'; raisesAttributeErrorfor other kernels or beforefit.
- predict(X)[source]#
Predict target values 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 of shape (n_rows,), float32 –
sum_j K(x, sv_j) dual_j + bfor each row.
- score(X, y)[source]#
Coefficient of determination R^2 of
predict(X)againsty.scikit-learn’s definition,
1 - SS_res / SS_tot, accumulated in float64. A constantyscores 1.0 when predicted exactly and 0.0 otherwise.- Parameters:
X (array-like of shape (n_rows, n_features)) – Query data.
y (array-like of shape (n_rows,)) – True target values.
- Returns:
float – The R^2 score.
- 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
SVR.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
SVR.save.- Returns:
SVR – A fitted model, ready to predict, with the saved numeric mode.
- Raises:
ValueError – If the file is not a valid saved
SVR.