0

I am trying to predict the next set of numbers in my dataset sequence however using the predict function classifys the entire dataset, how can I change my code so that it predicts the next outcome in the sequence?

I was following this tutorial and his model outputs 80 based on 50,60,70 as his dataset. Yet mine just predicts the entire dataset? How to Get Started with Deep Learning for Time Series Forecasting (7-Day Mini-Course)

This is my dataset

enter image description here

and this is the code:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report,confusion_matrix

from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
# load the dataset
col_names = ['N1', 'N2', 'N3', 'N4', 'N5', 'L1', 'L2','label']
# load dataset 
pima = pd.read_csv("dataset.csv", header=None, names=col_names)
pima.head()

feature_cols = ['N1', 'N2', 'N3', 'N4', 'N5', 'L1', 'L2']
X = pima[feature_cols] # Features
y = pima.label 

model = Sequential()
model.add(Dense(122, input_dim=7, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=2000, batch_size=10)
# =======================
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

yhat = model.predict(X, verbose=0)
print(yhat) <- this outputs the predictions for the entire dataset and not the next prediction

Edit:

The output I am getting is this, for the entire dataset.

enter image description here

The dataset runs from 1 to 1251 rows and I want to predict row 1252 with an output of N1,N2,N3,N4,N5,L1,L2.

G Gr
  • 6,030
  • 20
  • 91
  • 184

1 Answers1

0

You have sigmoid activation as your final layer, which gives outputs in range [-1, 1], which is not what you want for your final layer as you are predicting the next number.

model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

try changing this to

model.add(Dense(1))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

Also, you better use MSE loss function for this type of tasks, BCE is used more for classification tasks, you can check that here

Why is the Cross Entropy method preferred over Mean Squared Error? In what cases does this doesn't hold up?

model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])

hope this solves your problem

MadLordDev
  • 260
  • 2
  • 12
  • I dont think thats it, I have updated my question. The `model.predict` is outputting its predictions for the entire dataset as if its classifying and not actually predicting the next sequence of numbers? – G Gr Sep 26 '19 at 23:01
  • Using your answer I also get this error `TypeError: '(0, slice(None, None, None))' is an invalid key` – G Gr Sep 26 '19 at 23:02
  • 1
    I just said about input matrix vs vector, I will test the code now and get back to you – MadLordDev Sep 26 '19 at 23:07
  • I understand, the dataset runs from 1 to 1251 rows and I want to predict row 1252 with an output of N1,N2,N3,N4,N5,L1,L2 however I cant figure out 1) why its classifying my entire dataset and 2) how to output a matrix like you said. I was assuming I need to figure out why number 1) is happening first? – G Gr Sep 26 '19 at 23:10
  • 1
    Oo I see, sorry I didnt pay attention to that, I will edit my answer. – MadLordDev Sep 26 '19 at 23:13
  • Its still classifying all of X? Its not predicting row 1252? – G Gr Sep 26 '19 at 23:27
  • Can you give me some part of output? – MadLordDev Sep 26 '19 at 23:29
  • `yhat = model.predict(X, verbose = 0) print(yhat) [[0.01997364] [0.10846496] [0.07146579] ... [0.45351756] [0.05341327] [0.20674002]]` – G Gr Sep 26 '19 at 23:30
  • Did you remove sigmoid from the last layer? – MadLordDev Sep 26 '19 at 23:32
  • The output of yhat prediction should be Row 1252 (or just one row): 2, 18, 26, 48, 50, 1, 11. It should not be classifying all of X which it seems to be doing? Yes I removed sigmoid. – G Gr Sep 26 '19 at 23:32