我在使用交叉验证时遇到问题

时间:2019-05-25 13:42:46

标签: python

我正在预测客户行为,但由于循环无法正常工作,因此在使用K折交叉验证时出现错误
我正在使用交叉验证来找到最佳模型,但是循环不起作用,请提供替代解决方案或答案。

models=[]
models.append(('LR',LogisticRegression()))
models.append(('SM',SVC))
models.append(('RF',RandomForestClassifier()))
models.append(('GB',GradientBoostingClassifier()))

r = []
n = []
for name, model in models:
    kfold = KFold(n_splits=10, random_state=10)
    cv_results = cross_val_score(model, x, y, cv=kfold)
    r.append(cv_results)
    n.append(name)
    m = "%s: %f (%f)" %(name, cv_results.mean() ,cv_results.std())
    print(m)

TypeError                                 Traceback (most recent call last)
<ipython-input-248-754ed0df2e5d> in <module>()
      3 for name, model in models:
      4     kfold = KFold(n_splits=10)
----> 5     cv_results = cross_val_score(model, x, y, cv=kfold)
      6     r.append(cv_results)
      7     n.append(name)

~\Anaconda3\lib\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 

~\Anaconda3\lib\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:

~\Anaconda3\lib\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:

~\Anaconda3\lib\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.

~\Anaconda3\lib\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 

~\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in <genexpr>(.0)
    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:

~\Anaconda3\lib\site-packages\sklearn\base.py in clone(estimator, safe)
     58                             % (repr(estimator), type(estimator)))
     59     klass = estimator.__class__
---> 60     new_object_params = estimator.get_params(deep=False)
     61     for name, param in six.iteritems(new_object_params):
     62         new_object_params[name] = clone(param, safe=False)

TypeError: get_params() missing 1 required positional argument: 'self'

0 个答案:

没有答案