Sklearn GridSearchCV TypeError: 'LeaveOneOut' object is not iterable

Viewed 4483

I am trying to solve an error that I get but with no luck.

My goal is to perform a GridSearch with Cross validation (Leave One Out) for a neural net. I am using sklearn and python 2.7. I have 300 samples.

Here is my code:

from sklearn.model_selection import LeaveOneOut
from sklearn import metrics
from sklearn.neural_network import MLPClassifier
from sklearn import grid_search

my_cv = LeaveOneOut()

param_grid = {'hidden_layer_sizes': [(249,),(500,),(250,),(10,)], 
'activation': ('relu', 'logistic'),'solver': 
('sgd','adam'),'learning_rate_init':[0.005,0.05,0.001]}

nn = MLPClassifier(alpha=0.001,random_state=1,nesterovs_momentum=True)
clf = grid_search.GridSearchCV(estimator=nn, param_grid=param_grid, cv=my_cv)
clf.fit(X, y)
print clf.best_score_
print clf.best_params_

The error that I get is: TypeError: 'LeaveOneOut' object is not iterable

Note that same thing happens if I use KFold(n_splits=300) instead of LeaveOneOut().

UPDATE

SOLVED : after replacing

from sklearn import grid_search

with

from sklearn.model_selection import GridSearchCV

thanks to @mkaran

0 Answers
Related