AttributeError:“切片”对象没有属性“标志”错误

时间:2019-03-13 13:56:27

标签: python slice gridsearchcv

我尝试使用GridSearchCv而不进行交叉验证,因为我有单独的一组验证。我发现了this解决方案,建议我对cv参数使用cv=[(slice(None), slice(None))]代码,它可以工作。但是此代码在我同事的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'

我该如何解决?

1 个答案:

答案 0 :(得分:0)

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

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

效果应该一样