<jupyter_start><jupyter_text>Author : <NAME> VIP - Data Science September-2021 Task 2 : Stock Market Prediction And Forecasting Using Stacked LSTM Dataset Link : https://raw.githubusercontent.com/mwitiderrick/stockprice/master/NSE-TATAGLOBAL.csv Importing required Libraries<jupyter_code>#Importing Libraries1
%time
import tensorflow as tf # This code has been tested with Tensorflow
import tensorflow_datasets as tfds
from tensorflow import keras
import math
import pandas as pd
import pandas_datareader as web
import numpy as np
import datetime as dt
import urllib.request, json
import os

import matplotlib.pyplot as plt
%matplotlib inline

import seaborn as sns

from keras.models import Sequential
from keras.layers import Dense,LSTM

import nltk
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import subjectivity
from nltk.sentiment import SentimentAnalyzer
from nltk.sentiment.util import *

from sklearn import preprocessing, metrics
from sklearn.preprocessing import MinMaxScaler
plt.style.use('fivethirtyeight')
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Importing dataset<jupyter_code>df = web.DataReader('AAPL', data_source='yahoo', start='2012-01-01', end='2019-12-17')
df = pd.read_csv("NSE-TATAGLOBAL.csv")
df<jupyter_output><empty_output><jupyter_text>Inspecting Data<jupyter_code>df.head()
df.shape
df.info()
df.describe()
df.isnull().sum()
df.dropna(inplace = True, how = 'all')
df.isnull().sum()
# number of records in both stock Market Prediction and Forecasting Using Stacked LSTM
len(df)
# checking for null values in both the datasets
df.isna().any()
df['Data'] = pd.to_datetime(df['Date']).dt.normalize()

#flitering the important columns requried
df = df.filter(['Date', 'Close', 'Open', 'high', 'Low', 'Volume'])

# setting column 'Date' as the index column
df.set_index('Date', inplace = True)

# sorting the data according to the index i.e Date'
df = df.sort_index(ascending = True, axis = 0)
df<jupyter_output><empty_output><jupyter_text>Data Visualization<jupyter_code>plt.figure(figsize=(10,10))
plt.subplot(1,2,1)
plt.plot(df['Open'],'b')
plt.title("Open Price")
plt.ylabel("Price")

plt.subplot(1,2,2)
plt.plot(df['Close'],'r')
plt.title("Close Price")
plt.ylabel("Price")
plt.show()<jupyter_output><empty_output><jupyter_text>200 Exponential Moving Average (EMA) and 200 Simple Moving Average (SMA)<jupyter_code>df.ewm(span=200).mean()['Close'].plot(figsize = (15,15), label = '200 EMA')
df.rolling(window=200).mean()['Close'].plot(figsize=(15,15), label = '200 SMA')
df['Close'].plot(label = 'Close')
plt.legend()
plt.ylabel('Price')
plt.show()
training_orig = df.loc[:, ['Close']]
training_orig
training_orig['Close'].plot()
# setting figure size
plt.figure(figsize=(16,10))

# plotting close price
df['Close'].plot()

# setting plot tittle x and y labels
plt.title("Close Price")
plt.xlabel('Date')
plt.ylabel('Close Price ($)')
plt.figure(figsize=(16,8))
plt.title('Close Price History')
plt.plot(df['Close'])
plt.xlabel('Date', fontsize = 18)
plt.ylabel('Close Price USD($)', fontsize = 18)
plt.show()
data = df.filter(['Close'])
dataset = data.values
training_data_len = math.ceil(len(dataset) * .8)

#scale the data
scaler=MinMaxScaler(feature_range=(0,1))
scaled_data=scaler.fit_transform(dataset)

scaled_data
# Create training data set

train_data = scaled_data[0:training_data_len,:]

#split the data into x_train and y_train data sets

x_train = []
y_train = []

for i in range(60,len(train_data)):
    x_train.append(train_data[i-60:i,0])
    y_train.append(train_data[i,0])
    if i <= 60:
        
        print(x_train)
        print(y_train)
        print()
