Thursday, 28 January 2021

test flights

 expense
Jan 29 - driving 200km
Jan 30 - driving 200km, fuel, jeffery's dinner, dollarama
Jan 31 - driving 200km, fuel x 2, field dinner, hard drive
Feb 1 - driving 200km, fuel, hard drive, canadian tire
Feb 2 - driving 160km

time sheet
Jan 28 -  build mount - 8h - document review 2h
Jan 29 - install old new tabi - 14h
Jan 30 - trouble shoot new tabi shutter problem - 8h, bundle flight old, new tabi - 8h
Jan 31 - medicine hat survey 8h, documentation + backup 8h
Feb 1 - install uv 8h, bundle, water bath, power line 8h
Feb 2 - install new tabi + uv - 8h

THE GAMESTONK

Tuesday, 26 January 2021

keras 18 RNN generate music

 
reference:

music vocab

install music21

pip install music21

#in pycharm type following code,
from music21 import *
configure.run()

# press enter for all default option -> install MuseScore 3
#in pycharm type
from music21 import *
s = corpus.parse('bach/bwv65.2.xml')
s.show()
#musescore3 opens -> click play music

Monday, 25 January 2021

Chinese Auto Show 2021 Models



keras 17 RNN predict stock price 2


apple stock zoomed in view


tesla stock zoomed in view









#stock_xsample.py
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
from tensorflow.keras.models import load_model
import matplotlib.dates as mdates
import pandas_datareader.data as web
import datetime as dt

