Class: SGDRegressor

Linear model fitted by minimizing a regularized empirical loss with SGD.

SGD stands for Stochastic Gradient Descent: the gradient of the loss is estimated each sample at a time and the model is updated along the way with a decreasing strength schedule (aka learning rate).

The regularizer is a penalty added to the loss function that shrinks model parameters towards the zero vector using either the squared euclidean norm L2 or the absolute norm L1 or a combination of both (Elastic Net). If the parameter update crosses the 0.0 value because of the regularizer, the update is truncated to 0.0 to allow for learning sparse models and achieve online feature selection.

This implementation works with data represented as dense numpy arrays of floating point values for the features.

Read more in the User Guide.

Python Reference

Constructors

new SGDRegressor()

new SGDRegressor(opts?): SGDRegressor

Parameters

ParameterTypeDescription
opts?object-
opts.alpha?numberConstant that multiplies the regularization term. The higher the value, the stronger the regularization. Also used to compute the learning rate when learning_rate is set to ‘optimal’. Values must be in the range \[0.0, inf).
opts.average?number | booleanWhen set to true, computes the averaged SGD weights across all updates and stores the result in the coef_ attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So average=10 will begin averaging after seeing 10 samples.
opts.early_stopping?booleanWhether to use early stopping to terminate training when validation score is not improving. If set to true, it will automatically set aside a fraction of training data as validation and terminate training when validation score returned by the score method is not improving by at least tol for n_iter_no_change consecutive epochs.
opts.epsilon?numberEpsilon in the epsilon-insensitive loss functions; only if loss is ‘huber’, ‘epsilon_insensitive’, or ‘squared_epsilon_insensitive’. For ‘huber’, determines the threshold at which it becomes less important to get the prediction exactly right. For epsilon-insensitive, any differences between the current prediction and the correct label are ignored if they are less than this threshold. Values must be in the range \[0.0, inf).
opts.eta0?numberThe initial learning rate for the ‘constant’, ‘invscaling’ or ‘adaptive’ schedules. The default value is 0.01. Values must be in the range \[0.0, inf).
opts.fit_intercept?booleanWhether the intercept should be estimated or not. If false, the data is assumed to be already centered.
opts.l1_ratio?numberThe Elastic Net mixing parameter, with 0 <= l1_ratio <= 1. l1_ratio=0 corresponds to L2 penalty, l1_ratio=1 to L1. Only used if penalty is ‘elasticnet’. Values must be in the range \[0.0, 1.0\].
opts.learning_rate?stringThe learning rate schedule:
opts.loss?stringThe loss function to be used. The possible values are ‘squared_error’, ‘huber’, ‘epsilon_insensitive’, or ‘squared_epsilon_insensitive’ The ‘squared_error’ refers to the ordinary least squares fit. ‘huber’ modifies ‘squared_error’ to focus less on getting outliers correct by switching from squared to linear loss past a distance of epsilon. ‘epsilon_insensitive’ ignores errors less than epsilon and is linear past that; this is the loss function used in SVR. ‘squared_epsilon_insensitive’ is the same but becomes squared loss past a tolerance of epsilon. More details about the losses formulas can be found in the User Guide.
opts.max_iter?numberThe maximum number of passes over the training data (aka epochs). It only impacts the behavior in the fit method, and not the partial_fit method. Values must be in the range \[1, inf).
opts.n_iter_no_change?numberNumber of iterations with no improvement to wait before stopping fitting. Convergence is checked against the training loss or the validation loss depending on the early_stopping parameter. Integer values must be in the range \[1, max_iter).
opts.penalty?"l1" | "l2" | "elasticnet"The penalty (aka regularization term) to be used. Defaults to ‘l2’ which is the standard regularizer for linear SVM models. ‘l1’ and ‘elasticnet’ might bring sparsity to the model (feature selection) not achievable with ‘l2’. No penalty is added when set to undefined.
opts.power_t?numberThe exponent for inverse scaling learning rate. Values must be in the range (-inf, inf).
opts.random_state?numberUsed for shuffling the data, when shuffle is set to true. Pass an int for reproducible output across multiple function calls. See Glossary.
opts.shuffle?booleanWhether or not the training data should be shuffled after each epoch.
opts.tol?numberThe stopping criterion. If it is not undefined, training will stop when (loss > best_loss - tol) for n_iter_no_change consecutive epochs. Convergence is checked against the training loss or the validation loss depending on the early_stopping parameter. Values must be in the range \[0.0, inf).
opts.validation_fraction?numberThe proportion of training data to set aside as validation set for early stopping. Must be between 0 and 1. Only used if early_stopping is true. Values must be in the range (0.0, 1.0).
opts.verbose?numberThe verbosity level. Values must be in the range \[0, inf).
opts.warm_start?booleanWhen set to true, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. See the Glossary. Repeatedly calling fit or partial_fit when warm_start is true can result in a different solution than when calling fit a single time because of the way the data is shuffled. If a dynamic learning rate is used, the learning rate is adapted depending on the number of samples already seen. Calling fit resets this counter, while partial_fit will result in increasing the existing counter.

Returns SGDRegressor

Defined in generated/linear_model/SGDRegressor.ts:29

Properties

PropertyTypeDefault valueDefined in
_isDisposedbooleanfalsegenerated/linear_model/SGDRegressor.ts:27
_isInitializedbooleanfalsegenerated/linear_model/SGDRegressor.ts:26
_pyPythonBridgeundefinedgenerated/linear_model/SGDRegressor.ts:25
idstringundefinedgenerated/linear_model/SGDRegressor.ts:22
optsanyundefinedgenerated/linear_model/SGDRegressor.ts:23

