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)[source]#

Bases: _LinearRegressorMixin, NumericModeMixin

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=4096 rows per step (batch_size=0 gives scikit-learn’s per-sample updates), and random_state=None means 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', 'deterministic' or 'fast', which runs on Apple GPUs only); None uses the process default (see mojolearn.set_numeric_mode()).

Variables:
  • coef_ (float32 array of shape (n_features,)) – Weights.

  • intercept_ (float32 array of shape (1,)) – Intercept.

  • n_iter_ (int) – Number of passes run.

  • t_ (float) – Number of weight updates, n_iter_ * n_samples + 1.

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

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)
__init__(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)[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 targets for X.

score(X, y)

Coefficient of determination R^2 of predict(X) against y.

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, 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).