How can I run test data against my keras trained model?

Viewed 8944

The code to train my model 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'])

X = pd.get_dummies(X, columns=["Type", "Breed1",
                               "Breed2", 'Color1', 'Color2', 'Color3', 'Gender', 'MaturitySize', 'FurLength'])
print(X)

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

input_units = X.shape[1]

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

I have a file called test.csv. How can I test that set against the model to see how effective my model is?

It seems to have a 97% accuracy with the training data, but I'm concerned it may be overfitting.

2 Answers

In order to pick the best model to evaluate it on the test set you should firstly split the training set into training and validation set. Then you iteratively train and validate the model. The Keras fit method does that automatically for you.

model.fit(X, Y, epochs=250, batch_size=1000, validation_split=0.2)

As you can see, this will allocate 20% of the training set to be used as validation of the model.

Then, you should use the test.csv file you have only to measure how good the model you picked is. However, never do model selection with the test set. The test set is used so you can make an unbiased estimate of how good your model will perform in the real world.

Then I would load the test.csv file and use:

model.evaluate(x=X_test, y=Y_test)

Otherwise, if you just want to perform inference on the test set you can do:

predictions = model.predict(X_test)

This method will return the predictions for your test set.

You can do it same as your training approach as follow:

f = "test.csv"
X = pd.read_csv(
    f, 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(
    f, header=0, usecols=['AdoptionSpeed'])

X = pd.get_dummies(X, columns=["Type", "Breed1",
                               "Breed2", 'Color1', 'Color2', 'Color3', 'Gender', 'MaturitySize', 'FurLength'])
print(X)

Y = Y['AdoptionSpeed'].apply(lambda v: v / 4)
scores = model.evaluate(X, Y)
Related