<jupyter_start><jupyter_text>Description: This model uses R.N.N to predict closing stock prices of Netflix<jupyter_code>import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense
import pandas_datareader as dr
df = dr.get_data_yahoo('NFLX' , start='2012-01-01', end = '2020-05-08')
df.head()
df.shape
# visualzing the closing price history
plt.figure(figsize=(16,8))
plt.title('Closing Price History')
plt.plot(df['Adj Close'])
plt.xlabel('Date', fontsize = 15)
plt.ylabel("Close Price USD" , fontsize = 15)
plt.show()
## create a new dataframe with only 'adj close' column
data = df[['Adj Close']]
data.head()
data.rename(columns={'Adj Close' : 'price_t'}, inplace=True)
data.head()
# Shift the price_t column by 1 index to obtain price_t-1
data['price_t-1'] = data['price_t'].shift(1)<jupyter_output>C:\Users\apurv\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy<jupyter_text>Calculating returns<jupyter_code>data['returns'] = data['price_t'].pct_change(1)
data.head()
data = data.dropna(how='any' , axis=0)
# visualzing the closing price history
plt.figure(figsize=(16,8))
plt.title('Closing Price History')
plt.plot(data['price_t'])
plt.xlabel('Date', fontsize = 15)
plt.ylabel("Close Price USD" , fontsize = 15)
plt.show()
# visualzing the closing price history
plt.figure(figsize=(12,8))
plt.title('Returns')
plt.plot(data['returns'])
plt.xlabel('Date', fontsize = 15)
plt.ylabel("Returns" , fontsize = 15)
plt.show()
expected_returns = data['returns'].mean()
expected_returns*100<jupyter_output><empty_output><jupyter_text>Expected returns are quite high<jupyter_code>data['expected_returns'] = expected_returns
data.head()
data[['returns' , 'expected_returns']].plot(figsize  = (15,12))
plt.xticks(rotation = 90)<jupyter_output><empty_output><jupyter_text>Exploring moving averages<jupyter_code>data['moving_avg_30_days'] = data['returns'].rolling(30).mean()
data['moving_avg_7_days'] = data['returns'].rolling(7).mean()
data.head()
data[30:35]
data[['returns' , 'expected_returns','moving_avg_30_days','moving_avg_7_days']].plot(figsize  = (20,15))
plt.xticks(rotation = 90)<jupyter_output><empty_output><jupyter_text>Estimating standard deviations<jupyter_code># Calculate the deviations for each return observation
data['deviations'] = data['returns'] - data['expected_returns']
# Calculate the squared deviations for each observation
data['squared_deviations'] = data['deviations'] ** 2
data.head()
# Sum of squared deviations
sum(data['squared_deviations'].dropna())
sum_squared_deviations = np.sum(data['squared_deviations'])
# Estimate the variance as the sum of squared deviations divided by N - 1,
#     where N = len(data['squared_deviations'].dropna())
var_manual = sum_squared_deviations / (len(data['squared_deviations'].dropna()) - 1)
var_manual
# Annualised Expected Return using the 'crude' method
(data['returns'].mean()) * 250
# Annualised Expected Return using the 'sophisticated' method
(1 + data['returns'].mean()) ** 250 - 1
data.rename(columns={'price_t':'Adj Close'}, inplace=True)
data.head()
## create a new dataframe with only 'adj close' column
data = data[['Adj Close']]
data.head()
# Convert the dataframe into numpy array
dataset = data.values
training_data_len = math.ceil(len(dataset)*0.8)
training_data_len<jupyter_output><empty_output><jupyter_text>Scale the data<jupyter_code>scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset)
scaled_data
# Creating a scaled training data
train_data = scaled_data[0:training_data_len , :]
#split the data into x_train and y_train
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()<jupyter_output>[array([0.00882336, 0.00844262, 0.01075689, 0.01469347, 0.01391542,
       0.01269704, 0.01269704, 0.01343535, 0.01354792, 0.01481266,
       0.01644158, 0.0153755 , 0.0132963 , 0.0128692 , 0.01365387,
       0.02059668, 0.0231725 , 0.02371548, 0.02198392, 0.02290101,
       0.02324203, 0.02404656, 0.02498022, 0.02452663, 0.02324203,
       0.02352014, 0.02321886, 0.02135486, 0.02293412, 0.02259973,
       0.02255007, 0.0225302 , 0.02105688, 0.01940147, 0.01959681,
       0.01915978, 0.01841153, 0.01970607, 0.01884856, 0.01951735,
       0.02029539, 0.01887505, 0.01765666, 0.01701436, 0.01796788,
       0.01831883, 0.01734875, 0.01739841, 0.01703422, 0.01866315,
       0.01859362, 0.02004046, 0.02026891, 0.02195081, 0.02149722,
       0.0219806 , 0.02257655, 0.02213952, 0.02152371, 0.02027884])]
