# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt


#Extracting DataFrame From Web :
import pandas_datareader as web

#Stock Symbol Used : Alphabet A Inc - > Google 
# From 1 April 2020 to 30 June 2020
#This data is full collection of all stock features from Google's Stock Market
df_g=web.DataReader('GOOGL',data_source='yahoo',start='04-01-2008',end='07-15-2020')

df_g.info()

df_g.describe()


# +

def plot_series(time, series, format="-", start=0, end=None):
    plt.plot(time[start:end], series[start:end], format)
    plt.xlabel("Time")
    plt.ylabel("Value")
    plt.grid(True)
# -





df_g
#Indicating Number Of days and features -> 123 days and 6 stock features

# +

temps=[]
time_step=[]

step=0
for row in range(3096):
      time_step.append(step)
      step = step + 1

ds = np.array(df_g['Close'].values)
time = np.array(time_step)
plt.figure(figsize=(10, 6))
plot_series(time, ds)


# -

ds_g = ds.reshape(-1,1)
print(ds_g.shape)

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0,1))
series = scaler.fit_transform(ds_g)

# +
split_time = 2500
time_train = time[:split_time]
x_train = series[:split_time]
time_valid = time[split_time:]
x_valid = series[split_time:]

window_size = 30
batch_size = 32
shuffle_buffer_size = 1000

# -

def windowed_dataset(series, window_size, batch_size, shuffle_buffer):
    ds = tf.data.Dataset.from_tensor_slices(series)
    ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(window_size + 1))
    ds = ds.shuffle(shuffle_buffer)
    ds = ds.map(lambda w: (w[:-1], w[-1:]))
    return ds.batch(batch_size).prefetch(1)


def windowed_valid_dataset(series, window_size, batch_size):
    ds = tf.data.Dataset.from_tensor_slices(series)
    ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(window_size + 1))
    ds = ds.map(lambda w: (w[:-1], w[-1:]))
    return ds.batch(batch_size).prefetch(1)


def model_forecast(model, series, window_size):
    ds = tf.data.Dataset.from_tensor_slices(series)
    ds = ds.window(window_size, shift=1, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(window_size))
    ds = ds.batch(32).prefetch(1)
    forecast = model.predict(ds)
    return forecast


train_set = windowed_dataset(x_train, window_size, batch_size, shuffle_buffer_size)

print(train_set)

#Checking The Batches And their corresponding or respective label ,x and y 
for x,y in train_set:
    print("x :",x.numpy())
    print("y :",y.numpy())

x[0],y[0]   #very First Set of batch of 64 values window and its respective 65th next value or label of first set..











# +
tf.keras.backend.clear_session()
tf.random.set_seed(51)
np.random.seed(51)
window_size = 64
batch_size = 256
train_set = windowed_dataset(x_train, window_size, batch_size, shuffle_buffer_size)
print(train_set)
print(x_train.shape)

model = tf.keras.models.Sequential([
  tf.keras.layers.Conv1D(filters=32, kernel_size=5,
                      strides=1, padding="causal",
                      activation="relu",
                      input_shape=[None, 1]),
  tf.keras.layers.LSTM(64, return_sequences=True),
  tf.keras.layers.LSTM(64, return_sequences=True), 
  tf.keras.layers.Dense(30, activation="relu"),
  tf.keras.layers.Dense(10, activation="relu"),
  tf.keras.layers.Dense(1),
  tf.keras.layers.Lambda(lambda x: x * 400)
])

optimizer = tf.keras.optimizers.SGD(lr=1e-6, momentum=0.9)
model.compile(loss=tf.keras.losses.Huber(),
              optimizer='adam',
              metrics=["mse"])
history = model.fit(train_set,epochs=100)

# -



# +
#plt.semilogx(history.history["lr"], history.history["loss"])
#plt.axis([1e-8, 1e-4, 0, 60])
# -

window_size1 = 64
batch_size1 = 256

pd=model_forecast(model,series,window_size1)

pd.shape

pd=pd[split_time - window_size:-1, -1, 0]

pd.shape

plot_series(time_valid, x_valid)
plot_series(time_valid,pd)

rnn_forecast = model_forecast(model, series, window_size)
rnn_forecast = rnn_forecast[split_time - window_size:-1, -1, 0]

print(rnn_forecast.shape)
print(x_valid.shape)
y=np.array(x_valid)
print(x_valid.shape)

plt.figure(figsize=(25, 6))
plot_series(time_valid, x_valid)
plot_series(time_valid, rnn_forecast)

tf.keras.metrics.mean_absolute_error(x_valid, rnn_forecast).numpy()

print(rnn_forecast)
print(x_valid)

StarcoderdataJupyterScriptsDedupFiltered