在语言检测中使用数组作为特征时出现KeyError

问题描述 投票:0回答:1

我正在遵循 this 使用机器学习进行语言检测的教程。然而,在我使用的数据集中,有多个变量作为特征。我尝试用

X = data["Text"]
X = df["message", "fingers", "tail"]
代替(消息、手指和尾巴是我正在使用的三个特征变量),但它抛出了 KeyError;

Traceback (most recent call last):
  File "C:\Users\usr\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc
    return self._engine.get_loc(casted_key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc
  File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\\_libs\\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\\_libs\\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('message', 'fingers', 'tail')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\usr\Downloads\thecode.py", line 13, in <module>
    X = df["message", "fingers", "tail"]
        ~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\usr\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 4102, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\usr\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc
    raise KeyError(key) from err
KeyError: ('message', 'fingers', 'tail')

我应该如何实现代码才能使用所有功能而不引发错误?

python pandas machine-learning naivebayes language-detection
1个回答
0
投票

将代码替换为

X = np.asarray(df[["message", "fingers", "tail"]])
即可解决该问题。

© www.soinside.com 2019 - 2024. All rights reserved.