首页 > 解决方案 > 如何在 sklearn 中使用 Ridge 回归运行 GridsearchCV

问题描述

我正在从 sklearn 导入 GridsearchCV 来执行此操作。我不知道我应该在参数的数组中给出什么值:

Parameters={'alpha':[array]}
Ridge_reg=GridsearchCV (ridge,parameters,scoring='neg mean squared error',cv=5)
  1. 这个对吗?
  2. 如何查看岭回归图?

标签: pythonscikit-learn

解决方案


您发布的代码有多个语法错误,例如GridsearchCVscoring='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()

在此处输入图像描述


推荐阅读