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

Posted

技术标签:

【中文标题】AttributeError:“切片”对象没有属性“标志”错误【英文标题】:AttributeError: 'slice' object has no attribute 'flags' Error 【发布时间】:2019-08-04 05:35:16 【问题描述】:

我正在尝试使用 GridSearchCv 而不进行交叉验证,因为我有一组单独的验证。我发现 this 解决方案建议我应该使用 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'

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

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

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

这应该是一样的效果

【讨论】:

这适用于 GridSearchCv?让我试着回去。谢谢。

以上是关于AttributeError:“切片”对象没有属性“标志”错误的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError: 'RDD' 对象没有属性 'show'

AttributeError:“NumpyArrayIterator”对象没有属性“类”

AttributeError:'list' 对象没有属性 'size'

AttributeError:“模块”对象没有属性“WebSocketApp”

AttributeError: 'float' 对象没有属性 'split'

AttributeError:“列表”对象没有属性“numpy”