这是我的代码:
import pandas as pd
# path of the training data
train_data_path = "C:/Users/User/Desktop/Machine_Learning/Neural_Network/addition_train_data.csv"
train_data = pd.read_csv(train_data_path) # loads the data using pandas
# sets a target variable for the ML to predict
# train_target = train_data.pop("Sum")
# path of the evalution data
eval_data_path = "C:/Users/User/Desktop/Machine_Learning/Neural_Network/addition_eval_data.csv"
# loads the data using pandas (again)
eval_data = pd.read_csv(eval_data_path)
# sets a target variable, same as the train_target
eval_target = eval_data.pop("Factor_2")
这是我的错误:
Traceback (most recent call last):
File "C:\Users\User\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in
pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in
pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Factor_2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "addition.py", line 27, in <module>
eval_target = eval_data.pop("Factor_2")
File "C:\Users\User\anaconda3\lib\site-packages\pandas\core\generic.py", line 790, in pop
result = self[item]
File "C:\Users\User\anaconda3\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\User\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in
pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in
pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Factor_2'
我试图加载两个文件,但是当我尝试弹出目标变量时,它返回KeyError。我对此进行了一些研究,但这是一个广泛的错误。
这是我的数据文件:
培训:
Factor_1, Factor_2, Sum
0, 0, 0
1, 0, 1
0, 1, 1
1, 1, 2
2, 0, 2
0, 2, 2
2, 1, 3
1, 2, 3
3, 0, 3
0, 3, 0
3, 1, 4
1, 3, 4
3, 2, 5
2, 3, 5
3, 3, 6
0, 4, 4
4, 0, 4
4, 1, 5
1, 4, 5
4, 2, 6
2, 4, 6
4, 3, 7
3, 4, 7
4, 4, 8
5, 0, 5
0, 5, 5
5, 1, 6
1, 5, 6
5, 2, 7
2, 5, 7
5, 3, 8
3, 5, 8
5, 4, 9
4, 5, 9
5, 5, 10
评估:
Factor_1, Factor_2, Sum
10, 0,
0, 10,
10, 1,
1, 10,
10, 2,
2, 10,
10, 3,
3, 10,
10, 4,
4, 10,
10, 5,
5, 10,
10, 6,
6, 10
10, 7,
7, 10,
10, 8,
8, 10,
10, 9,
9, 10,
10, 10,
20, 20
对于评估数据,“总和”列为空-供ML预测。
我对Tensorflow和ML还是很陌生,非常感谢任何帮助,技巧和建议!
答案 0 :(得分:1)
这是名称问题。错误消息明确指出“ Factor_2”无效。因此,您需要确定实际的列名是什么。
使用df.columns获取列名。
检查前导和尾随空格。逗号是名称的一部分吗?
或者,您可以重命名列。
eval_data.columns = ["Factor_1", "Factor_2", "Sum"]
这时您应该可以弹出列。