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

我与影子孤独终老i 提交于 2020-08-01 07:04:08

问题


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:

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:


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.



来源:https://stackoverflow.com/questions/34448582/how-to-input-data-into-keras-specifically-what-is-the-x-train-and-y-train-if-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!