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.
#1
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.
Sequence:
112233112233112233 ......
or
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]
                        xset.append(a)
                dataX.append(xset)
                dataY.append(dataset[i + look_back,0])
        return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(1)
# load the dataset
file='test123456.xlsx'
xl=pd.ExcelFile(file)
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.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='Adam')
model.fit(trainX, 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.))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use .predict() method Jack90_ 4 699 Nov-03-2023, 08:21 PM
Last Post: Larz60+
  How to predict Total Hours needed with List as Input? caninan 0 761 Jan-27-2023, 04:20 PM
Last Post: caninan
  Sequence to sequence AI advice tomharvey 0 974 Apr-24-2022, 05:06 AM
Last Post: tomharvey
  Which network architecture should be chosen to predict disease severity? AlekseyPython 2 1,961 Aug-21-2021, 07:12 PM
Last Post: jefsummers
  How to predict output in a regression problem? Rezaafz 1 2,471 Apr-03-2021, 04:16 PM
Last Post: jefsummers
  Keras.Predict into Dataframe Finpyth 13 9,586 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  How to analyze a string, then write to SPSS Twanski94 0 1,263 Jun-16-2020, 08:38 PM
Last Post: Twanski94
  Error When Using sklearn Predict Function firebird 0 2,026 Mar-21-2020, 04:34 PM
Last Post: firebird
  Predict Longitude and Latitude Using Python vibeandvisualize 1 2,250 Dec-27-2019, 12:10 PM
Last Post: Larz60+
  How to predict with date as input for DecisionTreeRegressor sandeep_ganga 0 1,784 Dec-12-2019, 03:29 AM
Last Post: sandeep_ganga

Forum Jump:

User Panel Messages

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