def predict_stock_trend(ticker, start, end, train_sample, train_epoch, forecast_days):

    df = web.DataReader(ticker, 'yahoo', start, end)

    data = df['Adj Close'].values
    #print(data)

    #scale input between 0, 1
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(data.reshape(-1, 1))
    #print(scaled_data)
    #print(scaled_data.shape[0])

    x = []
    y = []
    sample = train_sample

    #y lags x by n samples
    for i in range(sample, scaled_data.shape[0]):
        x.append(scaled_data[i-sample:i, 0])
        y.append(scaled_data[i, 0])

    x, y = np.array(x), np.array(y)

    x = np.reshape(x, (x.shape[0], x.shape[1], 1))
    #print(x.shape)
    #(1208, n, 1)
    #print(x[0])
    print(y.shape)
    #print(y)

    #train on GPU
    pysical_devices = tf.config.experimental.list_physical_devices('GPU')
    #print("Num GPUs Available: ", len(pysical_devices))
    tf.config.experimental.set_memory_growth(pysical_devices[0], True)

    model = Sequential()
    model.add(LSTM(128, input_shape=(sample, 1), return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(128, return_sequences=True))
    model.add(Dropout(0.1))

    model.add(LSTM(128, return_sequences=True))
    model.add(Dropout(0.1))

    model.add(LSTM(128))
    model.add(Dropout(0.1))

    model.add(Dense(1))
    model.summary()

    model.compile(
        loss='mean_squared_error',
        optimizer='adam',
    )

    model.fit(x,
              y,
              epochs=train_epoch,
              batch_size=32)

    model.save("stock_xsample.h5")

    x_forecast = scaled_data.flatten()
    #print(x_forecast.shape)
    #print(x_forecast[-50:])

    x_forecast = x_forecast[-sample:]
    y_forecast = np.array([])
    forecast_period = forecast_days
    #print(x_forecast)

    #model = load_model('stock_xsample.h5')

    #assume sample = 50. use last 50 known stock price to predict the next one
    #then use last 49 known stock price + previous predicted price to predict next one...
    #loop for forecast peroid, all forecasted price are recorded in y_forecast
    for i in range(0, forecast_period):
        if i < sample:
            x_forecast_new = np.append(x_forecast[i:], y_forecast)
        else:
            x_forecast_new = y_forecast[i-sample:]

        x_forecast_reshaped = np.reshape(x_forecast_new, (1, sample, 1))
        predicted_stock_price_single = model.predict(x_forecast_reshaped)
        y_forecast = np.append(y_forecast, predicted_stock_price_single[0][0])

    #print(y_forecast)
    #print(predicted_stock_price.shape)
    #print(predicted_stock_price)

    y_forecast = np.reshape(y_forecast, (len(y_forecast), 1))
    predicted_stock_price = scaler.inverse_transform(y_forecast)
    predicted_stock_price = predicted_stock_price.flatten()
    #print(predicted_stock_price)

    ax = plt.figure(figsize=(7, 5), dpi=100).add_subplot(111)
    #fig, ax = plt.subplots()

    t = df['Adj Close'].index
    t = pd.to_datetime(t)
    #print(t)

    #create xaxis for predicted stock price
    t_predicted = pd.bdate_range(end.strftime('%Y-%m-%d'), periods=forecast_period, freq="D")
    #print(t_predicted)

    ax.plot(t_predicted, predicted_stock_price, label="Predicted stock price", color="red")
    ax.plot(t, data, label="Real Stock Price", color="blue")

    ax.legend()
    ax.xaxis.set_major_locator(mdates.DayLocator(interval=300))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d %Y'))

    ax.set_title(ticker + " Price Prediction")
    plt.ylabel("USD")

    ax.xaxis.grid(True, which="major")
    ax.yaxis.grid(True, which="major")

    plt.xticks(rotation=45)
    plt.subplots_adjust(bottom=.2)

    zoom_factory(ax)
    plt.show()


# for mouse scroll zoom
def zoom_factory(ax,base_scale = 1.2):
    def zoom_fun(event):
        # get the current x and y limits
        cur_xlim = ax.get_xlim()
        cur_ylim = ax.get_ylim()
        # set the range
        cur_xrange = (cur_xlim[1] - cur_xlim[0])*.5
        cur_yrange = (cur_ylim[1] - cur_ylim[0])*.5
        xdata = event.xdata # get event x location
        ydata = event.ydata # get event y location
        if event.button == 'up':
            # deal with zoom in
            scale_factor = 1/base_scale
        elif event.button == 'down':
            # deal with zoom out
            scale_factor = base_scale
        else:
            # deal with something that should never happen
            scale_factor = 1
            print (event.button)
        # set new limits
        ax.set_xlim([xdata - cur_xrange*scale_factor,
                     xdata + cur_xrange*scale_factor])
        ax.set_ylim([ydata - cur_yrange*scale_factor,
                     ydata + cur_yrange*scale_factor])
        ax.figure.canvas.draw_idle() # force re-draw the next time the GUI refreshes

    fig = ax.get_figure() # get the figure of interest
    # attach the call back
    fig.canvas.mpl_connect('scroll_event',zoom_fun)

    #return the function
    return zoom_fun


#predict_stock_trend(ticker, start, end, train_sample, train_epoch, forecast_days)
predict_stock_trend("FDX", dt.datetime(2015, 1, 1), dt.datetime.today(), 50, 100, 90)

reference:

pandas date_range, bdate_range

matplotlib zoom on scroll

python .gitignore

Sunday, 24 January 2021

keras 16 RNN predict stock price

#training data from Jan 3 2012 to Dec 30 2016 
            Date    Open    High     Low   Close      Volume
0       1/3/2012  325.25  332.83  324.97  663.59   7,380,500
1       1/4/2012  331.27  333.87  329.08  666.45   5,749,400
2       1/5/2012  329.83  330.75  326.89  657.21   6,590,300
3       1/6/2012  328.34  328.77  323.68  648.24   5,405,900
4       1/9/2012  322.04  322.29  309.46  620.76  11,688,800
...          ...     ...     ...     ...     ...         ...
1253  12/23/2016  790.90  792.74  787.28  789.91     623,400
1254  12/27/2016  790.68  797.86  787.66  791.55     789,100
1255  12/28/2016  793.70  794.23  783.20  785.05   1,153,800
1256  12/29/2016  783.33  785.93  778.92  782.79     744,300
1257  12/30/2016  782.75  782.78  770.41  771.82   1,770,000

#testing data from Jan 3 2017 to Jan 31 2017
         Date    Open    High     Low   Close     Volume
0    1/3/2017  778.81  789.63  775.80  786.14  1,657,300
1    1/4/2017  788.36  791.34  783.16  786.90  1,073,000
2    1/5/2017  786.08  794.48  785.02  794.02  1,335,200
3    1/6/2017  795.26  807.90  792.20  806.15  1,640,200
...
16  1/26/2017  837.81  838.00  827.01  832.15  2,973,900
17  1/27/2017  834.71  841.95  820.44  823.31  2,965,800
18  1/30/2017  814.66  815.84  799.80  802.32  3,246,600
19  1/31/2017  796.86  801.25  790.52  796.79  2,160,600

#goal is to construct a model using 5 years' stock price to forecast next month stock trend

predicted trend follows the real one

#stock.py
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
from tensorflow.keras.models import load_model
import matplotlib.dates as mdates

csv = pd.read_csv("stock_train.csv")
#print(csv)

data = csv.loc[:, ("Open")].values
#print(data)

#scale input between 0, 1
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data.reshape(-1, 1))
#print(scaled_data)
#print(scaled_data.shape[0])

