请原谅我的编码经验。我正在尝试使用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))
我遇到的确切错误如下:
估计量应该是实现“ fit”方法的估计量,已传递0。对此的解释也将受到高度赞赏。
答案 0 :(得分:0)
您正在将parameters
传递给grd
,然后再对其进行定义。
尝试在最后一个else语句之后定义grd
,以便在将变量parameters
传递给分类器之前,确保变量{{1}}具有值。
答案 1 :(得分:0)
您的代码中有一些错误:
clf
对象中使用GridSearchCV
,该对象是一个整数,不是您创建的列表中的分类符。parameters
之前声明变量GridSearchCV
fit
,predict
,r2_score
和mean_absolute_error
代码移到for循环的正文中,否则它将仅对最后一个分类器。这是更正后的代码(我以Boston Dataset为例):
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中查看工作代码。