<jupyter_start><jupyter_text>CONVOLUTON NEURAL NETWORK    Bike Share 데이터로 RNN 구현해 보기  Configure Learning Environment<jupyter_code># !pip install mybatis_mapper2sql
# !pip install git+https://github.com/nockchun/rspy --force
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2"
import rspy as rsp
rsp.setSystemWarning(off=True)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from ipywidgets import interact
from sklearn import preprocessing as skpp, model_selection as skms
import tensorflow as tf
from tensorflow.keras import utils, layers, models, losses, preprocessing
# np.set_printoptions(linewidth=200, precision=2)
print("버전: ", tf.__version__)
print("GPU: ", "사용 가능" if tf.test.is_gpu_available() else "사용 불가능")
print("GPU Device: ", tf.config.experimental.get_visible_devices("GPU"))<jupyter_output>버전:  2.4.1
GPU:  사용 불가능
GPU Device:  []<jupyter_text>Data Loading * UCI Machine Learning Repository 데이터 이용* https://archive.ics.uci.edu/ml/datasets/bike+sharing+dataset<jupyter_code>import pathlib
dataset_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00275/Bike-Sharing-Dataset.zip"
data_dir = utils.get_file('bike_share.zip', origin=dataset_url, extract=True, cache_subdir='bike_share')
data_dir = pathlib.Path(data_dir)
data_dir.parent.as_posix()
!ls /home/rsnet/.keras/bike_share
df_origin = pd.read_csv(data_dir.parent.as_posix() + "/day.csv")
df_origin.head()
df_origin.shape<jupyter_output><empty_output><jupyter_text>Data Exploration & Analysis  데이터 내용>* instant : 기록 순서>* dteday : 기록 날짜>* season : 계절 [1:봄, 2:여름, 3:가을, 4:겨울]>* yr : 년>* mnth : 월 [1-12]>* holiday : 국경일 유무 [0:무, 1:유]>* weekday : 요일 [0:일, 1:월, 2:화, 3:수, 4:목, 5:금, 6:토]>* workingday : 휴무일 유무 [0:일하는 날, 1:쉬는 날]>* weathersit    - 1: Clear, Few clouds, Partly cloudy, Partly cloudy    - 2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist    - 3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds    - 4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog>* temp : 정규화된 섭씨 온도(℃)>* atemp : 정규화된 체감 온도. (t-t_min)/(t_max-t_min), t_min=-16, t_max=+50>* hum : 정규화된 습도.>* windspeed : 정규화된 풍속.>* casual : 일반 사용자.>* registered : 예약 사용자.>* cnt : 일반 + 예약 사용자. (전체 사용자)<jupyter_code>cols = ["season", "mnth", "holiday", "weekday", "workingday", "weathersit", "atemp", "hum", "windspeed"]
@interact(compare=cols)
def exploration(compare):
    fig, ax1 = plt.subplots(figsize=(30, 8))
    ax2 = ax1.twinx()
    
    ax1.plot(df_origin.cnt.values, color="#3186ad", label="cnt")
    ax1.plot(df_origin.casual.values, color="#bebebe", label="casual")
    ax1.plot(df_origin.registered.values, color="#878787", label="registered")
    ax1.set_ylabel("cnt")

    ax2.plot(df_origin[compare].values, color="red", alpha=0.7, label="compare")
    ax2.set_ylabel(compare)
    
    ax1.legend(title="Species", loc="upper left")
    plt.show()<jupyter_output><empty_output><jupyter_text>Preprocessing Data<jupyter_code>print(list(df_origin.columns))
df_preproc = df_origin[["holiday", "weekday", "workingday", "weathersit", "temp", "atemp", "hum", "windspeed", "cnt"]]
df_preproc.head()<jupyter_output><empty_output><jupyter_text>one-hot encoding<jupyter_code>onehotWeekday = pd.get_dummies(df_preproc["weekday"], prefix="weekday")
onehotWeathersit = pd.get_dummies(df_preproc["weathersit"], prefix="weathersit")
onehotWeekday.head()
df_preproc = df_preproc.join(onehotWeekday).join(onehotWeathersit)
df_preproc.head()
df_preproc.weathersit.unique()<jupyter_output><empty_output><jupyter_text>Scaling Y<jupyter_code>scalerCnt = skpp.MinMaxScaler((0, 1))
scaledCnt = scalerCnt.fit_transform(df_preproc["cnt"].values.reshape(len(df_preproc["cnt"]), 1))
df_preproc["cntscl"] = scaledCnt
df_preproc.head()
scalerCnt.transform([[985]])
scalerCnt.inverse_transform([[0.11079153]])<jupyter_output><empty_output><jupyter_text>Prepare Data<jupyter_code>preproc_data = df_preproc[["holiday", "workingday", "temp", "atemp", "hum", "windspeed",
     "weekday_0", "weekday_1", "weekday_2", "weekday_3", "weekday_4", "weekday_5", "weekday_6",
     "weathersit_1", "weathersit_2", "weathersit_3", "cntscl"]].values
