Milano
Milano

Reputation: 18705

Keras - how to predict two values instead of one value?

I'm learning machine learning and my dataset consists of 7 columns:

home_team, away_team, home_odds, away_odds, home_score, away_score, 1_if_home_wins_else_0

To be able to feed the Tensorflow with teams, I converted every team to integer so the first two columns are integers (like database ids)

There is a 10k rows in the csv.

example

enter image description here

I modified the code for pima indians diabetes to predict winnings of home team.

So now it "predicts" whether the home team win (1) otherwise 0.

Now I would like to modify the algorithm to predict exact score home_score, away_score. I know the outputs will be wrong it's just learning.

code

# load the dataset
dataset = loadtxt('football_data.csv', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:, 0:4]
y = dataset[:, 6]
# define the keras model
model = Sequential()
model.add(Dense(12, input_dim=4, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=10)
# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy * 100))

# make class predictions with the model
predictions = model.predict_classes(X)
# summarize the first 5 cases
for i in range(50):
    print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))

Do you know how to do that?

Upvotes: 2

Views: 4644

Answers (1)

desertnaut
desertnaut

Reputation: 60321

Since you now want to predict scores, i.e. a continuous quantity (although integer), this is no more a classification problem but a regression one.

To do that, you need two changes in your existing model; the first is to modify your final layer to

model.add(Dense(2)) # final layer

i.e. 2 nodes (as many as the required outputs) without any activation (which means the default linear, i.e. the one we use for regression).

The second modification should be in the loss:

model.compile(loss='mean_squared_error', optimizer='adam')

You need of course to modify accordingly your y data in order to contain 2 elements each, and use model.predict instead of model.predict_classes (since you don't have classes now).

What you ask for is essentially a multi-output regression; see also this recent thread: How to train a Regression model for single input and multiple output? - it may be better indeed to use the functional Keras API, as shown there and already suggested in the comments.

Upvotes: 3

Related Questions