我试图找到C&的最佳价值。使用GridSearchCV()的SVR()估计器的gamma,但是我得到了这个错误
TypeError:' KFold'对象不可迭代
这个代码
from sklearn.grid_search import GridSearchCV
from sklearn.model_selection import KFold
C_range = np.logspace(-2, 10, 13)
gamma_range = np.logspace(-9, 3, 13)
param_grid = dict(gamma=gamma_range, C=C_range)
cv = KFold(n_splits=5, shuffle=False, random_state=None)
grid = GridSearchCV(SVR(kernel='rbf'), param_grid=param_grid, cv=cv)
grid.fit(X, y)
print("The best parameters are %s with a score of %0.2f"
% (grid.best_params_, grid.best_score_))
答案 0 :(得分:6)
类似的问题解决了:
<强>更换:强>
from sklearn.grid_search import GridSearchCV
from sklearn.model_selection import GridSearchCV
答案 1 :(得分:1)
cv
是您案例中的一个对象。
您应该使用KFold
来创建批量数据,并将这些批次传递给GridSearchCV
参数中的cv
。
以下是使用KFold
创建拆分的示例:
>>> from sklearn.model_selection import KFold
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([1, 2, 3, 4])
>>> kf = KFold(n_splits=2)
>>> kf.get_n_splits(X)
2
>>> print(kf)
KFold(n_splits=2, random_state=None, shuffle=False)
>>> for train_index, test_index in kf.split(X):
... print("TRAIN:", train_index, "TEST:", test_index)
... X_train, X_test = X[train_index], X[test_index]
... y_train, y_test = y[train_index], y[test_index]
TRAIN: [2 3] TEST: [0 1]
TRAIN: [0 1] TEST: [2 3]