本文参考skearn官网教程,链接如下:
https://scikit-learn.org/stable/modules/linear_model.html#lasso
我们都知道:ridge是l2正则化的线性回归,lasso则是带l1正则化的线性回归。进一步说,他们都同样的比线性回归多一个超参数需要调,alpha。所以有了RidgeCV,LassoCV的说法。也就是说我们必须找到合理的alpha,那么这个线性模型我们才能说是找好了。
所以我建议在用这两个模型时,尽量都用CV形式的,而不是用Lasso与Ridge。
1.RidgeCV:
from sklearn import linear_model
reg = linear_model.RidgeCV(alphas=[0.1, 1.0, 10.0], cv=3)
reg.fit([[0, 0], [0, 0], [1, 1]], [0, .1, 1])
print(reg.alpha_)
??RidgeCV
#部分输出
Init signature: RidgeCV(alphas=(0.1, 1.0, 10.0), fit_intercept=True, normalize=False, scoring=None, cv=None, gcv_mode=None, store_cv_values=False)
Source:
class RidgeCV(_BaseRidgeCV, RegressorMixin):
"""Ridge regression with built-in cross-validation.
By default, it performs Generalized Cross-Validation, which is a form of
efficient Leave-One-Out cross-validation.
Read more in the :ref:`User Guide <ridge_regression>`.
Parameters
----------
alphas : numpy array of shape [n_alphas]
Array of alpha values to try.
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 ``C^-