GridSearchCV in scikit-learn(sklearn): TypeError: 'KFold' object is not iterable

5.5k views Asked by At

I'm trying to find best values of C & gamma for SVR() estimator using GridSearchCV() but I get this error

TypeError: 'KFold' object is not iterable

This the code

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_))
2

There are 2 answers

0
Farseer On

cv is an object in your case. You should use KFold in order to create batches of data, and pass these batches to GridSearchCV in cv argument.

Here an example on how to create splits, using 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]
0
seralouk On

Similar problem solved by:

Replacing:

from sklearn.grid_search import GridSearchCV

with

from sklearn.model_selection import GridSearchCV