【发布时间】:2022-01-20 16:42:21
【问题描述】:
谁能帮我调试这个错误?我很感激!
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)'''
Traceback(最近一次调用最后一次): 文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 3361 行,在 get_loc 返回 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')
上述异常是以下异常的直接原因:
Traceback(最近一次调用最后一次): 文件“/Users/Chris/PycharmProjects/TensorEnv/debug.py”,第 16 行,在 x = np.array(数据[gene1,gene2]) getitem 中的文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/frame.py”,第 3458 行 索引器 = self.columns.get_loc(key) 文件“/Users/Chris/opt/anaconda3/envs/tf/lib/python3.7/site-packages/pandas/core/indexes/base.py”,第 3363 行,在 get_loc 从错误中引发 KeyError(key) KeyError: ('Col_2', 'Col_3')
【问题讨论】:
标签: scikit-learn