请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Pipeline使用网格搜索

我尝试使用网格搜索的方式来寻找最优超参数,但是却出现了如下错误。不用Pipeline是可以的,请问是我调用的方式不对还是Pipeline本身不支持网格搜索操作呢?

代码如下:
import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets

boston = datasets.load_boston()
X = boston.data
y = boston.target

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)

from sklearn.svm import LinearSVR
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

def StandardSVR(epsilon=0.1,C=100, kernel=‘poly’):
return Pipeline([
(‘std_scaler’, StandardScaler()),
(‘SVR’, SVR(epsilon=epsilon, C=C, kernel=kernel))
])

from sklearn.model_selection import GridSearchCV
param_grid = [
{
‘epsilon’: np.linspace(0.01, 1, num=100),
‘C’: [i for i in range(1, 200)],
‘kernel’: [‘poly’, ‘rbf’]
}
]
svr = StandardSVR()
grid_search = GridSearchCV(svr, param_grid, n_jobs=-1, verbose=1) #进行对超参数的网格搜索
grid_search.fit(X_train, y_train)
print(grid_search.best_params_) #打印最佳超参数
best_dt_reg = grid_search.best_estimator_
print(best_dt_reg.score(X_test, y_test))

图片描述

正在回答 回答被采纳积分+3

1回答

liuyubobobo 2019-10-06 02:05:44

对于如何使用 pipeline 进行网格搜索,请参考这里:http://coding.imooc.com/learn/questiondetail/61679.html


继续加油!:)

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信