网格搜索SVC:IndexError:数组的索引过多

时间:2019-05-30 16:04:25

标签: scikit-learn svm grid-search svc

我正在尝试使用GridSearchCV来找到SVC的最佳参数。

from sklearn.svm import SVC
from sklearn import svm, grid_search
from sklearn.model_selection import GridSearchCV

param_grid = [
        {'C': [1,5,10,100]},
        ]
algo = SVC(kernel="poly",  degree=5, coef0=2)
grid_search = GridSearchCV(algo, param_grid, cv=3, scoring='neg_mean_squared_error')
grid_search.fit(X_train, y_train)
print(grid_search.best_params_) #line 162

我收到以下错误:

  File "main.py", line 162, in <module>
  IndexError: too many indices for array

当我不使用GridSearchCV时,它会起作用:

from sklearn.svm import SVC
from sklearn import svm, grid_search
from sklearn.model_selection import GridSearchCV

algo = SVC(kernel="poly", C=1, degree=5, coef0=2)
algo.fit(X_train, y_train)
predict_test = algo.predict(X_test)
mse = mean_squared_error(y_test, predict_test)
rmse = np.sqrt(mse)
print(rmse)

我得到一个分数。

1 个答案:

答案 0 :(得分:0)

GridSearchCV.fit()将目标值接受为形状为y[n_samples]的类似数组的[n_samples, n_output]

对于您来说,(892,)。因此,重塑y_train

y_train = y_train.reshape(892,)