Python Forum
Analyze, predict the next step in a sequence.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Analyze, predict the next step in a sequence.
Hi, I am communicating through a translator. Do not swear too much for this.
I have a question.
How can you implement a neural network. To analyze and predict the next step of the sequence.
Not a great example.
112233112233112233 ......
111211312111211312 ......
You can take any sequence.
But here's how to teach a neural network to predict the next step in a sequence.
Even in such simple sequences as in the example.
I have the following code.
But this is a neural network. Doesn't predict the next step. And repeats the previous ones.
How can this be fixed?
import numpy
import pandas as pd
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import accuracy_score
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back):
        dataX, dataY = [], []
        for i in range(len(dataset)-look_back-1):
                xset = []
                for j in range(dataset.shape[1]):
                        a = dataset[i:(i+look_back), j]
                dataY.append(dataset[i + look_back,0])
        return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
# load the dataset
dataframe = xl.parse('Sheet1')
dataset = dataframe.values
dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0,1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.75)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:],dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0],1,trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0],1,testX.shape[1]))                            
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(8, input_shape=(1, look_back)))
model.compile(loss='mean_squared_error', optimizer='Adam'), trainY, epochs=10000, batch_size=1, verbose=2)
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
print("X=%s, Predicted=%s" % (testPredict[-1],testX[-1]))
print("X=%s, Predicted=%s" % (testPredict[0],testX[0]))
By changing the settings of this neural network. Does not improve results.
I will be grateful for any help.
P.s. I'm learning the language.))

Possibly Related Threads…
Thread Author Replies Views Last Post
  Which network architecture should be chosen to predict disease severity? AlekseyPython 2 226 Aug-21-2021, 07:12 PM
Last Post: jefsummers
  How to predict output in a regression problem? Rezaafz 1 609 Apr-03-2021, 04:16 PM
Last Post: jefsummers
  Keras.Predict into Dataframe Finpyth 13 3,238 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  How to analyze a string, then write to SPSS Twanski94 0 480 Jun-16-2020, 08:38 PM
Last Post: Twanski94
  Error When Using sklearn Predict Function firebird 0 834 Mar-21-2020, 04:34 PM
Last Post: firebird
  Predict Longitude and Latitude Using Python vibeandvisualize 1 906 Dec-27-2019, 12:10 PM
Last Post: Larz60+
  How to predict with date as input for DecisionTreeRegressor sandeep_ganga 0 759 Dec-12-2019, 03:29 AM
Last Post: sandeep_ganga
  Can someone explain how does svr_rbf.predict(dates) work? j2ee 0 2,301 Feb-22-2018, 06:50 PM
Last Post: j2ee
  Model.predict() always returning the same value of 1 for opencv nastyheatnor 0 3,882 Dec-14-2017, 08:20 AM
Last Post: nastyheatnor
  how to predict next value taking in account several variables? drogontargaryen 2 2,784 Aug-23-2017, 02:16 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020