Simone
Simone

Reputation: 4940

Keras, IndexError: indices are out-of-bounds

I'm trying to implement this simple neural network by Keras (Tensorflow beckend):

x_train = df_train[["Pclass", "Gender", "Age","SibSp", "Parch"]]
y_train = df_train ["Survived"]

x_test = df_test[["Pclass", "Gender", "Age","SibSp", "Parch"]]
y_test = df_test["Survived"]

y_train = y_train.values
y_test = y_test.values

But when I run this part:

model = Sequential()
model.add(Dense(input_dim=5, output_dim=1))
model.add(Activation("softmax"))

model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

model.fit(x_train, y_train)

I get this error: IndexError: indices are out-of-bounds. I am supposing that it is about the arguments in model.fit(x_train, y_train). I have tried to pass these as numpy arrays by .values, but I still have the same error.

Upvotes: 4

Views: 815

Answers (1)

indraforyou
indraforyou

Reputation: 9099

Keras expects numpy arrays not pandas, so you need to convert all of the data that you are feeding into Keras APIs.. not just y_train and y_test

So:

x_train = x_train.values
y_train = y_train.values
x_test = x_test.values
y_test = y_test.values

Or

x_train = numpy.asarray(x_train)
y_train = numpy.asarray(y_train)
x_test = numpy.asarray(x_test)
y_test = numpy.asarray(y_test)

Upvotes: 2

Related Questions