pyrfm.linear_model.SGDRegressor¶
-
class
pyrfm.linear_model.
SGDRegressor
(transformer=RBFSampler(gamma=1.0, n_components=100, random_state=None), eta0=0.01, loss='squared', C=1.0, alpha=1.0, l1_ratio=0.0, intercept_decay=0.1, normalize=False, fit_intercept=True, max_iter=100, tol=1e-06, learning_rate='optimal', power_t=0.75, average=1, warm_start=False, random_state=None, verbose=True, fast_solver=True, shuffle=True)[source]¶ Bases:
pyrfm.linear_model.sgd.BaseSGDEstimator
,pyrfm.linear_model.base.LinearRegressorMixin
SGD solver for linear regression with random feature maps.
Random feature mapping is computed just before computing prediction and gradient. minimize sum_{i=1}^{n} loss(x_i, y_i) + alpha/C*reg
- Parameters
transformer (scikit-learn Transformer object (default=RBFSampler())) – A scikit-learn TransformerMixin object. transformer must have (1) n_components attribute, (2) fit(X, y), and (3) transform(X).
eta0 (double (default=0.01)) – Step-size parameter.
loss (str (default="squared")) –
Which loss function to use. Following losses can be used:
’squared’
C (double (default=1.0)) – Weight of the loss term.
alpha (double (default=1.0)) – Weight of the penalty term.
l1_ratio (double (default=0)) –
Ratio of L1 regularizer. Weight of L1 regularizer is alpha * l1_ratio and that of L2 regularizer is 0.5 * alpha * (1-l1_ratio).
l1_ratio = 0 : Ridge.
l1_ratio = 1 : Lasso.
Otherwise : Elastic Net.
intercept_decay (double (default=0.1)) – Weight of the penalty term for intercept.
normalize (bool (default=False)) – Whether normalize random features or not. If true, the sgd solver computes running mean and variance at learning, and uses them for inference.
fit_intercept (bool (default=True)) – Whether to fit intercept (bias term) or not.
max_iter (int (default=100)) – Maximum number of iterations.
tol (double (default=1e-6)) – Tolerance of stopping criterion. If sum of absolute val of update in one epoch is lower than tol, the SGD solver stops learning.
learning_rate (str (default='optimal')) –
The method for learning rate decay.
’constant’: eta = eta0
’pegasos’: eta = 1.0 / (alpha * (1-l1_ratio) * t)
’inv_scaling’: eta = eta0 / pow(t, power_t)
’optimal’: eta = eta0 / pow(1 + eta0*alpha*(1-l1_ratio)*t, power_t)
power_t (double (default=0.75)) – The parameter for learning_rate ‘inv_scaling’ and ‘optimal’.
average (int or bool (default=1)) – Whether output averaged weight or not. If type(average) is int, the optimizer will compute the averaged weights after average iterations. If average=True, the optimizer will compute the averaged weights after 1 iterations (i.e., averaging over all iterations). If averaging, you should use learning_rate=’optimal’ and power_t=0.75[2].
warm_start (bool (default=False)) – Whether to activate warm-start or not.
random_state (int, RandomState instance or None, optional (default=None)) – If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
verbose (bool (default=True)) – Verbose mode or not.
fast_solver (bool (default=True)) – Use cython fast solver or not. This argument is valid when transformer is implemented in random_features_fast.pyx/pxd.
shuffle (bool (default=True)) – Whether to shuffle data before each epoch or not.
-
self.
coef_
¶ The learned coefficients of the linear model.
- Type
array, shape (n_components, )
-
self.
coef_average_
¶ The latest learned coefficients of the linear model for average=True.
- Type
array, shape (n_components, )
-
self.
intercept_
¶ The learned intercept (bias) of the linear model.
- Type
array, shape (1, )
-
self.
intercept_average_
¶ The latest learned intercept (bias) of the linear model for average=True.
- Type
array, shape (1, )
-
self.mean_, self.var_
The running mean and variances of random feature vectors. They are used if normalize=True (they are None if False).
- Type
array or None, shape (n_components, )
-
self.
t_
¶ The number of iteration.
- Type
int
References
[1] Large-Scale Machine Learning with Stochastic Gradient Descent. Leon Bottou. In Proc. COMPSTAT’2010. (https://leon.bottou.org/publications/pdf/compstat-2010.pdf)
[2] Stochastic Gradient Descent Tricks. Leon Bottou. Neural Networks, Tricks of the Trade, Reloaded, 430–445, Lecture Notes in Computer Science (LNCS 7700), Springer, 2012 (https://link.springer.com/content/pdf/10.1007%2F978-3-642-35289-8_25.pdf)
-
LEARNING_RATE
= {'constant': 0, 'inv_scaling': 2, 'optimal': 3, 'pegasos': 1}¶
-
LOSSES
= {'squared': <pyrfm.linear_model.loss_fast.Squared object>}¶
-
fit
(X, y)¶ Fit model according to X and y.
- Parameters
X (array-like, shape = [n_samples, n_features]) – Training vectors, where n_samples is the number of samples and n_features is the number of features.
y (array-like, shape = [n_samples]) – Target values.
- Returns
self – Returns self.
- Return type
classifier
-
get_params
(deep=True)¶ Get parameters for this estimator.
- Parameters
deep (boolean, optional) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
params – Parameter names mapped to their values.
- Return type
mapping of string to any
-
predict
(X)¶ Perform regression on an array of test vectors X.
- Parameters
X (array-like, shape = [n_samples, n_features]) –
- Returns
Predicted target values for X
- Return type
array, shape = [n_samples]
-
score
(X, y, sample_weight=None)¶ Returns the coefficient of determination R^2 of the prediction.
The coefficient R^2 is defined as (1 - 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
X (array-like, shape = (n_samples, n_features)) – Test samples. For some estimators this may be a precomputed kernel matrix instead, shape = (n_samples, n_samples_fitted], where n_samples_fitted is the number of samples used in the fitting for the estimator.
y (array-like, shape = (n_samples) or (n_samples, n_outputs)) – True values for X.
sample_weight (array-like, shape = [n_samples], optional) – Sample weights.
- Returns
score – R^2 of self.predict(X) wrt. y.
- Return type
float
Notes
The R2 score used when calling
score
on a regressor will usemultioutput='uniform_average'
from version 0.23 to keep consistent with metrics.r2_score. This will influence thescore
method of all the multioutput regressors (except for multioutput.MultiOutputRegressor). To specify the default value manually and avoid the warning, please either call metrics.r2_score directly or make a custom scorer with metrics.make_scorer (the built-in scorer'r2'
usesmultioutput='uniform_average'
).
-
set_params
(**params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.- Returns
- Return type
self
-
stochastic
= True¶