Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - KeyError: '[] not in index' when training a Keras model

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

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

I get the following error:

KeyError: '[3 2 5 1 0 4] not in index'

What am I missing?

like image 652
Shlomi Schwartz Avatar asked Aug 03 '17 08:08

Shlomi Schwartz


People also ask

What does KeyError mean in pandas?

Pandas KeyError occurs when we try to access some column/row label in our DataFrame that doesn't exist. Usually, this error occurs when you misspell a column/row name or include an unwanted space before or after the column/row name.

Can you use pandas with keras?

tl;dr: keras-pandas allows users to rapidly build and iterate on deep learning models. Getting data formatted and into keras can be tedious, time consuming, and difficult, whether your a veteran or new to Keras.


1 Answers

keras expects model inputs to be numpy arrays - not pandas.DataFrames. Try:

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

As as_matrix method converts pandas.DataFrame to a numpy.array.

like image 59
Marcin Możejko Avatar answered Sep 20 '22 06:09

Marcin Możejko