简体   繁体   中英

Predict the next outcome MLP neural network Python

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

在此处输入图像描述

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.

在此处输入图像描述

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM