gpt4 book ai didi

python - 网格搜索 SVC : IndexError: too many indices for array

转载 作者:行者123 更新时间:2023-12-01 07:47:36 25 4
gpt4 key购买 nike

我正在尝试使用 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)

我得到了分数。

最佳答案

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

就您而言,(892,)。因此, reshape y_train:

y_train = y_train.reshape(892,)

关于python - 网格搜索 SVC : IndexError: too many indices for array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56381766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com