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

gridSearch in loop **estimator should be an estimator implementing 'fit' method, 0 was passed** error

请原谅我的编码经验。我正在尝试使用 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。 也将不胜感激。

您在定义之前将 parameters 传递给 grd

尝试在最后一个 else 语句之后定义 grd,以便确保变量 parameters 在传递给分类器之前保持值。

您的代码中有一些错误:

  • 您正在 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 notebook here.

中查看工作代码