mojolearn.QNRegressor#
- class mojolearn.QNRegressor(*, loss='squared_error', fit_intercept=True, l1_strength=0.0, l2_strength=0.0, max_iter=1000, tol=0.0001, delta=None, linesearch_max_iter=50, lbfgs_memory=5, warm_start=False, penalty_normalized=True)[source]#
Bases:
NumericModeMixinLinear regression on the squared or absolute loss, solved by a quasi-Newton method.
Minimizes
mean_i loss(y_i, x_i @ w + b) + l1_strength * ||w||_1 + (l2_strength / 2) * ||w||^2, where the loss is(z - y)**2 / 2('squared_error') or|z - y|('absolute_error'). Withpenalty_normalized=Trueboth strengths are divided by the number of samples. The solver is the L-BFGS / OWL-QN solver ofLogisticRegression: a nonzerol1_strengthselects OWL-QN. The intercept is a solver parameter and is not penalized. This is cuML’sQNsolver withloss='l2'orloss='l1'.Xis read as float32;ymust be one-dimensional. Out-of-range parameter values raiseValueError.- Parameters:
loss ({‘squared_error’, ‘absolute_error’}, default ‘squared_error’) – The data-fit loss; any other value raises
ValueError.fit_intercept (bool, default True) – Whether to fit an (unpenalized) intercept.
l1_strength (float, default 0.0) – Strength of the l1 penalty, non-negative.
l2_strength (float, default 0.0) – Strength of the l2 penalty, non-negative.
max_iter (int, default 1000) – Maximum number of solver iterations, a positive integer.
tol (float, default 1e-4) – Gradient tolerance, > 0.
delta (float or None, default None) – Tolerance on the change of the objective, non-negative; None means
tol * 0.01.linesearch_max_iter (int, default 50) – Maximum number of line-search steps per iteration, a positive integer.
lbfgs_memory (int, default 5) – Number of past updates kept by L-BFGS, a positive integer.
warm_start (bool, default False) – Only False is supported (the solver always starts from zero); True raises
NotImplementedError.penalty_normalized (bool, default True) – Whether to divide both penalty strengths by the number of samples.
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:
coef_ (array of shape (n_features,), float32) – The fitted coefficients.
intercept_ (float) – The fitted intercept; 0.0 when
fit_interceptis False.n_iter_ (array of shape (1,), int64) – The number of solver iterations run.
objective_ (float) – The final value of the minimized objective.
retcode_ (int) – The solver’s return code, as on
LogisticRegression(0 means converged).n_features_in_ (int) – The number of features seen in
fit.input_copied_ (bool) – Whether
fithad to copyXto 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 @ np.arange(8, dtype=np.float32) + 1.0 >>> model = mojolearn.QNRegressor(loss="absolute_error").fit(X, y) >>> pred = model.predict(X)
- __init__(*, loss='squared_error', fit_intercept=True, l1_strength=0.0, l2_strength=0.0, max_iter=1000, tol=0.0001, delta=None, linesearch_max_iter=50, lbfgs_memory=5, warm_start=False, penalty_normalized=True)[source]#
Methods
fit(X, y[, sample_weight])Fit the model.
get_params([deep])numeric_mode_used()The numeric mode this estimator runs in.
predict(X)Predict with the fitted model.
score(X, y)Return the coefficient of determination R^2 of the prediction.
set_params(**params)Set constructor parameters.
vendor_used()The GPU backend this estimator runs on.
- fit(X, y, sample_weight=None)[source]#
Fit the model.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data, read as float32.
y (array-like of shape (n_samples,)) – Target values. A 2-D
yor a length mismatch raisesValueError.sample_weight (None) – Must be None; weights raise
NotImplementedError.
- Returns:
self (QNRegressor) – The fitted estimator.
- predict(X)[source]#
Predict with the fitted model.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Samples, read as float32.
- Returns:
y_pred (array of shape (n_samples,), float32) –
X @ coef_ + intercept_.- Raises:
ValueError – If the model is not fitted, or
Xhas a different number of features than the training data.
- score(X, y)[source]#
Return the coefficient of determination R^2 of the prediction.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,)) – True target values.
- Returns:
score (float) –
1 - SS_res / SS_tot, computed in float64. A constantyscores 0.0.