[0.020275527729000754]<jupyter_text>Convert x_train and y_train into numpy array<jupyter_code>x_train , y_train  = np.array(x_train) , np.array(y_train)
### Reshaping the data
x_train = np.reshape(x_train, (x_train.shape[0] , x_train.shape[1] , 1))
x_train.shape<jupyter_output><empty_output><jupyter_text>Build LSTM Model<jupyter_code>model =Sequential()
model.add(LSTM(50 , return_sequences = True , input_shape = (x_train.shape[1] ,1)))
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(25))
model.add(Dense(1))<jupyter_output>WARNING:tensorflow:From C:\Users\apurv\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.<jupyter_text>Complie the model<jupyter_code>model.compile(optimizer = 'adam' , loss = 'mean_squared_error')<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>model.fit(x_train , y_train , batch_size=1 , epochs=5)<jupyter_output>WARNING:tensorflow:From C:\Users\apurv\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/5
1620/1620 [==============================] - 237s 146ms/step - loss: 0.0025s - loss: 0
Epoch 2/5
1620/1620 [==============================] - 234s 144ms/step - loss: 0.0012
Epoch 3/5
1620/1620 [==============================] - 232s 143ms/step - loss: 9.3804e-04
Epoch 4/5
1620/1620 [==============================] - 232s 143ms/step - loss: 6.3170e-04
Epoch 5/5
1620/1620 [==============================] - 230s 142ms/step - loss: 5.6062e-04<jupyter_text>Creating the test data<jupyter_code>test_data = scaled_data[training_data_len-60: , :]<jupyter_output><empty_output><jupyter_text>Creating x_test and y_test<jupyter_code>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 into numpy array
x_test  = np.array(x_test)
## Reshaping the data
x_test = np.reshape(x_test , (x_test.shape[0] , x_test.shape[1] , 1 ))
x_test.shape<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code>predictions = model.predict(x_test)
predictions = scaler.inverse_transform(predictions)
rmse = np.sqrt( np.mean(predictions  - y_test)**2)
rmse<jupyter_output><empty_output><jupyter_text>Plot the data<jupyter_code>train = data[:training_data_len]
valid = data[training_data_len:]
valid['Predictions'] = predictions
## visualize the data
plt.figure(figsize=(16,8))
plt.title('Model', fontsize =20)
plt.xlabel('Date', fontsize = 15)
plt.ylabel('Close Price USD' , fontsize= 15)
plt.plot(train['Adj Close'])
plt.plot(valid[['Adj Close', 'Predictions']])
plt.legend(['Train' ,'val' ,'Predictions'], loc = 'lower right')
plt.show()
valid
## Get the quote
nflx_quote = dr.get_data_yahoo('NFLX', start = '2012-01-01', end = '2020-05-05')
new_df = nflx_quote[['Adj Close']]
new_df.head()
#Get the last 60 days closing price and convert them into array
last_60_days = new_df[-60:].values
# Scale the data
last_60_days_scaled = scaler.transform(last_60_days)
x_test =[]
x_test.append(last_60_days_scaled)
## Convert it into numpy array
x_test = np.array(x_test)
# Reshape the data
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
# Get the predicted price
pred_price = model.predict(x_test)
## inverse xscaling
pred_price = scaler.inverse_transform(pred_price)
print(pred_price)
nflx_quote_new = dr.get_data_yahoo('NFLX', start = '2020-05-06', end = '2020-05-06')
print(nflx_quote_new["Adj Close"])<jupyter_output>Date
2020-05-05    424.679993
2020-05-06    434.260010
Name: Adj Close, dtype: float64
{'fraction_non_alphanumeric': 0.1553030303030303, 'fraction_numerical': 0.09982174688057041, 'mean_word_length': 6.124603174603174, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 52, 'https://': 0, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '311298', 'license': 'MIT', 'n_tokens_mistral': 3974, 'n_tokens_neox': 3312, 'n_words': 670, 'path': 'Stock_Price_Prediction_Netflix.ipynb', 'repo_name': 'appu03/Stock-Price-Prediction', 'size': '8976'}