Samael Olascoaga
Samael Olascoaga

Reputation: 161

Binary time series forecasting with LSTM in python

Hello I am working with binary time series of expression data as follows:

0: decrease expression
1: increase expression

I am training a Bidirectional LSTM network to predict the next value, but instead of giving me values ​​of 0 or 1, it returns values ​​like:

0.564
0.456
0.423
0.58

How can I get it to return 0 or 1?

this is my code:

ventana = 10
n_features = 1
neurons = 256 #155
activacion = 'softmax'
perdida = 0.25
batch_size = 32 # 32
epochs = 100 # 200
X, y = split_sequence(cierres, ventana)
X = X.reshape((X.shape[0], X.shape[1], n_features))
# define model
model = Sequential()
model.add(Bidirectional(LSTM(neurons, activation=activacion), input_shape=(ventana, n_features)))
model.add(Dropout(perdida))
model.add(Dense(1))
model.compile(optimizer='adam', loss='binary_crossentropy')
# fit model
model.fit(X, y, epochs=epochs, batch_size=batch_size, verbose=1, shuffle=False)

Upvotes: 0

Views: 2136

Answers (1)

DBat
DBat

Reputation: 588

The network is effectively performing a regression on the data, and doesn't give an exact 0 or 1. By giving a number in between, it is producing something of a degree of confidence, with number closer to 1 being more confidently a 1. To transform this, you can apply thresholding, where you round the output to 0 or 1.

import numpy as np
y_out = model.fit(...)
y_pred = np.round(y_out)

That being said, this doesn't actually minimize some kinds of loss functions. If you are being scored on a function like MSE, it is better to keep the numbers as they are.

Upvotes: 0

Related Questions