2

我试图在GridSearchCv没有交叉验证的情况下使用,因为我有一组单独的验证。我发现这个解决方案建议我应该使用cv=[(slice(None), slice(None))]cv 参数的代码并且它可以工作。但是这段代码在我同事的 PyCharm 编辑器中失败了。这是错误:AttributeError: 'slice' object has no attribute 'flags'

这是完整的错误堆栈:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2018.3.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/user/PycharmProjects/aitrading/src/studies/ModelRunner.py", line 61, in <module>
    ModelRunner()
  File "C:/Users/user/PycharmProjects/aitrading/src/studies/ModelRunner.py", line 58, in _init_
    grid_result = grid.fit(self.train_X, self.train_y, validation_data=(self.valid_X, self.valid_y))
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\model_selection\_search.py", line 722, in fit
    self._run_search(evaluate_candidates)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\model_selection\_search.py", line 1191, in _run_search
    evaluate_candidates(ParameterGrid(self.param_grid))
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\model_selection\_search.py", line 711, in evaluate_candidates
    cv.split(X, y, groups)))
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\parallel.py", line 917, in _call_
    if self.dispatch_one_batch(iterator):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\parallel.py", line 759, in dispatch_one_batch
    self._dispatch(tasks)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\parallel.py", line 716, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 182, in apply_async
    result = ImmediateResult(func)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 549, in _init_
    self.results = batch()
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\parallel.py", line 225, in _call_
    for func, args, kwargs in self.items]
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\externals\joblib\parallel.py", line 225, in <listcomp>
    for func, args, kwargs in self.items]
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\model_selection\_validation.py", line 518, in _fit_and_score
    X_train, y_train = _safe_split(estimator, X, y, train)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\metaestimators.py", line 203, in _safe_split
    X_subset = safe_indexing(X, indices)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\_init_.py", line 202, in safe_indexing
    indices = indices if indices.flags.writeable else indices.copy()
AttributeError: 'slice' object has no attribute 'flags'

我该如何解决这个问题?

4

1 回答 1

2

尝试在 GridSearch 中不使用 kfolds 时遇到完全相同的问题。我尝试改用另一种方法:

cv = ShuffleSplit(test_size=0.01, n_splits=1)

这应该是一样的效果

于 2019-03-22T15:53:20.010 回答