RidgeCV#
Implementa la regresión Ridge con validación cruzada.
Por defecto usa leave-on-out cross-validation.
[1]:
from sklearn.datasets import load_diabetes
X, y = load_diabetes(return_X_y=True)
[2]:
from sklearn.linear_model import RidgeCV
ridgeCV = RidgeCV(
# ---------------------------------------------------------------------
# Regularization strength; must be a positive float. Regularization
# improves the conditioning of the problem and reduces the variance of
# the estimates. Larger values specify stronger regularization. Alpha
# corresponds to 1 / (2C) in other linear models such as
# LogisticRegression or LinearSVC.
# alphas=(0.1, 1.0, 10.0),
alphas=[1e-3, 1e-2, 1e-1, 1],
# ---------------------------------------------------------------------
# Whether to fit the intercept for this model.
fit_intercept=True,
# ---------------------------------------------------------------------
# A string (see model evaluation documentation) or a scorer callable
# object / function with signature scorer(estimator, X, y).
scoring=None,
# ---------------------------------------------------------------------
# Determines the cross-validation splitting strategy. Possible inputs
# for cv are:
# * None, to use the efficient Leave-One-Out cross-validation
# * integer, to specify the number of folds.
# * CV splitter
# * An iterable yielding (train, test) splits as arrays of indices.
cv=None,
# ---------------------------------------------------------------------
# Flag indicating which strategy to use when performing Leave-One-Out
# Cross-Validation.
# * 'auto' : use 'svd' if n_samples > n_features, otherwise use 'eigen'
# * 'svd'
# * 'eigen' : force computation via eigendecomposition of X.X^T
gcv_mode='auto',
# ---------------------------------------------------------------------
# Flag indicating if the cross-validation values corresponding to each
# alpha should be stored in the cv_values_ attribute (see below). This
# flag is only compatible with cv=None (i.e. using Leave-One-Out
# Cross-Validation).
store_cv_values=False,
# ---------------------------------------------------------------------
# Flag indicating whether to optimize the alpha value (picked from the
# alphas parameter list) for each target separately (for multi-output
# settings: multiple prediction targets). When set to True, after
# fitting, the alpha_ attribute will contain a value for each target.
# When set to False, a single alpha is used for all targets.
alpha_per_target=False,
)
ridgeClassifierCV.fit(X, y)
ridgeClassifierCV.score(X, y)
[2]:
0.9630931458699473
[3]:
ridgeClassifierCV.alpha_
[3]:
0.01
[4]:
ridgeClassifierCV.coef_
[4]:
array([[ 2.70962810e-01, -1.99904473e-02, -2.68869779e-02,
-5.52130776e-04, 7.51784333e-01, 6.70081562e+00,
-2.24514478e+00, -3.78660924e+00, -2.34922271e-01,
3.05972457e-01, -9.50391185e-01, -3.25337487e-02,
7.39111510e-03, 3.34830382e-03, -5.15606649e+00,
-1.39236707e+00, 4.94530386e+00, -4.00371830e+00,
-2.54634379e+00, -1.35630031e-02, -3.49442568e-01,
-5.24646097e-03, 7.11490969e-03, 1.75379598e-03,
-4.16225726e+00, -5.32987896e-02, -6.21848378e-01,
-2.48894530e+00, -9.96733111e-01, -4.46615638e+00]])
[5]:
ridgeClassifierCV.intercept_
[5]:
array([4.76062228])