pandas -KeyError:训练Keras模型时'[]不在索引中' [英] Pandas - KeyError: '[] not in index' when training a Keras model
本文介绍了 pandas -KeyError:训练Keras模型时'[]不在索引中'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据数据集中的部分特征训练Keras模型.我已经加载了数据集并提取了如下功能:
I'm trying to train a Keras model based on partial features from my data set. I've loaded the data set and extracted the features like so:
train_data = pd.read_csv('../input/data.csv')
X = train_data.iloc[:, 0:30]
Y = train_data.iloc[:,30]
# Code for selecting the important features automatically (removed) ...
# Selectintg important features 14,17,12,11,10,16,18,4,9,3
X = train_data.reindex(columns=['V14','V17','V12','V11','V10','V16','V18','V4','V9','V3'])
print(X.shape[1]) # -> 10
但是当我调用fit方法时:
But when I'm calling the fit method:
# Fit the model
history = model.fit(X, Y, validation_split=0.33, epochs=10, batch_size=10, verbose=0, callbacks=[early_stop])
我收到以下错误:
KeyError: '[3 2 5 1 0 4] not in index'
我想念什么?
推荐答案
keras
期望模型输入是numpy
数组,而不是pandas.DataFrame
s.试试:
keras
expects model inputs to be numpy
arrays - not pandas.DataFrame
s. Try:
X = train_data.iloc[:, 0:30].as_matrix()
Y = train_data.iloc[:,30].as_matrix()
由于as_matrix
方法将pandas.DataFrame
转换为numpy.array
.
这篇关于 pandas -KeyError:训练Keras模型时'[]不在索引中'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文