gpt4 book ai didi

python - gridSearch in loop **estimator should be an estimator implementing 'fit' method, 0 was passed** 错误

转载 作者:行者123 更新时间:2023-11-28 18:56:19 24 4
gpt4 key购买 nike

请原谅我的编码经验。我正在尝试使用 GridSearch 进行一系列回归。我正在尝试循环整个过程以使过程更快,但我的代码不够好并且不介意提高效率。这是我的代码:

classifiers=[Lasso(max_iter=700,random_state=42), Ridge(max_iter=700,random_state=42), ElasticNet(max_iter=700,random_state=42)]

for clf in range(len(classifiers)):
grd=GridSearchCV(clf,parameters)

name = clf.__class__.__name__

print("="*25)
print(name)

if clf==0:
parameters={'alpha':[0.0005,0.0006,0.06,0.5,0.0001,0.01,1,2,3,4,4.4,4]}

elif clf==1:
parameters = {'alpha':[1,2,3,5,10,11,2,13,14,15]}

else:
parameters ={'alpha':[0.06,0.5,0.0001,0.01,1,2,3,4,4.4,4,5]}

grd.fit(X_train,y_train)
pred=grid.predict(X_test)

Rs = r2_score(y_test, pred)
rmse=np.sqrt(mean_squared_error(y_test,pred))

print('The R-squared is {:.4}'.format(Rs))
print('The root mean squared is {:.4}'.format(rmse))

我遇到的确切错误如下:

estimator 应该是实现“fit”方法的估计器,0 已通过。也将不胜感激。

最佳答案

您的代码中有一些错误:

  • 您正在 GridSearchCV 对象中使用 clf,它是一个整数,而不是您创建的列表中的分类器。
  • 在传入GridSearchCV之前,您需要声明变量parameters
  • 最后,您需要将 fitpredictr2_scoremean_absolute_error 代码移动到for 循环,否则它只会执行最后一个分类器的计算。

这是更正后的代码(我以波士顿数据集为例):

from sklearn.linear_model import Lasso, Ridge, ElasticNet
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_squared_error
import numpy as np

random_state = 42

# Load boston dataset
boston = load_boston()
X, y = boston['data'], boston['target']
X_train, X_test, y_train, y_test = train_test_split(X, y,
random_state=random_state)

classifiers=[Lasso(max_iter=700,random_state=random_state),
Ridge(max_iter=700,random_state=random_state),
ElasticNet(max_iter=700,random_state=random_state)]

for clf in range(len(classifiers)):
# First declare the variable parameters
if clf==0:
parameters={'alpha':[0.0005,0.0006,0.06,0.5,0.0001,0.01,1,2,3,4,4.4,4]}

elif clf==1:
parameters = {'alpha':[1,2,3,5,10,11,2,13,14,15]}

else:
parameters ={'alpha':[0.06,0.5,0.0001,0.01,1,2,3,4,4.4,4,5]}

# Use clf as index to get the classifier
current_clf = classifiers[clf]
grid=GridSearchCV(current_clf, parameters)

# This is the correct classifier name, previously it returned int
name = current_clf.__class__.__name__

print("="*25)
print(name)

# Moved the below code inside the for loop
grid.fit(X_train,y_train)
pred=grid.predict(X_test)

Rs = r2_score(y_test, pred)
rmse=np.sqrt(mean_squared_error(y_test,pred))

print('The R-squared is {:.4}'.format(Rs))
print('The root mean squared is {:.4}'.format(rmse))

您可以在 Google Colab 笔记本中查看工作代码 here .

关于python - gridSearch in loop **estimator should be an estimator implementing 'fit' method, 0 was passed** 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com