preproc_label = df_preproc[["cntscl"]].values
preproc_data.shape, preproc_label.shape<jupyter_output><empty_output><jupyter_text>Splitting<jupyter_code>SIZE_VALI  = 50
SIZE_TEST  = 50
SIZE_TRAIN = preproc_data.shape[0] - SIZE_VALI - SIZE_TEST
SIZE_TRAIN
train_data = preproc_data[:SIZE_TRAIN]
vali_data = preproc_data[SIZE_TRAIN: -SIZE_VALI]
test_data = preproc_data[-SIZE_VALI:]
train_data.shape, vali_data.shape, test_data.shape
train_label = preproc_label[:SIZE_TRAIN]
vali_label = preproc_label[SIZE_TRAIN: -SIZE_VALI]
test_label = preproc_label[-SIZE_VALI:]
train_label.shape, vali_label.shape, test_label.shape<jupyter_output><empty_output><jupyter_text>Windowing<jupyter_code>SIZE_FEATURES = train_data.shape[1]
SIZE_TRAIN_DATA = 128 # train_data.shape[0]
TIME_STEP = 5
SIZE_FEATURES, SIZE_TRAIN_DATA
train_window = preprocessing.sequence.TimeseriesGenerator(
    train_data, train_label, length=TIME_STEP, batch_size=SIZE_TRAIN_DATA
)
vali_window = preprocessing.sequence.TimeseriesGenerator(
    vali_data, vali_label, length=TIME_STEP, batch_size=1
)
test_window = preprocessing.sequence.TimeseriesGenerator(
    test_data, test_label, length=TIME_STEP, batch_size=1
)
len(test_window)
x, y = test_window[0]
print(x.tolist())
print(y)<jupyter_output>[[[1.0, 0.0, 0.485, 0.475383, 0.741667, 0.173517, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.7187068568798896], [0.0, 1.0, 0.343333, 0.323225, 0.662917, 0.342046, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.4684767602393005], [0.0, 1.0, 0.289167, 0.281563, 0.552083, 0.199625, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.6296594569719283], [0.0, 1.0, 0.321667, 0.324492, 0.620417, 0.152987, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.6239070409572021], [0.0, 1.0, 0.345, 0.347204, 0.524583, 0.171025, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.6530142659917165]]]
[[0.64507593]]<jupyter_text>Generate Model<jupyter_code>model = models.Sequential([
    layers.Input([TIME_STEP, SIZE_FEATURES]),
    layers.SimpleRNN(20, return_sequences=True),
    layers.TimeDistributed(layers.Dropout(.2)),
    layers.SimpleRNN(20),
    layers.Dropout(.2),
    layers.Dense(1, activation="tanh")
])
model.summary()
model.compile(
    loss="mse",
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001)
)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>history = model.fit_generator(
    train_window,
    epochs=100,
    verbose=1,
    validation_data=vali_window
)
df_history = pd.DataFrame(history.history)
df_history[["loss", "val_loss"]].plot()<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code>df_result = pd.DataFrame({'Actual' : [], 'Prediction' : []})

for i in range(len(test_window)):
    x, y = test_window[i]
    yhat = model.predict(x)
    df_result = df_result.append({
        'Actual': scalerCnt.inverse_transform(y)[0][0],
        'Prediction': scalerCnt.inverse_transform(yhat)[0][0]},
        ignore_index=True
    )
df_result['Diff'] = 100 * (df_result['Prediction'] - df_result['Actual']) / df_result['Actual']
df_result.head()
df_result[["Actual", "Prediction"]].plot(figsize=(15, 10))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16279069767441862, 'fraction_numerical': 0.06552667578659371, 'mean_word_length': 5.996172248803828, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 63, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '580385', 'license': 'Apache-2.0', 'n_tokens_mistral': 3374, 'n_tokens_neox': 3207, 'n_words': 514, 'path': 'lecture_source/machine_learning/0214_rnn_bike_share_1.ipynb', 'repo_name': 'nockchun/rsnet', 'size': '7310'}