0

我正在尝试使用提供的代码在 Python3 中使用 PLSR(偏最小二乘回归)开发模型https://github.com/pgbrodrick/ensemblePLSR。还提供了样本数据。

当我尝试运行代码时,它给了我错误

>>> python3 ensemble_plsr.py example_settings.txt

我正在使用 Python (3.7.3)、python 模块 scikit-learn (0.20.2) 和 pandas (0.23.3)。

/usr/lib/python3/dist-packages/sklearn/externals/joblib.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
n bad bands 57
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pandas/core/indexes/base.py", line 3078, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: -1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ensemble_plsr.py", line 173, in <module>
    df.pop(col)
  File "/usr/lib/python3/dist-packages/pandas/core/generic.py", line 760, in pop
    result = self[item]
  File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 2688, in __getitem__
    return self._getitem_column(key)
  File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 2695, in _getitem_column
    return self._get_item_cache(key)
  File "/usr/lib/python3/dist-packages/pandas/core/generic.py", line 2491, in _get_item_cache
    values = self._data.get(item)
  File "/usr/lib/python3/dist-packages/pandas/core/internals.py", line 4115, in get
    loc = self.items.get_loc(item)
  File "/usr/lib/python3/dist-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: -1
4

1 回答 1

0

简而言之,您试图删除在 ensemble_plsr.py 的第 173 行没有引用的列,因此在执行期间出现 KeyError。幕后发生的事情是,当 Python 尝试在 DataFrame 上为未指定/不存在的列执行 pop 方法时,它会引发该错误。有不同的方法可以解决此问题,但此解决方案将解决您看到的错误:

将 ensemble_plsr.py 中的第 172 行和第 173 行替换为以下内容:

for col in sf.get_setting('ignore columns'):
    if col != 'nothing_here':
      df.pop(col)

将 example_settings.txt 中的第 16 行替换为以下内容:

ignore columns(any other columns to remove) = nothing_here

好消息,你已经解决了这个问题。坏消息,您将遇到下一个错误,但您正在路上!

于 2020-03-11T20:51:26.763 回答