我normalized=
对 sklearn.linear_model 在 RidgeCV 中究竟做了什么感到困惑。
文档说:
fit_intercept
normalize : bool, default=False 当设置为 False时忽略此参数。如果为 True,则回归量 X 将在回归前通过减去均值并除以 l2 范数进行归一化。如果您希望标准化,请sklearn.preprocessing.StandardScaler
在调用fit
估算器之前使用 :class:normalize=False
。
- 我们通常将标准化称为减去均值并除以 l2 范数。但是文档将其称为“规范化”。
- 如果我正确理解了文档,我应该使用第三个代码块(最后一个块),如下所示
If you wish to standardize, please use :class:`sklearn.preprocessing.StandardScaler` before calling ``fit`` on an estimator with ``normalize=False``.
- 但是,我该如何解释这些系数呢?这些是标准化的系数吗?但是看看它们的大小,我怀疑它们是标准化的系数。
总的来说,我不确定我是否遵循了有关此normalize
参数的文档。
我将在其他语言中测试类似的代码,看看我得到了什么。
from sklearn.datasets import load_diabetes
from sklearn.linear_model import RidgeCV
X, y = load_diabetes(return_X_y=True)
没有标准化
clf = RidgeCV(normalize=False,alphas=[1e-3, 1e-2, 1e-1, 1]).fit(X, y)
clf.coef_
print(clf.alpha_)
print(clf.score(X,y))
print(clf.coef_)
0.01
0.5166287840315846
[ -7.19945679 -234.55293001 520.58313622 320.52335582 -380.60706569 150.48375154 -78.59123221 130.31305868 592.34958662 71.1337681 ]
标准化和标准化=真
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X)
X_std = scaler.transform(X)
clf = RidgeCV(normalize=True,alphas=[1e-3, 1e-2, 1e-1, 1]).fit(X_std, y)
print("standardize and normalize=True")
print(clf.alpha_)
print(clf.score(X_std,y))
print(clf.coef_)
standardize and normalize=True
0.01
0.5166287840315843
[ -0.34244324 -11.15654516 24.76161466 15.24574131 -18.10363195
7.15778213 -3.7382037 6.19836011 28.17519659 3.38348831]
标准化和标准化=假
clf = RidgeCV(normalize=False,alphas=[1e-3, 1e-2, 1e-1, 1]).fit(X_std, y)
print("standardize and normalize=False")
print(clf.alpha_)
print(clf.score(X_std,y))
print(clf.coef_)
standardize and normalize=False
1.0
0.5175831607267165
[ -0.43127609 -11.33381407 24.77096198 15.37375716 -30.08858903
16.65328714 1.46208255 7.5211415 32.84392268 3.26632702]