<jupyter_start><jupyter_text>TASK 2: Stock Market Prediction and Foecasting Using Stacked LSTM LGMVIP  by <NAME> import libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>load dataset<jupyter_code>url="https://raw.githubusercontent.com/mwitiderrick/stockprice/master/NSE-TATAGLOBAL.csv"
dataset_train=pd.read_csv(url)
training_set= dataset_train.iloc[:,1:2].values<jupyter_output><empty_output><jupyter_text>first five values of the dataset<jupyter_code>dataset_train.head()
data_close=dataset_train["Close"]
data_close
plt.plot(data_close)<jupyter_output><empty_output><jupyter_text>DATA NORMALISATION<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler= MinMaxScaler(feature_range = (0,1))
data_close = scaler.fit_transform(np.array(data_close).reshape(-1,1))
data_close.shape
print(data_close)<jupyter_output>[[0.62418301]
 [0.62214052]
 [0.62622549]
 ...
 [0.1621732 ]
 [0.16319444]
 [0.16584967]]<jupyter_text>TRAIN AND TEST SPLIT<jupyter_code>training_size = int(len(data_close)*0.75)
test_size = len(data_close) - training_size
train_data, test_data= data_close[0:training_size,:], data_close[training_size:len(data_close),:1]
def create_dataset(dataset, time_step = 1):
    datax,datay= [],[]
    for i in range (len(dataset)-time_step - 1):
        a = dataset[i:(i+time_step),0]
        datax.append(a)
        datay.append(dataset[i+time_step,0])
    return np.array(datax), np.array(datay)
time_step=100
x_train,y_train = create_dataset(train_data,time_step)
x_test,y_test = create_dataset(test_data,time_step)
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)
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dropout
from keras.layers import Dense
model = Sequential()
model.add(LSTM(50,return_sequences =True, input_shape = (100,1)))
model.add(LSTM(50,return_sequences = True))
model.add(LSTM(50))
model.add(Dense(1))
model.compile(loss= 'mean_squared_error', optimizer ='adam')
model.summary()

model.summary()
model.fit(x_train,y_train, validation_data = (x_test, y_test), epochs = 100,batch_size = 64, verbose = 1)

train_predict = model.predict(x_train)
test_predict = model.predict(x_test)
train_predict = scaler.inverse_transform(train_predict)
test_predict = scaler.inverse_transform(test_predict)
import math
from sklearn.metrics import mean_squared_error
math.sqrt(mean_squared_error(y_train, train_predict))
math.sqrt(mean_squared_error(y_test, test_predict))
look_back = 100
trainPredictPlot=np.empty_like(data_close)
trainPredictPlot[:,:] = np.nan
trainPredictPlot[look_back:len(train_predict)+look_back,:]=train_predict

testPredictPlot = np.empty_like(data_close)
testPredictPlot[:,:] = np.nan
testPredictPlot[len(train_predict)+(look_back*2)+1:len(data_close)-1, :] =test_predict

plt.plot(scaler.inverse_transform(data_close))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()<jupyter_output><empty_output><jupyter_text>PREDICTING STOCK FOR THE NEXT 30 DAYS<jupyter_code>len(test_data), x_test.shape
x_input = test_data[409:].reshape(1,-1)
x_input.shape
temp_input =list(x_input)
temp_input = temp_input[0].tolist()
lst_output= []
n_steps = 100
nextNumberOfDays = 30
i=0

while(i<nextNumberOfDays):
    
    if(len(temp_input)>100):
        x_input = np.array(temp_input[1:])
        print("{} day input {} ".format(i,x_input))
        x_input = x_input.reshape(1,-1)
        x_input = x_input.reshape(1, n_steps, 1)
        yhat = model.predict(x_input, verbose= 0)
        print("{} day output {} ".format(i,yhat))
        temp_input.extend(yhat[0].tolist())
        temp_input=temp_input[1:]
        lst_output.extend(yhat.tolist())
        i=i+1
    else:
        x_input = x_input.reshape((1,n_steps,1))
        yhat = model.predict(x_input,verbose= 0)
        print(yhat[0])
        temp_input.extend(yhat[0].tolist())
        print(len(temp_input))
        lst_output.extend(yhat.tolist())
        i=i+1
        
        
print(lst_output)
day_new= np.arange(1,101)
day_pred = np.arange(101,131)
df3= data_close.tolist()
df3.extend(lst_output)
len(data_close)
plt.plot(day_new,scaler.inverse_transform(data_close[1935:]))
plt.plot(day_pred,scaler.inverse_trans<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13337924701561066, 'fraction_numerical': 0.03420569329660239, 'mean_word_length': 6.273789649415693, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '128761', 'license': 'MIT', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1653, 'n_words': 235, 'path': 'Task - 2/TASK2- STOCK PREDICTION.ipynb', 'repo_name': 'swethachepuri777/LGMVP---DATA-SCIENCE', 'size': '4364'}