随机森林K折cross_validation键错误[Int64Index…] dtype ='int64]均不在列中

时间:2019-12-27 01:01:28

标签: python scikit-learn k-fold

我正在尝试对具有四个变量的df进行随机森林k折交叉验证,例如

df="A","A1","A2","A3"

但是我总是收到错误消息

"None of [Int64Index([ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,\n            23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,\n            40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50],\n           dtype='int64')] are in the [columns]"

如果有人可以帮助我,我将不胜感激。

代码行

from sklearn.model_selection import KFold

X=df.loc[:,["A","A1","A2","A3"]]
predictors=X[["A1","A2","A3"]]
targets = X.A

cv = model_selection.KFold(n_splits=10)
for train_index, test_index in kf.split(X):
    print("TRAIN:", train_index, "TEST:", test_index)
    X_train, X_test = predictors[train_index], predictors[test_index]
    y_train, y_test = targets[train_index], targets[test_index]

错误:

TRAIN: [ 6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50] TEST: [0 1 2 3 4 5]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-19-1341237a8f9e> in <module>
     15 for train_index, test_index in kf.split(X):
     16     print("TRAIN:", train_index, "TEST:", test_index)
---> 17     X_train, X_test = predictors[train_index], predictors[test_index]
     18     y_train, y_test = targets[train_index], targets[test_index]
     19 

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2932                 key = list(key)
   2933             indexer = self.loc._convert_to_indexer(key, axis=1,
-> 2934                                                    raise_missing=True)
   2935 
   2936         # take() does not accept boolean indexers

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter, raise_missing)
   1352                 kwargs = {'raise_missing': True if is_setter else
   1353                           raise_missing}
-> 1354                 return self._get_listlike_indexer(obj, axis, **kwargs)[1]
   1355         else:
   1356             try:

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1159         self._validate_read_indexer(keyarr, indexer,
   1160                                     o._get_axis_number(axis),
-> 1161                                     raise_missing=raise_missing)
   1162         return keyarr, indexer
   1163 

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1244                 raise KeyError(
   1245                     u"None of [{key}] are in the [{axis}]".format(
-> 1246                         key=key, axis=self.obj._get_axis_name(axis)))
   1247 
   1248             # We (temporarily) allow for some missing keys with .loc, except in

KeyError: "None of [Int64Index([ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,\n            23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,\n            40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50],\n           dtype='int64')] are in the [columns]"

0 个答案:

没有答案