mojolearn.SGDClassifier#

class mojolearn.SGDClassifier(loss='hinge', *, 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='optimal', eta0=0.0, power_t=0.5, early_stopping=False, n_iter_no_change=5, class_weight=None, average=False, warm_start=False, batch_size=4096)[source]#

Bases: _LinearClassifierMixin, NumericModeMixin

Linear classifier trained by stochastic gradient descent.

Follows scikit-learn’s SGDClassifier: a binary linear model, one-vs-rest for more than two classes. 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 ({‘hinge’, ‘log_loss’, ‘modified_huber’, ‘squared_hinge’, ‘perceptron’}, default ‘hinge’) – 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 'modified_huber' loss.

  • random_state (int or None, default None) – Non-negative seed for shuffling. None means seed 0.

  • learning_rate ({‘constant’, ‘optimal’, ‘invscaling’, ‘adaptive’}, default ‘optimal’) – Step-size schedule.

  • eta0 (float, default 0.0) – Initial step size; must be > 0 for ‘constant’, ‘invscaling’ and ‘adaptive’.

  • power_t (float, default 0.5) – 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.

  • class_weight (dict, ‘balanced’ or None, default None) – Per-class weights; ‘balanced’ weighs classes inversely to their frequency.

  • 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:
  • classes_ (array of shape (n_classes,)) – Class labels.

  • coef_ (float32 array of shape (1, n_features) or (n_classes, n_features)) – Weights; one row for a binary problem.

  • intercept_ (float32 array of shape (1,) or (n_classes,)) – Intercepts.

  • 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[:, 0] > 0.5).astype(np.int32)
>>> model = mojolearn.SGDClassifier(loss="log_loss").fit(X, y)
>>> proba = model.predict_proba(X)
__init__(loss='hinge', *, 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='optimal', eta0=0.0, power_t=0.5, early_stopping=False, n_iter_no_change=5, class_weight=None, average=False, warm_start=False, batch_size=4096)[source]#

Methods

decision_function(X)

Signed distance of each sample to the separating hyperplane(s).

fit(X, y[, sample_weight])

Fit the model.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict class labels.

predict_proba(X)

Class probabilities, available with loss='log_loss' only.

score(X, y)

Mean accuracy 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,)) – Class labels; at least two classes.

  • sample_weight (array-like of shape (n_samples,) or float, optional) – Non-negative weights with a positive sum.

Returns:

self (SGDClassifier) – The fitted model.

Raises:

ValueError – On an unsupported option, a single class, invalid weights, or a floating-point overflow during training (scale the inputs).

predict_proba(X)[source]#

Class probabilities, available with loss='log_loss' only.

The sigmoid of each one-vs-rest score, normalized over the classes when there are more than two (as scikit-learn does).

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples, converted to float32.

Returns:

proba (float32 array of shape (n_samples, n_classes)) – Probability of each class, in the order of classes_.

Raises:

AttributeError – If loss is not 'log_loss'.