<jupyter_start><jupyter_text>LetsGrowMoreVIP-Data Science Intern Author-<NAME> Stock Market Prediction And Forecasting Using Stacked LSTM(BEGINNER LEVEL TASK)<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM<jupyter_output><empty_output><jupyter_text>loading the data<jupyter_code>data = pd.read_csv('https://raw.githubusercontent.com/mwitiderrick/stockprice/master/NSE-TATAGLOBAL.csv')
data.head()
data.tail()
data.isnull().sum() # finding null value
data.describe() #looking into statistical values
data.reset_index(inplace=True) # to make index in proper order
data.head()
df1=data.reset_index()['Close']
df1.head()<jupyter_output><empty_output><jupyter_text>Plot the graph of stocks at Close<jupyter_code>plt.plot(data["Close"])
plt.xlabel('Number of datapoint')
plt.ylabel('Close')
plt.title("Stock Prediction")
plt.show()<jupyter_output><empty_output><jupyter_text>Normalization<jupyter_code>scaler=MinMaxScaler(feature_range=(0,1))
df1=scaler.fit_transform(np.array(df1).reshape(-1,1))
##splitting dataset into train and test split
training_size=int(len(df1)*0.65)
test_size=len(df1)-training_size

train_data=df1[0:training_size,:]
test_data=df1[training_size:len(df1),:1]
training_size,test_size # finding train and test size
train_data
#convert an array of values into a dataset matrix
def datasetCrtd(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)
# reshape into X=t,t+1,t+2,t+3 and Y=t+4
time_step = 100
X_train, y_train = datasetCrtd(train_data, time_step)
X_test, ytest = datasetCrtd(test_data, time_step)
print(X_train.shape), print(y_train.shape)
# reshape input to be [samples, time steps, features] which is required for LSTM
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)<jupyter_output><empty_output><jupyter_text>Build the Model<jupyter_code>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.fit(X_train,y_train,validation_data=(X_test,ytest),epochs=100,batch_size=64,verbose=1)
### Lets Do the prediction and check performance metrics
train_predict=model.predict(X_train)
test_predict=model.predict(X_test)

##Transformback to original form
train_predict=scaler.inverse_transform(train_predict)
test_predict=scaler.inverse_transform(test_predict)
### Plotting 
# shift train predictions for plotting
look_back=100
trainPredictPlot = np.empty_like(df1)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict
# shift test predictions for plotting
testPredictPlot = np.empty_like(df1)
testPredictPlot[:, :] = np.nan
testPredictPlot[len(train_predict)+(look_back*2)+1:len(df1)-1, :] = test_predict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(df1))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12723541483435943, 'fraction_numerical': 0.01817648783347992, 'mean_word_length': 8.611267605633802, 'pattern_counts': {'":': 0, '<': 22, '<?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': '576406', 'license': 'MIT', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1230, 'n_words': 223, 'path': 'Stock Market Prediction And Forecasting Using Stacked LSTM.ipynb', 'repo_name': 'RIT-MESH/LetsGrowMoreVIP-Data-Science', 'size': '3419'}