Shamoon
Shamoon

Reputation: 43491

How can I use tensorflow one hot encoding with a keras based neural network?

My code is:

from keras.models import Sequential
from keras.layers import Dense
import numpy
import pandas as pd

X = pd.read_csv(
    "data/train.csv", header=0, usecols=['Type', 'Age', 'Breed1', 'Breed2', 'Gender', 'Color1', 'Color2', 'Color3', 'MaturitySize', 'FurLength',    'Vaccinated',   'Dewormed', 'Sterilized',   'Health',   'Quantity', 'Fee', 'VideoAmt', 'PhotoAmt'])
Y = pd.read_csv(
    "data/train.csv", header=0, usecols=['AdoptionSpeed'])

Y = Y['AdoptionSpeed'].apply(lambda v: v / 4)

model = Sequential()
model.add(Dense(18, input_dim=18, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, epochs=150, batch_size=100)
scores = model.evaluate(X, Y)

However, Type can either be 1 or 2, so I think it makes sense to use one hot encoding for that. The same is true of Breed1, Breed2, etc.

It looks like I can do some sort of encoding with:

typehot = tf.one_hot([0, 1])

But that seems to not do very much and secondly, how do I put it as an input into the network?

Upvotes: 1

Views: 489

Answers (1)

gorjan
gorjan

Reputation: 5555

I wouldn't use the one hot encoding method from Tensorflow. Because I can see that you are loading the dataset with Pandas, why just not use:

X = pd.get_dummies(X, columns=["Type", "Breed1", "Breed2"])

Then just train the network in the same as as you are doing it now.

Upvotes: 1

Related Questions