2 回答
TA贡献1828条经验 获得超6个赞
这是一个纯粹的sklearn答案:
import numpy as np
from sklearn.linear_model import Ridge
alphas = np.logspace(-10, 10, 1000)
solution_norm = []
residual_norm = []
for alpha in alphas:
lm = Ridge(alpha=alpha)
lm.fit(X, y)
solution_norm += [(lm.coef_**2).sum()]
residual_norm += [((lm.predict(X) - y)**2).sum()]
plt.loglog(residual_norm, solution_norm, 'k-')
plt.show()
whereX和y分别是你的预测变量和目标。
TA贡献1860条经验 获得超9个赞
scikit-learn 中没有这样的内置功能,但是Yellowbrick库提供了这样的功能(使用pipor安装conda);将 LassoCV 示例从他们的文档改编到您的 RidgeCV 案例给出:
import numpy as np
from sklearn.linear_model import RidgeCV
from yellowbrick.regressor import AlphaSelection
from yellowbrick.datasets import load_concrete
# Load the regression dataset
X, y = load_concrete()
# Create a list of alphas to cross-validate against
alphas = np.logspace(-10, 1, 40)
# Instantiate the linear model and visualizer
model = RidgeCV(alphas=alphas)
visualizer = AlphaSelection(model)
visualizer.fit(X, y)
visualizer.show()
添加回答
举报