2017-08-03 67 views
9

我試圖根據我的數據集中的部分功能來訓練Keras模型。我已經加載的數據集和提取,像這樣的特徵:熊貓 - KeyError:'[]不在索引'當培訓Keras模型

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

我錯過了什麼?

+3

檢查[此線程](https://stackoverflow.com/questions/33564181/keras-gru-nn-keyerror-when-fitting-not-in-index)。 –

回答

5

keras預計模型輸入爲numpy陣列 - 不是pandas.DataFrame s。嘗試:

X = train_data.iloc[:, 0:30].as_matrix() 
Y = train_data.iloc[:,30].as_matrix() 

由於as_matrix方法轉換pandas.DataFramenumpy.array

+0

賓果!那工作 –