【问题标题】:KeyError: ('Col_2', 'Col_3') The above exception was the direct cause of the following exception:KeyError: ('Col_2', 'Col_3') 上述异常是以下异常的直接原因:
【发布时间】: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


    【解决方案1】:

    错误是当您将名称gene1 和gene2 分配给列时,您没有正确执行。

    gene1 = df["Col_2"]
    

    但我什至不会为此烦恼,只是在将数据输入算法时使用实际的列名或更改它们的名称,你会感到困惑。

    【讨论】:

    • 我以前用过这段代码,但没有用。我得到了同样的错误。 x = np.array(data["Col_2","Col_3"])
    • 你需要双括号。 x = np.array(数据[["Col_2","Col_3"]])。您收到的错误意味着 sklearn 无法找到这些数组,这是一个格式问题
    • 我明白了。太感谢了!现在,我没有足够的声望来投票给你的答案。我将数据更改为数组,它也可以工作。数据 = np.array(数据)。
    • 很高兴为您提供帮助!那你能接受我的回答吗?
    • 当然。我接受了。
    猜你喜欢
    • 2022-08-22
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多