2016-12-31 95 views
2

我試圖找到最佳值的C &伽馬SVR()估計使用GridSearchCV(),但我得到這個錯誤RBF SVM參數在scikit中使用GridSearchCV()學習.... TypeError:'KFold'對象不可迭代

類型錯誤: '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_)) 

回答

1

cv是你的情況的對象。 您應該使用KFold來創建批次數據,並將這些批次傳遞給GridSearchCVcv參數。

這裏就如何創建一個分裂例如,使用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] 
5

要解決類似的問題:

更換:

from sklearn.grid_search import GridSearchCV 

from sklearn.model_selection import GridSearchCV