python - 如何在 sklearn 中使用 Ridge 回归运行 GridsearchCV
问题描述
我正在从 sklearn 导入 GridsearchCV 来执行此操作。我不知道我应该在参数的数组中给出什么值:
Parameters={'alpha':[array]}
Ridge_reg=GridsearchCV (ridge,parameters,scoring='neg mean squared error',cv=5)
- 这个对吗?
- 如何查看岭回归图?
解决方案
您发布的代码有多个语法错误,例如GridsearchCV
和scoring='neg mean squared error'
.
第一个输入参数应该是一个对象(模型)。
用这个:
from sklearn.linear_model import Ridge
import numpy as np
from sklearn.model_selection import GridSearchCV
n_samples, n_features = 10, 5
rng = np.random.RandomState(0)
y = rng.randn(n_samples)
X = rng.randn(n_samples, n_features)
parameters = {'alpha':[1, 10]}
# define the model/ estimator
model = Ridge()
# define the grid search
Ridge_reg= GridSearchCV(model, parameters, scoring='neg_mean_squared_error',cv=5)
#fit the grid search
Ridge_reg.fit(X,y)
# best estimator
print(Ridge_reg.best_estimator_)
# best model
best_model = Ridge_reg.best_estimator_
best_model.fit(X,y)
...
...
对于可视化(作为正则化函数的岭系数):
import matplotlib.pyplot as plt
alphas = [1, 10]
coefs = []
for a in alphas:
ridge = Ridge(alpha=a, fit_intercept=False)
ridge.fit(X, y)
coefs.append(ridge.coef_)
ax = plt.gca()
ax.plot(alphas, coefs)
ax.set_xscale('log')
ax.set_xlim(ax.get_xlim()[::-1]) # reverse axis
plt.xlabel('alpha')
plt.ylabel('weights')
plt.title('Ridge coefficients as a function of the regularization')
plt.axis('tight')
plt.show()
推荐阅读
- react-native - 从 Immutable 迁移到 Immer,缺少 object.filter()
- sql - 如何计算一列中某些值从一个值到下一个值的变化,同时在另一列中有一些共同值?
- python - Python字典导致递归问题
- python - Python f-string 在浮点数上的惊人结果
- javascript - fetch 未被识别为异步函数
- python - Discord bot 未上线,可能是证书错误?
- python - 如何使用 Python 快速下载 Tar 文件、解压缩并将内容上传到 Azure 块存储?
- python - 只打开一个 Tk.Toplevel 窗口
- java - 在 Java 中,当我们在终端上打印字符串文字时,该字符串文字是否也存储在字符串池中?
- ffmpeg - FFmpeg 剪切视频结束没有声音