![]() |
Analyze, predict the next step in a sequence. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Analyze, predict the next step in a sequence. (/thread-28553.html) |
Analyze, predict the next step in a sequence. - Antonio0608 - Jul-23-2020 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.)) |