How to input data into Keras? Specifically what is the x_train and y_train if I have more than 2 columns?

pr338 picture pr338 · Dec 24, 2015 · Viewed 18.5k times · Source

How can I input data into keras? What is the structure? Specifically what is the x_train and y_train if I have more than 2 columns?

This is the data I want to input:

enter image description here

I am trying to define Xtrain in this example Multi Layer Perceptron Neural Network code Keras has in its documentation. (http://keras.io/examples/) Here is the code:

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD

model = Sequential()
model.add(Dense(64, input_dim=20, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(2, init='uniform'))
model.add(Activation('softmax'))

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer=sgd)

model.fit(X_train, y_train, nb_epoch=20, batch_size=16)
score = model.evaluate(X_test, y_test, batch_size=16)

EDIT (additional information):

Looking here: What is data type for Python Keras deep learning package?

Keras uses numpy arrays containing the theano.config.floatX floating point type. This can be configured in your .theanorc file. Typically, it will be float64 for CPU computations and float32 for GPU computations, although you can also set it to float32 when working on the CPU if you prefer. You can create a zero-filled array of the proper type by the command

X = numpy.zeros((4,3), dtype=theano.config.floatX)

Question: Step 1 looks like create a floating point numpy array using my above data from the excel file. What do I do with the winner column?

Answer

Ian Chen picture Ian Chen · Jan 2, 2016

It all depends on your need.

It looks like that you want to predict the winner based on the parameters shown in column A - N. Then you should define input_dim to be 14, and X_train should be an (N,14) numpy array like this:

[
   [9278,  37.9, ...],
   [18594, 36.3, ...],
   ...
]

It seems that your prediction set only contains 2 items ( 2 president candidates LOL), so you should encode the answer Y_train in an (N,2) numpy array like this:

[
   [1, 0],
   [1, 0],
   ...
   [0, 1],
   [0, 1],
   ...
]

where [1,0] indicates that Barack Obama is the winner and vice versa.