从sklearn尝试“ LeaveOneGroupOut”时收到python异常

时间:2019-01-17 23:50:44

标签: python python-3.x exception scikit-learn cross-validation

我是Scikit-Learn软件包的新手,正在尝试使用 LeaveOneGroupOut 交叉验证进行简单的分类任务。
我使用了以下代码,这些代码是根据 scikit-learn.org 网站上的 [this link] 的文档采用的:

from sklearn.model_selection import LeaveOneGroupOut
from sklearn.model_selection import cross_val_score
from sklearn import svm    

X = Selected_Dataset[:,:-1]
y = Selected_Labels
groups = Selected_SubjIDs

clf = svm.SVC(kernel='linear', C=1)    
cv = LeaveOneGroupOut()
cv.get_n_splits(X, y, groups=groups)

cross_val_score(clf, X, y, cv=cv)

但是此代码生成以下异常:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-27b53a67db71> in <module>
     14 
     15 
---> 16 cross_val_score(clf, X, y, cv=cv)
     17 
     18 

~/miniconda3/lib/python3.6/site-packages/sklearn/model_selection/_validation.py in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch)
    340                                 n_jobs=n_jobs, verbose=verbose,
    341                                 fit_params=fit_params,
--> 342                                 pre_dispatch=pre_dispatch)
    343     return cv_results['test_score']
    344 

~/miniconda3/lib/python3.6/site-packages/sklearn/model_selection/_validation.py in cross_validate(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch, return_train_score)
    204             fit_params, return_train_score=return_train_score,
    205             return_times=True)
--> 206         for train, test in cv.split(X, y, groups))
    207 
    208     if return_train_score:

~/miniconda3/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in __call__(self, iterable)
    777             # was dispatched. In particular this covers the edge
    778             # case of Parallel used with an exhausted iterator.
--> 779             while self.dispatch_one_batch(iterator):
    780                 self._iterating = True
    781             else:

~/miniconda3/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in dispatch_one_batch(self, iterator)
    618 
    619         with self._lock:
--> 620             tasks = BatchedCalls(itertools.islice(iterator, batch_size))
    621             if len(tasks) == 0:
    622                 # No more tasks available in the iterator: tell caller to stop.

~/miniconda3/lib/python3.6/site-packages/sklearn/externals/joblib/parallel.py in __init__(self, iterator_slice)
    125 
    126     def __init__(self, iterator_slice):
--> 127         self.items = list(iterator_slice)
    128         self._size = len(self.items)
    129 

~/miniconda3/lib/python3.6/site-packages/sklearn/model_selection/_validation.py in <genexpr>(.0)
    200                         pre_dispatch=pre_dispatch)
    201     scores = parallel(
--> 202         delayed(_fit_and_score)(
    203             clone(estimator), X, y, scorers, train, test, verbose, None,
    204             fit_params, return_train_score=return_train_score,

~/miniconda3/lib/python3.6/site-packages/sklearn/model_selection/_split.py in split(self, X, y, groups)
     93         X, y, groups = indexable(X, y, groups)
     94         indices = np.arange(_num_samples(X))
---> 95         for test_index in self._iter_test_masks(X, y, groups):
     96             train_index = indices[np.logical_not(test_index)]
     97             test_index = indices[test_index]

~/miniconda3/lib/python3.6/site-packages/sklearn/model_selection/_split.py in _iter_test_masks(self, X, y, groups)
    822     def _iter_test_masks(self, X, y, groups):
    823         if groups is None:
--> 824             raise ValueError("The 'groups' parameter should not be None.")
    825         # We make a copy of groups to avoid side-effects during iteration
    826         groups = check_array(groups, copy=True, ensure_2d=False, dtype=None)

ValueError: The 'groups' parameter should not be None.

我发现20162017中报告了这两个相关的错误。

有什么办法解决吗?

1 个答案:

答案 0 :(得分:2)

您必须使用

@media screen and (max-width: 500px) {
.left__img2 {
    max-width: 10px;
} 
}

,然后您可以删除cross_val_score(clf, X, y, cv=cv, groups=groups)

工作示例

get_n_splits