首页 > 解决方案 > 我在使用交叉验证时遇到问题

问题描述

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

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'

标签: python

解决方案


推荐阅读