mojolearn.SGDRegressor#
- class mojolearn.SGDRegressor(loss='squared_error', *, penalty='l2', alpha=0.0001, l1_ratio=0.15, fit_intercept=True, max_iter=1000, tol=0.001, shuffle=True, epsilon=0.1, random_state=None, learning_rate='invscaling', eta0=0.01, power_t=0.25, early_stopping=False, n_iter_no_change=5, average=False, warm_start=False, batch_size=4096)#
Linear regression trained by stochastic gradient descent.
Follows scikit-learn’s SGDRegressor. Two defaults differ from scikit-learn and change results: the fit runs minibatch SGD with
batch_size=4096rows per step (batch_size=0gives scikit-learn’s per-sample updates), andrandom_state=Nonemeans seed 0, so a fit is reproducible by default.- Parameters:
loss ({‘squared_error’, ‘huber’, ‘epsilon_insensitive’, ‘squared_epsilon_insensitive’}, default ‘squared_error’) – Loss function.
penalty ({‘l2’, ‘l1’, ‘elasticnet’} or None, default ‘l2’) – Regularization term.
alpha (float, default 0.0001) – Regularization strength; must be > 0 with
learning_rate='optimal'.l1_ratio (float, default 0.15) – Elastic-net mixing (0 is L2, 1 is L1); used with
penalty='elasticnet'.fit_intercept (bool, default True) – Whether to fit an intercept.
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.
epsilon (float, default 0.1) – Epsilon of the huber and epsilon-insensitive losses.
random_state (int or None, default None) – Non-negative seed for shuffling. None means seed 0.
learning_rate ({‘constant’, ‘optimal’, ‘invscaling’, ‘adaptive’}, default ‘invscaling’) – Step-size schedule.
eta0 (float, default 0.01) – Initial step size; must be > 0 for ‘constant’, ‘invscaling’ and ‘adaptive’.
power_t (float, default 0.25) – Exponent of the ‘invscaling’ schedule.
early_stopping (bool, default False) – Not supported: True raises ValueError.
n_iter_no_change (int, default 5) – Passes without improvement before stopping.
average (bool, default False) – Not supported: True raises ValueError.
warm_start (bool, default False) – Not supported: True raises ValueError.
batch_size (int, default 4096) – Rows per minibatch step; 0 runs per-sample SGD.
numeric_mode (str or None, default None) – Numeric mode for this model (
'identical'or'fast', which runs on Apple GPUs only); None uses the process default (seemojolearn.set_numeric_mode()).
- Variables:
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) >>> model = mojolearn.SGDRegressor().fit(X, y) >>> y_pred = model.predict(X)
Methods
fit(X, y[, sample_weight])Fit the model.
get_params([deep])The numeric mode this estimator runs in.
predict(X)Predict targets for
X.score(X, y)Coefficient of determination R^2 of
predict(X)againsty.set_params(**params)Set constructor parameters.
The GPU backend this estimator runs on.
- fit(X, y, sample_weight=None)#
Fit the model.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data, converted to float32; values must be finite.
y (array-like of shape (n_samples,)) – Targets.
sample_weight (array-like of shape (n_samples,) or float, optional) – Non-negative weights with a positive sum.
- Returns:
self (SGDRegressor) – The fitted model.
- Raises:
ValueError – On an unsupported option, invalid weights, or a floating-point overflow during training (scale the inputs).
- numeric_mode_used()#
The numeric mode this estimator runs in.
- Returns:
str –
'identical'or'fast', read from the compiled binary the estimator uses, not from the argument it was given.
- predict(X)#
Predict targets for
X.Returns
X @ coef_ + intercept_; models with a log link (Poisson, Gamma and Tweedie withpower > 0) return its exponential.- Parameters:
X (array-like of shape (n_samples, n_features)) – Samples, converted to float32. The feature count must match the one seen in
fit.- Returns:
y_pred (float32 array of shape (n_samples,)) – Predicted values.
- Raises:
RuntimeError – If the model is not fitted.
ValueError – If
Xhas a different number of features than infit.
- score(X, y)#
Coefficient of determination R^2 of
predict(X)againsty.- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,)) – True targets.
- Returns:
score (float) – R^2; 1.0 is a perfect fit.
- set_params(**params)#
Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.
- vendor_used()#
The GPU backend this estimator runs on.
- Returns:
str –
'metal','cuda'or'hip', read from the compiled binary the estimator uses.