Pipeline使用网格搜索
来源:11-9 SVM思想解决回归问题
louyuwc
2019-10-05
我尝试使用网格搜索的方式来寻找最优超参数,但是却出现了如下错误。不用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))
1回答
-
liuyubobobo
2019-10-06
00
相似问题