当前位置: 首页 > 知识库问答 >
问题:

KeyError:"没有[Int64Index([12313,\n,34534],\n dtype='int64', leng

越英范
2023-03-14

官方指南

  • 我正在尝试使用官方的最新示例代码StratifiedKFold
>>> import numpy as np
>>> from sklearn.model_selection import StratifiedKFold
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([0, 0, 1, 1])
>>> skf = StratifiedKFold(n_splits=2)
>>> skf.get_n_splits(X, y)
2
>>> print(skf)
StratifiedKFold(n_splits=2, random_state=None, shuffle=False)
>>> for train_index, test_index in skf.split(X, y):
...     print("TRAIN:", train_index, "TEST:", test_index)
...     X_train, X_test = X[train_index], X[test_index]
...     y_train, y_test = y[train_index], y[test_index]
TRAIN: [1 3] TEST: [0 2]
TRAIN: [0 2] TEST: [1 3]

我的代码

  • 我将所有日期保存在两个数据帧X、y中,分别为整数值和浮点值
skf = StratifiedKFold(n_splits=4) # shuffle=True, random_state=1

for train_index, test_index in skf.split(X, y):
    X_train = X[train_index]
    X_test = X[test_index]
    y_train = y[train_index]
    y_test = y[test_index]
    print("TRAIN:", train_index, "TEST:", test_index)

错误

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-2776afce57e9> in <module>
      2 
      3 for train_index, test_index in skf.split(X, y):
----> 4     X_train = X[train_index]
      5     X_test = X[test_index]
      6     y_train = y[train_index]

~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2906             if is_iterator(key):
   2907                 key = list(key)
-> 2908             indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
   2909 
   2910         # take() does not accept boolean indexers

~/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1252             keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
   1253 
-> 1254         self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
   1255         return keyarr, indexer
   1256 

~/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1296             if missing == len(indexer):
   1297                 axis_name = self.obj._get_axis_name(axis)
-> 1298                 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   1299 
   1300             # We (temporarily) allow for some missing keys with .loc, except in

KeyError: "None of [Int64Index([ 785015,  785016,  785017,  785018,  785019,  785020,  785021,\n             785022,  785023,  785024,\n            ...\n            3140252, 3140253, 3140254, 3140255, 3140256, 3140257, 3140258,\n            3140259, 3140260, 3140261],\n           dtype='int64', length=2355196)] are in the [columns]"

我尝试过的解决方案

  • 他在不同的位置有错误-键错误:无[Int64Index…]dtype='int64]在列中

共有1个答案

昝阳嘉
2023-03-14

在这篇文章中,他们的回答略有不同,但其中一条评论回答了我的问题。

>

加载数据时,它必须是Numpy矢量化的,而不是数据帧。

X = mydataframe.drop(['acol','bcol'], axis=1).values 
y = mydataframe['targetvalue'].values
 类似资料:
  • 有一个323列和10348行的数据帧。我想用下面的代码用分层k-Fold来划分它 但是我得到了以下错误 有人告诉我为什么会出现这个错误以及如何修复它吗

  • 法典:- 错误 我试图在列和它们的前陈列室价格之间画一个箱线图。前展厅价格的值是分类的,因此,我首先将它们转换为整数,然后尝试绘制箱线图,但它会抛出错误,关键错误:“None of [Int64Index...] dtype='int64]在列中。

  • 这是我的数据帧: 我试着用它做一个非常简单的情节: 但我一直收到一条关键错误消息: 我尝试将列[a]转换为日期时间,但仍然收到相同的错误消息。

  • 我试图在管道上运行k-折叠交叉验证(标准化定标器,决策树分类器)。 首先,我导入数据。 然后对数据帧进行预处理 然后对特征和目标进行切片 并使用SMOTE来平衡数据 这是问题的一部分。 错误代码

  • 我试图使用np.random.shuffle()方法对索引进行洗牌,但我一直收到一个我不理解的错误。如果有人能帮我解决这个问题,我将不胜感激。非常感谢。 当我在开始创建我的raw_csv_数据变量时,我尝试使用分隔符='、'和delim_空格=0,因为我认为这是另一个问题的解决方案,但它不断抛出相同的错误 这是我尝试洗牌索引时不断遇到的错误: getitem(self,key)中的~\Anacon

  • 将测试和列车数据输入ROC曲线图时,我收到以下错误: KeyError:“[Int64Index([0,1,2,…dtype='int64',length=1323])中没有一个在[columns]中” 错误似乎是说它不喜欢我的数据格式,但它在第一次运行时起作用,我无法让它再次运行。 我是否错误地拆分数据或将格式错误的数据发送到函数中? 阅读几个StackOverflow帖子与相同的KeyErro