KeyError: ('Col_2', 'Col_3') The above exception was the direct cause of the following exception:
KeyError: ('Col_2', 'Col_3') The above exception was the direct cause of the following exception:
谁能帮我调试这个错误?我很感激!
Col_1 Col_2 Col_3
0 8 0 0
1 0 1 0
2 0 0 1
3 8 0 0
'''
import pandas as pd
import numpy as np
import sklearn
from sklearn import linear_model
from sklearn.utils import shuffle
data = pd.read_csv("simple_data_2.csv")
print(data.shape)
data.dropna()
data = data[["Col_1","Col_2","Col_3"]]
predict ="Col_1"
gene1 = "Col_2"
gene2 = "Col_3"
data = data.dropna()
data = data.reset_index(drop=True)
print(data)
x = np.array(data[gene1,gene2])
y = np.array(data[predict])
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x,y, test_size=0.1)
linear = linear_model.LinearRegression()
linear.fit(x_train,y_train)
acc = linear.score(x_test, y_test)
print(acc)'''
回溯(最后一次调用):
文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 3361 行,在 get_loc 中
return self._engine.get_loc(casted_key)
文件“pandas/_libs/index.pyx”,第 76 行,在 pandas._libs.index.IndexEngine.get_loc 中
文件“pandas/_libs/index.pyx”,第 108 行,在 pandas._libs.index.IndexEngine.get_loc 中
文件“pandas/_libs/hashtable_class_helper.pxi”,第 5198 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item
文件“pandas/_libs/hashtable_class_helper.pxi”,第 5206 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item 中
KeyError: ('Col_2', 'Col_3')
上述异常是以下异常的直接原因:
回溯(最后一次调用):
文件“/Users/Chris/PycharmProjects/TensorEnv/debug.py”,第 16 行,位于
x = np.array(数据[基因1,基因2])
文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/frame.py”,第 3458 行,在 getitem 中
索引器 = self.columns.get_loc(键)
文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 3363 行,在 get_loc 中
从 err 引发 KeyError(key)
KeyError: ('Col_2', 'Col_3')
错误是当您将名称 gene1 和 gene2 分配给列时,您没有正确执行。
gene1 = df["Col_2"]
但我什至不会为此烦恼,只要在将数据输入算法时使用实际的列名或更改它们的名称,您就会感到困惑。
谁能帮我调试这个错误?我很感激!
Col_1 Col_2 Col_3
0 8 0 0
1 0 1 0
2 0 0 1
3 8 0 0
'''
import pandas as pd
import numpy as np
import sklearn
from sklearn import linear_model
from sklearn.utils import shuffle
data = pd.read_csv("simple_data_2.csv")
print(data.shape)
data.dropna()
data = data[["Col_1","Col_2","Col_3"]]
predict ="Col_1"
gene1 = "Col_2"
gene2 = "Col_3"
data = data.dropna()
data = data.reset_index(drop=True)
print(data)
x = np.array(data[gene1,gene2])
y = np.array(data[predict])
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x,y, test_size=0.1)
linear = linear_model.LinearRegression()
linear.fit(x_train,y_train)
acc = linear.score(x_test, y_test)
print(acc)'''
回溯(最后一次调用): 文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 3361 行,在 get_loc 中 return self._engine.get_loc(casted_key) 文件“pandas/_libs/index.pyx”,第 76 行,在 pandas._libs.index.IndexEngine.get_loc 中 文件“pandas/_libs/index.pyx”,第 108 行,在 pandas._libs.index.IndexEngine.get_loc 中 文件“pandas/_libs/hashtable_class_helper.pxi”,第 5198 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item 文件“pandas/_libs/hashtable_class_helper.pxi”,第 5206 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item 中 KeyError: ('Col_2', 'Col_3')
上述异常是以下异常的直接原因:
回溯(最后一次调用): 文件“/Users/Chris/PycharmProjects/TensorEnv/debug.py”,第 16 行,位于 x = np.array(数据[基因1,基因2]) 文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/frame.py”,第 3458 行,在 getitem 中 索引器 = self.columns.get_loc(键) 文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 3363 行,在 get_loc 中 从 err 引发 KeyError(key) KeyError: ('Col_2', 'Col_3')
错误是当您将名称 gene1 和 gene2 分配给列时,您没有正确执行。
gene1 = df["Col_2"]
但我什至不会为此烦恼,只要在将数据输入算法时使用实际的列名或更改它们的名称,您就会感到困惑。