Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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?

1 Answer

0 votes
by (41.4k points)

If  you want to predict the winner based on the parameters shown in column A - N, then  define input_dim as 14, and X_train as (N,14) numpy array as follows:

[

   [9278,  37.9, ...],

   [18594, 36.3, ...],

   ...

]

So,your prediction set only contains 2 items that is two president candidates. For this you should  encode the answer Y_train in an (N,2) numpy array as follows:

 

[

   [1, 0],

   [1, 0],

   ...

   [0, 1],

   [0, 1],

   ...

]

here [1,0] shows that Barack Obama is the winner and vice versa.

Browse Categories

...