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 (opens in a new tab)
Constructors
constructor()
Signature
new SGDRegressor(opts?: object): SGDRegressor;
Parameters
Name | Type | Description |
---|---|---|
opts? | object | - |
opts.alpha? | number | Constant that multiplies the regularization term. The higher the value, the stronger the regularization. Also used to compute the learning rate when set to learning\_rate is set to ‘optimal’. Default Value 0.0001 |
opts.average? | number | boolean | When 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. Default Value false |
opts.early_stopping? | boolean | Whether 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. Default Value false |
opts.epsilon? | number | Epsilon 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. Default Value 0.1 |
opts.eta0? | number | The initial learning rate for the ‘constant’, ‘invscaling’ or ‘adaptive’ schedules. The default value is 0.01. Default Value 0.01 |
opts.fit_intercept? | boolean | Whether the intercept should be estimated or not. If false , the data is assumed to be already centered. Default Value true |
opts.l1_ratio? | number | The 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’. Default Value 0.15 |
opts.learning_rate? | string | The learning rate schedule: Default Value 'invscaling' |
opts.loss? | string | The 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. Default Value 'squared_error' |
opts.max_iter? | number | The 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. Default Value 1000 |
opts.n_iter_no_change? | number | Number 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. Default Value 5 |
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 . Default Value 'l2' |
opts.power_t? | number | The exponent for inverse scaling learning rate. Default Value 0.25 |
opts.random_state? | number | Used for shuffling the data, when shuffle is set to true . Pass an int for reproducible output across multiple function calls. See Glossary. |
opts.shuffle? | boolean | Whether or not the training data should be shuffled after each epoch. Default Value true |
opts.tol? | number | The 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. Default Value 0.001 |
opts.validation_fraction? | number | The 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 . Default Value 0.1 |
opts.verbose? | number | The verbosity level. Default Value 0 |
opts.warm_start? | boolean | When 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. Default Value false |
Returns
Defined in: generated/linear_model/SGDRegressor.ts:29 (opens in a new tab)
Properties
_isDisposed
boolean
=false
Defined in: generated/linear_model/SGDRegressor.ts:27 (opens in a new tab)
_isInitialized
boolean
=false
Defined in: generated/linear_model/SGDRegressor.ts:26 (opens in a new tab)
_py
PythonBridge
Defined in: generated/linear_model/SGDRegressor.ts:25 (opens in a new tab)
id
string
Defined in: generated/linear_model/SGDRegressor.ts:22 (opens in a new tab)
opts
any
Defined in: generated/linear_model/SGDRegressor.ts:23 (opens in a new tab)
Accessors
coef_
Weights assigned to the features.
Signature
coef_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/SGDRegressor.ts:509 (opens in a new tab)
feature_names_in_
Names of features seen during fit. Defined only when X
has feature names that are all strings.
Signature
feature_names_in_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/SGDRegressor.ts:628 (opens in a new tab)
intercept_
The intercept term.
Signature
intercept_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/linear_model/SGDRegressor.ts:532 (opens in a new tab)
n_features_in_
Number of features seen during fit.
Signature
n_features_in_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/linear_model/SGDRegressor.ts:603 (opens in a new tab)
n_iter_
The actual number of iterations before reaching the stopping criterion.
Signature
n_iter_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/linear_model/SGDRegressor.ts:557 (opens in a new tab)
py
Signature
py(): PythonBridge;
Returns
PythonBridge
Defined in: generated/linear_model/SGDRegressor.ts:171 (opens in a new tab)
Signature
py(pythonBridge: PythonBridge): void;
Parameters
Name | Type |
---|---|
pythonBridge | PythonBridge |
Returns
void
Defined in: generated/linear_model/SGDRegressor.ts:175 (opens in a new tab)
t_
Number of weight updates performed during training. Same as (n\_iter\_ \* n\_samples + 1)
.
Signature
t_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/linear_model/SGDRegressor.ts:580 (opens in a new tab)
Methods
densify()
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.
Signature
densify(opts: object): Promise<any>;
Parameters
Name | Type |
---|---|
opts | object |
Returns
Promise
<any
>
Defined in: generated/linear_model/SGDRegressor.ts:263 (opens in a new tab)
dispose()
Disposes of the underlying Python resources.
Once dispose()
is called, the instance is no longer usable.
Signature
dispose(): Promise<void>;
Returns
Promise
<void
>
Defined in: generated/linear_model/SGDRegressor.ts:244 (opens in a new tab)
fit()
Fit linear model with Stochastic Gradient Descent.
Signature
fit(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | any | Training data. |
opts.coef_init? | ArrayLike | The initial coefficients to warm-start the optimization. |
opts.intercept_init? | ArrayLike | The initial intercept to warm-start the optimization. |
opts.sample_weight? | ArrayLike | Weights applied to individual samples (1. for unweighted). |
opts.y? | ArrayLike | Target values. |
Returns
Promise
<any
>
Defined in: generated/linear_model/SGDRegressor.ts:289 (opens in a new tab)
init()
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Signature
init(py: PythonBridge): Promise<void>;
Parameters
Name | Type |
---|---|
py | PythonBridge |
Returns
Promise
<void
>
Defined in: generated/linear_model/SGDRegressor.ts:184 (opens in a new tab)
partial_fit()
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.
Signature
partial_fit(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | any | Subset of training data. |
opts.sample_weight? | ArrayLike | Weights applied to individual samples. If not provided, uniform weights are assumed. |
opts.y? | any [] | Subset of target values. |
Returns
Promise
<any
>
Defined in: generated/linear_model/SGDRegressor.ts:352 (opens in a new tab)
predict()
Predict using the linear model.
Signature
predict(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | any | Input data. |
Returns
Promise
<any
>
Defined in: generated/linear_model/SGDRegressor.ts:397 (opens in a new tab)
score()
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.
Signature
score(opts: object): Promise<number>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
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.sample_weight? | ArrayLike | Sample weights. |
opts.y? | ArrayLike | True values for X . |
Returns
Promise
<number
>
Defined in: generated/linear_model/SGDRegressor.ts:432 (opens in a new tab)
sparsify()
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.
Signature
sparsify(opts: object): Promise<any>;
Parameters
Name | Type |
---|---|
opts | object |
Returns
Promise
<any
>
Defined in: generated/linear_model/SGDRegressor.ts:483 (opens in a new tab)