Accessors

coef_

Get Signature

get coef_(): Promise<ArrayLike>

Weights assigned to the features.

Returns Promise<ArrayLike>

Defined in generated/linear_model/SGDRegressor.ts:628


feature_names_in_

Get Signature

get feature_names_in_(): Promise<ArrayLike>

Names of features seen during fit. Defined only when X has feature names that are all strings.

Returns Promise<ArrayLike>

Defined in generated/linear_model/SGDRegressor.ts:747


intercept_

Get Signature

get intercept_(): Promise<ArrayLike>

The intercept term.

Returns Promise<ArrayLike>

Defined in generated/linear_model/SGDRegressor.ts:651


n_features_in_

Get Signature

get n_features_in_(): Promise<number>

Number of features seen during fit.

Returns Promise<number>

Defined in generated/linear_model/SGDRegressor.ts:722


n_iter_

Get Signature

get n_iter_(): Promise<number>

The actual number of iterations before reaching the stopping criterion.

Returns Promise<number>

Defined in generated/linear_model/SGDRegressor.ts:676


py

Get Signature

get py(): PythonBridge

Returns PythonBridge

Set Signature

set py(pythonBridge): void

Parameters

ParameterType
pythonBridgePythonBridge

Returns void

Defined in generated/linear_model/SGDRegressor.ts:171


t_

Get Signature

get t_(): Promise<number>

Number of weight updates performed during training. Same as (n_iter_ \* n_samples + 1).

Returns Promise<number>

Defined in generated/linear_model/SGDRegressor.ts:699

Methods

densify()

densify(opts): Promise<any>

Convert coefficient matrix to dense array format.

Converts the coef_ member (back) to a numpy.ndarray. This is the default format of coef_ and is required for fitting, so calling this method is only required on models that have previously been sparsified; otherwise, it is a no-op.

Parameters

ParameterType
optsobject

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:242


dispose()

dispose(): Promise<void>

Disposes of the underlying Python resources.

Once dispose() is called, the instance is no longer usable.

Returns Promise<void>

Defined in generated/linear_model/SGDRegressor.ts:223


fit()

fit(opts): Promise<any>

Fit linear model with Stochastic Gradient Descent.

Parameters

ParameterTypeDescription
optsobject-
opts.coef_init?ArrayLikeThe initial coefficients to warm-start the optimization.
opts.intercept_init?ArrayLikeThe initial intercept to warm-start the optimization.
opts.sample_weight?ArrayLikeWeights applied to individual samples (1. for unweighted).
opts.X?anyTraining data.
opts.y?ArrayLikeTarget values.

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:268


get_metadata_routing()

get_metadata_routing(opts): Promise<any>

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Parameters

ParameterTypeDescription
optsobject-
opts.routing?anyA MetadataRequest encapsulating routing information.

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:322


init()

init(py): Promise<void>

Initializes the underlying Python resources.

This instance is not usable until the Promise returned by init() resolves.

Parameters

ParameterType
pyPythonBridge

Returns Promise<void>

Defined in generated/linear_model/SGDRegressor.ts:184


partial_fit()

partial_fit(opts): Promise<any>

Perform one epoch of stochastic gradient descent on given samples.

Internally, this method uses max_iter \= 1. Therefore, it is not guaranteed that a minimum of the cost function is reached after calling it once. Matters such as objective convergence and early stopping should be handled by the user.

Parameters

ParameterTypeDescription
optsobject-
opts.sample_weight?ArrayLikeWeights applied to individual samples. If not provided, uniform weights are assumed.
opts.X?anySubset of training data.
opts.y?any[]Subset of target values.

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:358


predict()

predict(opts): Promise<any>

Predict using the linear model.

Parameters

ParameterTypeDescription
optsobject-
opts.X?anyInput data.

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:400


score()

score(opts): Promise<number>

Return the coefficient of determination of the prediction.

The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true \- y_pred)\*\* 2).sum() and \(v\) is the total sum of squares ((y_true \- y_true.mean()) \*\* 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.

Parameters

ParameterTypeDescription
optsobject-
opts.sample_weight?ArrayLikeSample weights.
opts.X?ArrayLike[]Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
opts.y?ArrayLikeTrue values for X.

Returns Promise<number>

Defined in generated/linear_model/SGDRegressor.ts:434


set_fit_request()

set_fit_request(opts): Promise<any>

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

Parameters

ParameterTypeDescription
optsobject-
opts.coef_init?string | booleanMetadata routing for coef_init parameter in fit.
opts.intercept_init?string | booleanMetadata routing for intercept_init parameter in fit.
opts.sample_weight?string | booleanMetadata routing for sample_weight parameter in fit.

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:480


set_partial_fit_request()

set_partial_fit_request(opts): Promise<any>

Request metadata passed to the partial_fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

Parameters

ParameterTypeDescription
optsobject-
opts.sample_weight?string | booleanMetadata routing for sample_weight parameter in partial_fit.

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:526


set_score_request()

set_score_request(opts): Promise<any>

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

Parameters

ParameterTypeDescription
optsobject-
opts.sample_weight?string | booleanMetadata routing for sample_weight parameter in score.

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:564


sparsify()

sparsify(opts): Promise<any>

Convert coefficient matrix to sparse format.

Converts the coef_ member to a scipy.sparse matrix, which for L1-regularized models can be much more memory- and storage-efficient than the usual numpy.ndarray representation.

The intercept_ member is not converted.

Parameters

ParameterType
optsobject

Returns Promise<any>

Defined in generated/linear_model/SGDRegressor.ts:602