Simple keras lstm example

It is the sequence of the problems that can store the information. The process is done by importing numpy computation and matplotlib for plotting the graphs and manipulating datasets. The Keras is a network library written in Python that has a high-level which makes it simple and intuitive to use. It will work as a wrapper to low-level libraries. They are designed to enable fast experimentation with deep neural networks and focuses on being user-friendly, modular, and extensible. That supports convolution networks and recurrent networks as well as combinations of the two.
Also runs seamlessly on CPU and GPU.

We need to import a couple of modules from Keras,

  1. Sequential for initializing the neural network
  2. Dense for adding a densely connected neural network layer
  3. LSTM for adding the Long Short-Term Memory layer
  4. Dropout for adding dropout layers that prevent overfitting

LSTM:-
Import numpy as np
Import matplotlib.pyplot as plt
Import pandas as pd

Simple Keras lstm Example:-

Import pandas
Import matplotlib.pyplot as plt
From sklearn.preprocessing import MinMaxScaler
Import numpy as np
From keras.models import sequential
From keras.layers import dense
From keras, layer import LSTM
Import math
From sklearn.metrics import mean_squred_error

Explanation:-

In the first, line the pandas are imported and then the library is used to visualize the data as well used for manipulation of data.
In the second line, we need to show the graph at the end between the predicted value and actual value so matplotlib is imported that performs such act. plt.
And used to plot both the predicted as well as actual values
From 3rd step, the Sklearn is a library which is used for splitting the dataset into training and testing phase.
Also used to normalize the dataset and the Normalization is needed so that every value/column is set to a specified range and machine can learn dataset more accurately.
 We need to convert the dataframe into a matrix hence numpy is used.
They will converts data into array and these arrays are fed to a machine for training purposes.
Keras will support two types of models one is the sequential and other is functional.
Sequential model is imported from Keras layers.

Stock using the test set:-

We need to import the test set and use to make predictions,
Dataset_test=pd.read_csv(‘tatatest.csv’)
Real_stock_price=dataser_test.iloc[:.1:2]
 
 We need to do a couple of things after loading in the test set:-

  1. We have to merge the training set and the test set on the 0 axes.
  2. Then Set the time step as 60.
  3. Then we Use MinMaxScaler to transform the new dataset
  4. Reshape the dataset

Dataset_total=pd.concat ((dataset_train [‘Open’], dataset_test [‘open’], axis=0)
Inputs=dataset_total [len (dataset_total)-len (dataset_test)-60:].values
Inputs=inputs. reshape (-1, 1)
Inputs=sc.transform (inputs)
X_test= []
For I in range (60, 76):
X_test.append (inputs [i-60: I, 0])
X_test=np.array (x_test)
X_test=np.reshape (x_test, (X_test.shape [0], x_test.shape [1], 1))
Predicted_stock_price=regressor.predict (X_test))
Predicted_stock_price=sc.inverse_transform (predicted_stock_price)

Plotting the Result:-

 
Matplotlib is used to visualize the result of the predicted stock price and the real stock price.
Plt.plot (real_stock_price, color=’black’, label=’TATA Stock Price’)
Plt.plot (predicted_stock_price, color=’green’, label=’Predicted TATA Stock Price’)
Plt.title (‘Time’)
Plt.ylabel (‘TATA Stock Price’)
Plt.legend () Plt.show ()

simple keras lstm example

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India