x = []
y = []
#model is trained by sampling 50 days' stock price as input to fit the 51st day price as output
#y lags x by 50 samples
for i in range(50, scaled_data.shape[0]):
    x.append(scaled_data[i-50:i, 0])
    y.append(scaled_data[i, 0])

x, y = np.array(x), np.array(y)

x = np.reshape(x, (x.shape[0], x.shape[1], 1))
#print(x.shape)
#(1208, 50, 1)
#print(x[0])
#print(y.shape)
#(1208,)
#print(y)

#train on GPU
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
#print("Num GPUs Available: ", len(pysical_devices))
tf.config.experimental.set_memory_growth(pysical_devices[0], True)
"""
model = Sequential()
model.add(LSTM(128, input_shape=(50, 1), return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(128, return_sequences=True))
model.add(Dropout(0.1))

model.add(LSTM(128, return_sequences=True))
model.add(Dropout(0.1))

model.add(LSTM(128))
model.add(Dropout(0.1))

model.add(Dense(1))
model.summary()

model.compile(
    loss='mean_squared_error',
    optimizer='adam',
)

model.fit(x,
          y,
          epochs=100,
          batch_size=32)

model.save("stock.h5")
"""
#append test.csv to train.csv
csv2 = pd.read_csv("stock_test.csv")
print(csv2)
csv3 = pd.concat((csv, csv2), axis=0)

#because model use previous 50 days's sample to predict 51st day, 
#needs to grab last 50 sample from train data and concat with test data
#get last 50 from train.csv plus all data from test.csv
data = csv3.loc[:, ("Open")].iloc[x.shape[0]:, ].values
#print(data.shape)
#print(data)

scaled_data = scaler.fit_transform(data.reshape(-1, 1))

x_test = []

for i in range(50, scaled_data.shape[0]):
    x_test.append(scaled_data[i-50:i, 0])

x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))

print(x_test.shape)
#(20, 50, 1)

model = load_model('stock.h5')

predicted_stock_price = model.predict(x_test)
#print(predicted_stock_price.shape)
#print(predicted_stock_price)

#modal output is scaled between 0 and 1, needs convert back to real value
predicted_stock_price = scaler.inverse_transform(predicted_stock_price)
#rint(predicted_stock_price)

ax = plt.figure(figsize=(7, 5), dpi=100).add_subplot(111)

t = csv2.loc[:, "Date"].values
t = pd.to_datetime(t)

ax.plot(t, csv2.loc[:, ("Open")].values, label="Real stock price", color="red")
ax.plot(t, predicted_stock_price, label="Predicted Stock Price", color="blue")

ax.legend()
ax.xaxis.set_major_locator(mdates.DayLocator(interval=4))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d %Y'))

ax.set_title("Stock Price Prediction")
plt.ylabel("USD")

ax.xaxis.grid(True, which="major")
ax.yaxis.grid(True, which="major")

plt.xticks(rotation=45)
plt.subplots_adjust(bottom=.2)
plt.show()

------------------------------
#logs
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lstm (LSTM)                  (None, 50, 128)           66560
_________________________________________________________________
dropout (Dropout)            (None, 50, 128)           0
_________________________________________________________________
lstm_1 (LSTM)                (None, 50, 128)           131584
_________________________________________________________________
dropout_1 (Dropout)          (None, 50, 128)           0
_________________________________________________________________
lstm_2 (LSTM)                (None, 50, 128)           131584
_________________________________________________________________
dropout_2 (Dropout)          (None, 50, 128)           0
_________________________________________________________________
lstm_3 (LSTM)                (None, 128)               131584
_________________________________________________________________
dropout_3 (Dropout)          (None, 128)               0
_________________________________________________________________
dense (Dense)                (None, 1)                 129
=================================================================
Total params: 461,441
Trainable params: 461,441
Non-trainable params: 0

Epoch 1/100
38/38 [==============================] - 5s 14ms/step - loss: 0.1101
Epoch 2/100
38/38 [==============================] - 1s 14ms/step - loss: 0.0039
Epoch 3/100
38/38 [==============================] - 1s 13ms/step - loss: 0.0030
...
Epoch 98/100
38/38 [==============================] - 0s 13ms/step - loss: 8.8894e-04
Epoch 99/100
38/38 [==============================] - 0s 13ms/step - loss: 8.8318e-04
Epoch 100/100
38/38 [==============================] - 0s 13ms/step - loss: 8.7235e-04

Super Mario Odyssey