简体   繁体   English

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

[英]KeyError: "None of [Int64Index([ 12313,\n , 34534],\n dtype='int64', leng

Official Guide官方指南

>>> 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]

MY CODE我的代码

  • I keep all my date in 2 pandas data frame X,y in integer and float values我将所有日期保存在 2 个熊猫数据框 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)

ERROR错误

---------------------------------------------------------------------------
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]"

Solutions that I have tried我尝试过的解决方案

In this post they answer it in a different way a bit but one of the comments answers my questions.在这篇文章中,他们以不同的方式回答了这个问题,但其中一条评论回答了我的问题。

X = mydataframe.drop(['acol','bcol'], axis=1).values 
y = mydataframe['targetvalue'].values

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 KeyError:“[Int64Index([112, 113,..121,\n.\n 58, 559],\n dtype='int64', length=448)] 都不在 [列] 中” - KeyError: "None of [Int64Index([112, 113,..121,\n .\n 58, 559],\n dtype='int64', length=448)] are in the [columns]" 读取 CSV &amp; Columns - KeyError: “[Int64Index([0, 1, 2, 3], dtype='int64')] 都在 [columns] 中” - Reading CSV & Columns - KeyError: “None of [Int64Index([0, 1, 2, 3], dtype='int64')] are in the [columns]” KeyError:“[Int64Index dtype=&#39;int64&#39;, length=9313)] 都不在 [columns]” - KeyError: "None of [Int64Index dtype='int64', length=9313)] are in the [columns]" Receiving KeyError: “[Int64Index([ ... dtype=&#39;int64&#39;, length=1323)] 都不在 [columns]” - Receiving KeyError: "None of [Int64Index([ ... dtype='int64', length=1323)] are in the [columns]" Python Mlens Ensemble:KeyError:“[Int64Index([... dtype='int64', length=105)] 均不在 [columns] 中” - Python Mlens Ensemble: KeyError: "None of [Int64Index([... dtype='int64', length=105)] are in the [columns]" 关键错误:[Int64Index([…]dtype='int64')] 均不在 [columns] 中 - Key Error: None of [Int64Index([…]dtype='int64')] are in the [columns] [Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64', name='index')] 中没有一个在 [index] - None of [Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64', name='index')] are in the [index] KeyError:使用 drop_duplicates 时的 Int64Index([1], dtype='int64') - KeyError: Int64Index([1], dtype='int64') when using drop_duplicates 关键错误:[Int64Index…] dtype='int64] 均不在 [columns] 中 - Key Error: None of [Int64Index…] dtype='int64] are in the [columns] Sklearn 错误:[Int64Index([2, 3], dtype=&#39;int64&#39;)] 均不在 [columns] 中 - Sklearn error: None of [Int64Index([2, 3], dtype='int64')] are in the [columns]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM