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

# +
import numpy as np 
import pandas as pd 

from subprocess import check_output
import time 
import matplotlib.pyplot as plt
from numpy import newaxis
# -

prices_dataset =  pd.read_csv('../data/raw/prices.csv', header=0)
prices_dataset

symbol = 'YHOO'
yahoo = prices_dataset[prices_dataset['symbol']==symbol]
yahoo_stock_prices = yahoo.close.values.astype('float32')
yahoo_stock_prices = yahoo_stock_prices.reshape(1762, 1)
yahoo_stock_prices.shape

plt.plot(yahoo_stock_prices)
plt.show()

train_size = int(len(yahoo_stock_prices) * 0.80)
test_size = len(yahoo_stock_prices) - train_size
train, test = yahoo_stock_prices[0:train_size,:], yahoo_stock_prices[train_size:len(yahoo_stock_prices),:]
val_size = int(train_size * 0.20)
train_size = train_size - val_size
train, val = train[0:train_size,:], train[train_size:,:]
print(len(train), len(test), len(val))


# convert series to supervised learning
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = pd.DataFrame(data)
    cols_train, names_train, cols_test, names_test = list(), list(), list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols_train.append(df.shift(i))
        names_train += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols_test.append(df.shift(-i))
        if i == 0:
            names_test += [('var%d(t)' % (j+1)) for j in range(n_vars)]
        else:
            names_test += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
    # put it all together
    agg = pd.concat(cols_train +cols_test, axis=1)
    agg.columns = names_train + names_test
    if dropnan:
        agg.dropna(inplace=True)
    X = agg[agg.columns[:n_in*data.shape[1]]]
    y = agg[agg.columns[n_in*data.shape[1]:]]
    
    # drop rows with NaN values
    
    return X, y


# reshape into X=t and Y=t+1
n_in=28
n_out=28
X_train, y_train = series_to_supervised(train, n_in, n_out)
X_test, y_test = series_to_supervised(test, n_in, n_out)
X_val, y_val = series_to_supervised(val, n_in, n_out)

X_train.shape

X_train.to_csv('../data/processed/X_train_{}.csv'.format(symbol), index=False)
X_val.to_csv('../data/processed/X_val_{}.csv'.format(symbol), index=False)
X_test.to_csv('../data/processed/X_test_{}.csv'.format(symbol), index=False)
y_train.to_csv('../data/processed/y_train_{}.csv'.format(symbol), index=False)
y_val.to_csv('../data/processed/y_val_{}.csv'.format(symbol), index=False)
y_test.to_csv('../data/processed/y_test_{}.csv'.format(symbol), index=False)
#print(X_train.shape, y_train.shape, X_eval.shape, y_eval.shape)



StarcoderdataJupyterScriptsDedupFiltered