#convert train data to numpy arrays
x_train, y_train = np.array(x_train),np.array(y_train)

#Reshape
x_train.shape
x_train = np.reshape(x_train,(x_train.shape[0], x_train.shape[1],1))
x_train.shape
# calculating 7 days moving avaerage
df.rolling(7).mean().head(20)
#setting figure size
plt.figure(figsize = (16,10))

# plotting the close price and a 30 days rolling mean of close price
df['Close'].plot()
df.rolling(window=30).mean()['Close'].plot()
# displaying df
df
# calculating data_to_use
percentage_of_data = 1.0
data_to_use = int(percentage_of_data*(len(df)-1))

# using 80% of data for training
train_end = int(data_to_use*0.8)
total_data = len(df)
start = total_data - data_to_use

# printing number of records in the training and test datasets
print("Number of records in Training Data:", train_end)
print("Number of records in Test Data:", total_data - train_end)
from sklearn.preprocessing import MinMaxScaler

sc = MinMaxScaler(feature_range=(0,1))
training = sc.fit_transform(np.array(training_orig['Close']).reshape(-1,1))
print(training.shape)
train_size = int(len(training)*0.65)

train = training[0:train_size]
test = training[train_size:]

print(train.shape)
print(test.shape)
test_plot = np.empty_like(training)
test_plot[: , :] = np.nan
test_plot[len(train): , :] = test

plt.figure(figsize = (20,20))
plt.plot(train, 'red', label = 'train')
plt.plot(test_plot, 'blue', label='test')
plt.legend(loc='upper left')
def helper(dataset, timestep):
    x=[]
    y=[]
    for i in range(len(dataset)-timestep-1):
        x.append(dataset[i:(i+timestep),0])
        y.append(dataset[i+timestep, 0])
    return np.array(x), np.array(y)
x_train, y_train = helper(train,100)
x_test, y_test = helper(test,100)
x_train.shape, y_train.shape
x_test.shape, y_test.shape
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1)
x_test.shape
# build LSTM Model
model=Sequential()
model.add(LSTM(50,return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(LSTM(50,return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))
#copile the model
model.compile(optimizer='adam', loss='mean_squared_error')
#train the model
model.fit(x_train,y_train,batch_size=1,epochs=1)
# copile the model
model.compile(optimizer='adam', loss='mean_squared_error')
#create the testing dataset
#create a new array containing scaled values from index 1543 to 2003
test_data = scaled_data[training_data_len - 60:,:]
# create the data sets x_test and y_test
x_test=[]
y_test=dataset[training_data_len:,:]
for i in range(60,len(test_data)):
    x_test.append(test_data[i-60:i,0])
#convert the data to a numpy array
x_test=np.array(x_test)
#reshape
x_test=np.reshape(x_test,(x_test.shape[0], x_test.shape[1],1))
#get the models predicited price values
predictions=model.predict(x_test)
predictions=scaler.inverse_transform(predictions)
#Get the root mean squared error(RMSE)
rmse=np.sqrt( np.mean((predictions - y_test)**2))
rmse
train=data[:training_data_len]
valid=data[training_data_len:]
valid['Predictions']=predictions

#visualize the data
plt.figure(figsize=(16,8))
plt.title('Model')
plt.xlabel('Date', fontsize=18)
plt.ylabel('Close Price USD($)', fontsize=18)
plt.plot(train['Close'])
plt.plot(valid[['Close', 'Predictions']])
plt.legend(['Train', 'Val', 'Predictions'],loc='lower right')
plt.show()
#show the valid and predicted prices
valid<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14450217666058138, 'fraction_numerical': 0.023592192107850022, 'mean_word_length': 6.576595744680851, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '588757', 'license': 'MIT', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2550, 'n_words': 579, 'path': '01 Beginner Level Task/Task 2-Stock Market Prediction And Forecasting Using Stacked LSTM/Beginner Level Task 2-Stock Market Prediction And Forecasting Using Stacked LSTM.ipynb', 'repo_name': 'akashloka/LGMVIP-DataScience', 'size': '7135'}