{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Time Series Forecasting Assignment"]},{"cell_type":"markdown","metadata":{},"source":["## Tasks"]},{"cell_type":"markdown","metadata":{},"source":["![types](https://static-1300131294.cos.ap-shanghai.myqcloud.com/images/assignment/deep-learning/time-series/time-series.jpg)"]},{"cell_type":"markdown","metadata":{},"source":["### Task1 \n","Given 10 timesteps of features, predict the USDT of the next timestep (many to one)"]},{"cell_type":"markdown","metadata":{},"source":["### Task2\n","Given 10 timestaps of features, predict the USDT of those timesteps (many to many)"]},{"cell_type":"markdown","metadata":{},"source":["## Imports"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T14:35:13.896551Z","iopub.status.busy":"2022-04-12T14:35:13.895956Z","iopub.status.idle":"2022-04-12T14:35:23.233167Z","shell.execute_reply":"2022-04-12T14:35:23.232198Z","shell.execute_reply.started":"2022-04-12T14:35:13.896458Z"},"trusted":true},"outputs":[],"source":["import numpy as np\n","import matplotlib.pyplot as plt\n","import tensorflow as tf\n","import pandas as pd\n","from tensorflow import keras\n","from sklearn.preprocessing import StandardScaler\n","from tensorflow.keras.models import Sequential\n","from tensorflow.keras.layers import LSTM, Dense, Dropout, Flatten, Conv1D, SimpleRNN\n","# import plotly.express as px"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T14:35:23.235292Z","iopub.status.busy":"2022-04-12T14:35:23.234994Z","iopub.status.idle":"2022-04-12T14:35:23.241403Z","shell.execute_reply":"2022-04-12T14:35:23.239025Z","shell.execute_reply.started":"2022-04-12T14:35:23.235258Z"},"trusted":true},"outputs":[],"source":["tf.random.set_seed(42)\n","np.random.seed(42)"]},{"cell_type":"markdown","metadata":{},"source":["## Loading Data"]},{"cell_type":"markdown","metadata":{},"source":["### CSV Data Path "]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T14:35:24.035161Z","iopub.status.busy":"2022-04-12T14:35:24.034785Z","iopub.status.idle":"2022-04-12T14:35:24.03989Z","shell.execute_reply":"2022-04-12T14:35:24.039016Z","shell.execute_reply.started":"2022-04-12T14:35:24.03513Z"},"trusted":true},"outputs":[],"source":["data_path = 'https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/timeseries.csv'"]},{"cell_type":"markdown","metadata":{},"source":["### Reading CSV file with Pandas"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T14:35:24.041939Z","iopub.status.busy":"2022-04-12T14:35:24.041439Z","iopub.status.idle":"2022-04-12T14:35:24.069844Z","shell.execute_reply":"2022-04-12T14:35:24.06893Z","shell.execute_reply.started":"2022-04-12T14:35:24.041894Z"},"trusted":true},"outputs":[],"source":["data = pd.read_csv(data_path)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["data.head()"]},{"cell_type":"markdown","metadata":{},"source":["### Normalizing Data"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T14:35:24.071742Z","iopub.status.busy":"2022-04-12T14:35:24.071222Z","iopub.status.idle":"2022-04-12T14:35:24.076727Z","shell.execute_reply":"2022-04-12T14:35:24.075451Z","shell.execute_reply.started":"2022-04-12T14:35:24.07169Z"},"trusted":true},"outputs":[],"source":["from sklearn import preprocessing"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T14:35:24.0787Z","iopub.status.busy":"2022-04-12T14:35:24.078053Z","iopub.status.idle":"2022-04-12T14:35:24.089461Z","shell.execute_reply":"2022-04-12T14:35:24.08881Z","shell.execute_reply.started":"2022-04-12T14:35:24.078653Z"},"trusted":true},"outputs":[],"source":["features = ['high', 'low', 'open', 'close', 'Volume XRP', 'Volume USDT']"]},{"cell_type":"markdown","metadata":{},"source":["0 unix 1334 non-null float64\n"," 1 date 1334 non-null object \n"," 2 symbol 1334 non-null object \n"," 3 open 1334 non-null float64\n"," 4 high 1334 non-null float64\n"," 5 low 1334 non-null float64\n"," 6 close 1334 non-null float64\n"," 7 Volume XRP 1334 non-null float64\n"," 8 Volume USDT 1334 non-null float64"]},{"cell_type":"markdown","metadata":{},"source":["## Task1"]},{"cell_type":"markdown","metadata":{},"source":["### Data Preparation"]},{"cell_type":"markdown","metadata":{},"source":["Preparing the data for the first task"]},{"cell_type":"markdown","metadata":{},"source":["We will try to predict the value of USDT on the 11th day using the data of other features from day 1 to 10"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T14:35:24.091614Z","iopub.status.busy":"2022-04-12T14:35:24.090863Z","iopub.status.idle":"2022-04-12T14:35:24.101867Z","shell.execute_reply":"2022-04-12T14:35:24.10125Z","shell.execute_reply.started":"2022-04-12T14:35:24.091564Z"},"trusted":true},"outputs":[],"source":["def sequential_window_dataset(series, window_size):\n"," series = tf.expand_dims(series, axis=-1)\n"," ds = tf.data.Dataset.from_tensor_slices(series)\n"," ds = ds.window(window_size + 1, shift=window_size, drop_remainder=True)\n"," ds = ds.flat_map(lambda window: window.batch(window_size + 1))\n"," ds = ds.map(lambda window: (window[:-1], window[1:]))\n"," return ds.batch(1).prefetch(1)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:13:53.884886Z","iopub.status.busy":"2022-04-12T15:13:53.884547Z","iopub.status.idle":"2022-04-12T15:13:53.893855Z","shell.execute_reply":"2022-04-12T15:13:53.893073Z","shell.execute_reply.started":"2022-04-12T15:13:53.884848Z"},"trusted":true},"outputs":[],"source":["def data_preparation(df, lookback, future, scaler):\n"," date_train=pd.to_datetime(df['date'])\n"," features = ['high', 'low', 'open', 'close', 'Volume XRP', 'Volume USDT'] \n"," df_train=df[features]\n"," df_train=df_train.astype(float)\n"," \n"," df_train_scaled = scaler.transform(df_train)\n","\n"," X, y = [],[]\n"," for i in range(lookback, len(df_train_scaled)-future+1):\n"," X.append(df_train_scaled[i-lookback:i, 0:df_train.shape[1]])\n"," y.append(df_train_scaled[i+future-1:i+future, 0])\n"," \n"," return np.array(X), np.array(y), df_train, date_train\n"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["def predict(model, date_train,X_train, future_steps, ds):\n"," # Extracting dates\n"," dates = pd.date_range(list(date_train)[-1], periods=future, freq='1d').tolist()\n"," # use the last future steps from X_train \n"," predicted = model.predict(X_train[-future_steps:])\n"," predicted = np.repeat(predicted, ds.shape[1], axis=-1)\n"," nsamples, nx, ny = predicted.shape\n"," predicted = predicted.reshape((nsamples,nx*ny))\n","\n"," return predicted, dates\n","\n","def output_preparation(forecasting_dates, predictions, date_column = 'date', predicted_column='Volume USDT'):\n"," dates=[]\n"," for date in forecasting_dates:\n"," dates.append(date.date())\n"," predicted_df=pd.DataFrame(columns=[date_column, predicted_column])\n"," predicted_df[date_column] = pd.to_datetime(dates)\n"," predicted_df[predicted_column]= predictions\n"," return predicted_df"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:31:12.536092Z","iopub.status.busy":"2022-04-12T15:31:12.535772Z","iopub.status.idle":"2022-04-12T15:31:12.541764Z","shell.execute_reply":"2022-04-12T15:31:12.541144Z","shell.execute_reply.started":"2022-04-12T15:31:12.536057Z"},"trusted":true},"outputs":[],"source":["def results(df, lookback, future, scaler, col, X_train, y_train, df_train, date_train, model):\n"," predictions, forecasting_dates = predict(model, date_train, X_train, future, df)\n"," results = output_preparation(forecasting_dates, predictions) \n"," print(results.head())"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:13:02.580693Z","iopub.status.busy":"2022-04-12T15:13:02.580373Z","iopub.status.idle":"2022-04-12T15:13:02.592909Z","shell.execute_reply":"2022-04-12T15:13:02.591836Z","shell.execute_reply.started":"2022-04-12T15:13:02.580661Z"},"trusted":true},"outputs":[],"source":["scaler = StandardScaler()\n","features = ['high', 'low', 'open', 'close', 'Volume XRP', 'Volume USDT'] \n","df_train=data[features]\n","df_train=df_train.astype(float)\n","\n","scaler = scaler.fit(df_train)\n","df_scaled = scaler.transform(df_train)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:36:53.155428Z","iopub.status.busy":"2022-04-12T15:36:53.155088Z","iopub.status.idle":"2022-04-12T15:36:53.163918Z","shell.execute_reply":"2022-04-12T15:36:53.162807Z","shell.execute_reply.started":"2022-04-12T15:36:53.155394Z"},"trusted":true},"outputs":[],"source":["scaler_y = StandardScaler()\n","df_y = scaler_y.fit_transform(data[['Volume USDT']].to_numpy())"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:19:04.326831Z","iopub.status.busy":"2022-04-12T15:19:04.326497Z","iopub.status.idle":"2022-04-12T15:19:04.425146Z","shell.execute_reply":"2022-04-12T15:19:04.423976Z","shell.execute_reply.started":"2022-04-12T15:19:04.326799Z"},"trusted":true},"outputs":[],"source":["window_size = 10\n","future = 1\n","X_train, y_train, df_train, date_train = data_preparation(data, window_size, future, scaler)"]},{"cell_type":"markdown","metadata":{},"source":["### Modeling"]},{"cell_type":"markdown","metadata":{},"source":["#### ANN"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from keras.callbacks import EarlyStopping, ModelCheckpoint"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:19:12.988061Z","iopub.status.busy":"2022-04-12T15:19:12.987515Z","iopub.status.idle":"2022-04-12T15:19:12.996624Z","shell.execute_reply":"2022-04-12T15:19:12.995372Z","shell.execute_reply.started":"2022-04-12T15:19:12.988026Z"},"trusted":true},"outputs":[],"source":["def ann_builder(X, y):\n"," model = tf.keras.models.Sequential() \n"," model.add(Dense(12, activation='selu', input_shape=(X.shape[1], X.shape[2])))\n"," model.add(Dense(y.shape[1]))\n","\n"," optimizer = tf.keras.optimizers.Adam() \n"," model.compile(\n"," loss='huber',\n"," optimizer=optimizer,\n"," metrics=['mse']\n"," )\n"," \n"," callbacks = [EarlyStopping(monitor='val_loss', patience=20),\n"," ModelCheckpoint(filepath='best_model_ann.h5', monitor='val_loss', save_best_only=True)]\n","\n"," model.fit(X, y, epochs=50, verbose=1, validation_split=0.1, callbacks = callbacks, batch_size=16)\n"," return model"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:19:13.32032Z","iopub.status.busy":"2022-04-12T15:19:13.319985Z","iopub.status.idle":"2022-04-12T15:19:20.806441Z","shell.execute_reply":"2022-04-12T15:19:20.805518Z","shell.execute_reply.started":"2022-04-12T15:19:13.320289Z"},"trusted":true},"outputs":[],"source":["model = ann_builder(X_train, y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:19:20.808839Z","iopub.status.busy":"2022-04-12T15:19:20.808543Z","iopub.status.idle":"2022-04-12T15:19:21.00832Z","shell.execute_reply":"2022-04-12T15:19:21.007418Z","shell.execute_reply.started":"2022-04-12T15:19:20.808809Z"},"trusted":true},"outputs":[],"source":["pd.DataFrame(model.history.history).plot()"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["#if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/model/best_model_ann.h5\"\n","model_path = get_file(\"best_model_ann.h5\", model_url)\n","model = load_model(model_path)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:39:33.02328Z","iopub.status.busy":"2022-04-12T15:39:33.022708Z","iopub.status.idle":"2022-04-12T15:39:33.303576Z","shell.execute_reply":"2022-04-12T15:39:33.30297Z","shell.execute_reply.started":"2022-04-12T15:39:33.023223Z"},"trusted":true},"outputs":[],"source":["results(data, window_size, future, scaler, 'Volume USDT', X_train, y_train, df_train, date_train, model)"]},{"cell_type":"markdown","metadata":{},"source":["#### CNN"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:40:29.602463Z","iopub.status.busy":"2022-04-12T15:40:29.601886Z","iopub.status.idle":"2022-04-12T15:40:29.609586Z","shell.execute_reply":"2022-04-12T15:40:29.608617Z","shell.execute_reply.started":"2022-04-12T15:40:29.60241Z"},"trusted":true},"outputs":[],"source":["def cnn_builder(X,y):\n"," model = tf.keras.models.Sequential() \n"," model.add(Conv1D(32, (3), activation='relu', input_shape=(X.shape[1], X.shape[2])))\n"," model.add(Dense(y.shape[1]))\n","\n"," optimizer = tf.keras.optimizers.Adam() \n"," model.compile(\n"," loss='huber',\n"," optimizer=optimizer,\n"," metrics=['mse']\n"," )\n"," \n"," callbacks = [EarlyStopping(monitor='val_loss', patience=20),\n"," ModelCheckpoint(filepath='best_model_cnn.h5', monitor='val_loss', save_best_only=True)]\n","\n"," model.fit(X, y, epochs=50, verbose=1, validation_split=0.1, callbacks = callbacks, batch_size=16)\n"," return model"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:56:06.785711Z","iopub.status.busy":"2022-04-12T15:56:06.784985Z","iopub.status.idle":"2022-04-12T15:56:17.085759Z","shell.execute_reply":"2022-04-12T15:56:17.084731Z","shell.execute_reply.started":"2022-04-12T15:56:06.785658Z"},"trusted":true},"outputs":[],"source":["model = cnn_builder(X_train, y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:56:17.088463Z","iopub.status.busy":"2022-04-12T15:56:17.087874Z","iopub.status.idle":"2022-04-12T15:56:17.357589Z","shell.execute_reply":"2022-04-12T15:56:17.356708Z","shell.execute_reply.started":"2022-04-12T15:56:17.088424Z"},"trusted":true},"outputs":[],"source":["pd.DataFrame(model.history.history).plot()"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["#if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/model/best_model_cnn.h5\"\n","model_path = get_file(\"best_model_cnn_2.h5\", model_url)\n","model = load_model(model_path)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:56:17.359474Z","iopub.status.busy":"2022-04-12T15:56:17.359243Z","iopub.status.idle":"2022-04-12T15:56:17.755583Z","shell.execute_reply":"2022-04-12T15:56:17.754723Z","shell.execute_reply.started":"2022-04-12T15:56:17.359447Z"},"trusted":true},"outputs":[],"source":["results(data, window_size, future, scaler, 'Volume USDT', X_train, y_train, df_train, date_train, model)"]},{"cell_type":"markdown","metadata":{},"source":["#### Simple RNN"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:44:40.680135Z","iopub.status.busy":"2022-04-12T15:44:40.679776Z","iopub.status.idle":"2022-04-12T15:44:40.688Z","shell.execute_reply":"2022-04-12T15:44:40.687263Z","shell.execute_reply.started":"2022-04-12T15:44:40.680102Z"},"trusted":true},"outputs":[],"source":["def rnn_builder(X,y):\n"," model = tf.keras.models.Sequential() \n"," model.add(SimpleRNN(1, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))\n"," model.add(Dense(y.shape[1]))\n","\n"," optimizer = tf.keras.optimizers.Adam() \n"," model.compile(\n"," loss='huber',\n"," optimizer=optimizer,\n"," metrics=['mse']\n"," )\n"," \n"," callbacks = [EarlyStopping(monitor='val_loss', patience=20),\n"," ModelCheckpoint(filepath='best_model_rnn.h5', monitor='val_loss', save_best_only=True)]\n","\n"," model.fit(X, y, epochs=50, verbose=1, validation_split=0.1, callbacks = callbacks, batch_size=16)\n"," return model"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:45:49.897658Z","iopub.status.busy":"2022-04-12T15:45:49.896897Z","iopub.status.idle":"2022-04-12T15:46:04.122733Z","shell.execute_reply":"2022-04-12T15:46:04.121792Z","shell.execute_reply.started":"2022-04-12T15:45:49.897597Z"},"trusted":true},"outputs":[],"source":["model = rnn_builder(X_train, y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:46:04.125656Z","iopub.status.busy":"2022-04-12T15:46:04.125086Z","iopub.status.idle":"2022-04-12T15:46:04.393283Z","shell.execute_reply":"2022-04-12T15:46:04.392611Z","shell.execute_reply.started":"2022-04-12T15:46:04.125604Z"},"trusted":true},"outputs":[],"source":["pd.DataFrame(model.history.history).plot()"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["#if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/model/best_model_rnn.h5\"\n","model_path = get_file(\"best_model_rnn.h5\", model_url)\n","model = load_model(model_path)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T15:46:04.395197Z","iopub.status.busy":"2022-04-12T15:46:04.394439Z","iopub.status.idle":"2022-04-12T15:46:04.910361Z","shell.execute_reply":"2022-04-12T15:46:04.909532Z","shell.execute_reply.started":"2022-04-12T15:46:04.395135Z"},"trusted":true},"outputs":[],"source":["results(data, window_size, future, scaler, 'Volume USDT', X_train, y_train, df_train, date_train, model)"]},{"cell_type":"markdown","metadata":{},"source":["#### LSTM"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T14:46:37.385227Z","iopub.status.busy":"2022-04-12T14:46:37.384868Z","iopub.status.idle":"2022-04-12T14:46:37.392746Z","shell.execute_reply":"2022-04-12T14:46:37.392017Z","shell.execute_reply.started":"2022-04-12T14:46:37.385172Z"},"trusted":true},"outputs":[],"source":["def lstm_builder(X,y):\n"," model = tf.keras.models.Sequential() \n"," model.add(LSTM(1, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))\n"," model.add(Dense(y.shape[1]))\n","\n"," optimizer = tf.keras.optimizers.Adam() \n"," model.compile(\n"," loss='huber',\n"," optimizer=optimizer,\n"," metrics=['mse']\n"," )\n"," \n"," callbacks = [EarlyStopping(monitor='val_loss', patience=20),\n"," ModelCheckpoint(filepath='best_model_lstm.h5', monitor='val_loss', save_best_only=True)]\n","\n"," model.fit(X, y, epochs=50, verbose=1, validation_split=0.1, callbacks = callbacks, batch_size=16)\n"," return model"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:00:08.922017Z","iopub.status.busy":"2022-04-12T16:00:08.92167Z","iopub.status.idle":"2022-04-12T16:00:30.313211Z","shell.execute_reply":"2022-04-12T16:00:30.312502Z","shell.execute_reply.started":"2022-04-12T16:00:08.921985Z"},"trusted":true},"outputs":[],"source":["model = lstm_builder(X_train,y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["#if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/model/best_model_lstm.h5\"\n","model_path = get_file(\"best_model_lstm.h5\", model_url)\n","model = load_model(model_path)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:01:32.636256Z","iopub.status.busy":"2022-04-12T16:01:32.634909Z","iopub.status.idle":"2022-04-12T16:01:33.393577Z","shell.execute_reply":"2022-04-12T16:01:33.392644Z","shell.execute_reply.started":"2022-04-12T16:01:32.636211Z"},"trusted":true},"outputs":[],"source":["results(data, window_size, future, scaler, 'Volume USDT', X_train, y_train, df_train, date_train, model)"]},{"cell_type":"markdown","metadata":{},"source":["## Task 2"]},{"cell_type":"markdown","metadata":{},"source":["Just setting future to 10 and the window to 10"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:02:46.395939Z","iopub.status.busy":"2022-04-12T16:02:46.394995Z","iopub.status.idle":"2022-04-12T16:02:46.496877Z","shell.execute_reply":"2022-04-12T16:02:46.495933Z","shell.execute_reply.started":"2022-04-12T16:02:46.395897Z"},"trusted":true},"outputs":[],"source":["window_size = 20\n","future = 20\n","X_train, y_train, df_train, date_train = data_preparation(data, window_size, future, scaler)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["def results(df, lookback, future, scaler, col, X_train, y_train, df_train, date_train, model):\n"," predictions, forecasting_dates = predict(model, date_train, X_train, future, df)\n"," results = output_preparation(forecasting_dates, predictions) \n"," print(results.head())\n"," fig = plt.plot(results['date'], results['Volume USDT'])\n"," plt.show()"]},{"cell_type":"markdown","metadata":{},"source":["Same models"]},{"cell_type":"markdown","metadata":{},"source":["### ANN\n"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:02:50.007844Z","iopub.status.busy":"2022-04-12T16:02:50.007473Z","iopub.status.idle":"2022-04-12T16:02:50.015958Z","shell.execute_reply":"2022-04-12T16:02:50.014983Z","shell.execute_reply.started":"2022-04-12T16:02:50.007787Z"},"trusted":true},"outputs":[],"source":["def ann_builder(X,y):\n"," model = tf.keras.models.Sequential() \n"," model.add(Dense(32, activation='selu', input_shape=(X.shape[1], X.shape[2])))\n"," model.add(Dense(y.shape[1]))\n","\n"," optimizer = tf.keras.optimizers.Adam() \n"," model.compile(\n"," loss='huber',\n"," optimizer=optimizer,\n"," metrics=['mse']\n"," )\n"," \n"," callbacks = [EarlyStopping(monitor='val_loss', patience=20),\n"," ModelCheckpoint(filepath='best_model_ann_2.h5', monitor='val_loss', save_best_only=True)]\n","\n"," model.fit(X, y, epochs=50, verbose=1, validation_split=0.1, callbacks = callbacks, batch_size=16)\n"," return model"]},{"cell_type":"code","execution_count":null,"metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2022-04-12T16:02:51.357651Z","iopub.status.busy":"2022-04-12T16:02:51.357013Z","iopub.status.idle":"2022-04-12T16:02:59.451746Z","shell.execute_reply":"2022-04-12T16:02:59.450903Z","shell.execute_reply.started":"2022-04-12T16:02:51.357592Z"},"trusted":true},"outputs":[],"source":["model = ann_builder(X_train,y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["#if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/model/best_model_ann_2.h5\"\n","model_path = get_file(\"best_model_ann_2.h5\", model_url)\n","model = load_model(model_path)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:02:59.453678Z","iopub.status.busy":"2022-04-12T16:02:59.453453Z","iopub.status.idle":"2022-04-12T16:02:59.83894Z","shell.execute_reply":"2022-04-12T16:02:59.838262Z","shell.execute_reply.started":"2022-04-12T16:02:59.453651Z"},"trusted":true},"outputs":[],"source":["results(data, window_size, future, scaler, 'Volume USDT', X_train, y_train, df_train, date_train, model)"]},{"cell_type":"markdown","metadata":{},"source":["### RNN"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:03:49.756263Z","iopub.status.busy":"2022-04-12T16:03:49.755868Z","iopub.status.idle":"2022-04-12T16:03:49.765105Z","shell.execute_reply":"2022-04-12T16:03:49.7642Z","shell.execute_reply.started":"2022-04-12T16:03:49.756222Z"},"trusted":true},"outputs":[],"source":["def rnn_builder(X,y):\n"," model = tf.keras.models.Sequential() \n"," model.add(SimpleRNN(1, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))\n"," model.add(Dense(y.shape[1]))\n","\n"," optimizer = tf.keras.optimizers.Adam() \n"," model.compile(\n"," loss='huber',\n"," optimizer=optimizer,\n"," metrics=['mse']\n"," )\n"," \n"," callbacks = [EarlyStopping(monitor='val_loss', patience=20),\n"," ModelCheckpoint(filepath='best_model_rnn_2.h5', monitor='val_loss', save_best_only=True)]\n","\n"," model.fit(X, y, epochs=50, verbose=1, validation_split=0.1, callbacks = callbacks, batch_size=16)\n"," return model"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:04:06.380045Z","iopub.status.busy":"2022-04-12T16:04:06.379369Z","iopub.status.idle":"2022-04-12T16:04:24.655724Z","shell.execute_reply":"2022-04-12T16:04:24.65491Z","shell.execute_reply.started":"2022-04-12T16:04:06.379994Z"},"trusted":true},"outputs":[],"source":["model = rnn_builder(X_train, y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:09:44.406871Z","iopub.status.busy":"2022-04-12T16:09:44.406498Z","iopub.status.idle":"2022-04-12T16:09:44.671091Z","shell.execute_reply":"2022-04-12T16:09:44.670199Z","shell.execute_reply.started":"2022-04-12T16:09:44.406836Z"},"trusted":true},"outputs":[],"source":["pd.DataFrame(model.history.history).plot()"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["#if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/model/best_model_rnn_2.h5\"\n","model_path = get_file(\"best_model_rnn_2.h5\", model_url)\n","model = load_model(model_path)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:09:46.239654Z","iopub.status.busy":"2022-04-12T16:09:46.23927Z","iopub.status.idle":"2022-04-12T16:09:46.756669Z","shell.execute_reply":"2022-04-12T16:09:46.755737Z","shell.execute_reply.started":"2022-04-12T16:09:46.239616Z"},"trusted":true},"outputs":[],"source":["results(data, window_size, future, scaler, 'Volume USDT', X_train, y_train, df_train, date_train, model)"]},{"cell_type":"markdown","metadata":{},"source":["### CNN"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:09:57.36946Z","iopub.status.busy":"2022-04-12T16:09:57.369151Z","iopub.status.idle":"2022-04-12T16:09:57.377443Z","shell.execute_reply":"2022-04-12T16:09:57.376437Z","shell.execute_reply.started":"2022-04-12T16:09:57.36943Z"},"trusted":true},"outputs":[],"source":["def cnn_builder(X,y):\n"," model = tf.keras.models.Sequential() \n"," model.add(Conv1D(32, (3), activation='relu', input_shape=(X.shape[1], X.shape[2])))\n"," model.add(Dense(y.shape[1]))\n","\n"," optimizer = tf.keras.optimizers.Adam() \n"," model.compile(\n"," loss='huber',\n"," optimizer=optimizer,\n"," metrics=['mse']\n"," )\n"," \n"," callbacks = [EarlyStopping(monitor='val_loss', patience=20),\n"," ModelCheckpoint(filepath='best_model_cnn_2.h5', monitor='val_loss', save_best_only=True)]\n","\n"," model.fit(X, y, epochs=50, verbose=1, validation_split=0.1, batch_size=16)\n"," return model"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:10:07.420494Z","iopub.status.busy":"2022-04-12T16:10:07.419047Z","iopub.status.idle":"2022-04-12T16:10:19.115237Z","shell.execute_reply":"2022-04-12T16:10:19.114482Z","shell.execute_reply.started":"2022-04-12T16:10:07.420368Z"},"trusted":true},"outputs":[],"source":["model = cnn_builder(X_train, y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:11:08.903363Z","iopub.status.busy":"2022-04-12T16:11:08.902128Z","iopub.status.idle":"2022-04-12T16:11:09.200721Z","shell.execute_reply":"2022-04-12T16:11:09.199975Z","shell.execute_reply.started":"2022-04-12T16:11:08.903312Z"},"trusted":true},"outputs":[],"source":["pd.DataFrame(model.history.history).plot()"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["# if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/model/best_model_cnn_2.h5\"\n","model_path = get_file('best_model_cnn_2.h5', model_url)\n","model = load_model( model_path )"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:11:21.092932Z","iopub.status.busy":"2022-04-12T16:11:21.092424Z","iopub.status.idle":"2022-04-12T16:11:21.481942Z","shell.execute_reply":"2022-04-12T16:11:21.480889Z","shell.execute_reply.started":"2022-04-12T16:11:21.09288Z"},"trusted":true},"outputs":[],"source":["results(data, window_size, future, scaler, 'Volume USDT', X_train, y_train, df_train, date_train, model)"]},{"cell_type":"markdown","metadata":{},"source":["### LSTM\n"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:11:51.705242Z","iopub.status.busy":"2022-04-12T16:11:51.704703Z","iopub.status.idle":"2022-04-12T16:11:51.711702Z","shell.execute_reply":"2022-04-12T16:11:51.711075Z","shell.execute_reply.started":"2022-04-12T16:11:51.705204Z"},"trusted":true},"outputs":[],"source":["def lstm_builder(X,y):\n"," model = tf.keras.models.Sequential() \n"," model.add(LSTM(1, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))\n"," model.add(Dense(y.shape[1]))\n","\n"," optimizer = tf.keras.optimizers.Adam() \n"," model.compile(\n"," loss='huber',\n"," optimizer=optimizer,\n"," metrics=['mse']\n"," )\n"," \n"," callbacks = [EarlyStopping(monitor='val_loss', patience=20),\n"," ModelCheckpoint(filepath='best_model_lstm_2.h5', monitor='val_loss', save_best_only=True)]\n","\n"," model.fit(X, y, epochs=50, verbose=1, validation_split=0.1, callbacks = callbacks, batch_size=16)\n"," return model"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:11:52.512142Z","iopub.status.busy":"2022-04-12T16:11:52.511485Z","iopub.status.idle":"2022-04-12T16:12:36.292853Z","shell.execute_reply":"2022-04-12T16:12:36.291629Z","shell.execute_reply.started":"2022-04-12T16:11:52.512099Z"},"trusted":true},"outputs":[],"source":["model = lstm_builder(X_train, y_train)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:13:31.272784Z","iopub.status.busy":"2022-04-12T16:13:31.271853Z","iopub.status.idle":"2022-04-12T16:13:31.538668Z","shell.execute_reply":"2022-04-12T16:13:31.537648Z","shell.execute_reply.started":"2022-04-12T16:13:31.272726Z"},"trusted":true},"outputs":[],"source":["pd.DataFrame(model.history.history).plot()"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["# if you need a model trained, you can use this cell\n","import tensorflow as tf\n","from tensorflow.keras.models import load_model\n","from tensorflow.keras.utils import get_file\n","model_url = \"https://static-1300131294.cos.ap-shanghai.myqcloud.com/data/deep-learning/time-series/model/best_model_lstm_2.h5\"\n","model_path = get_file(\"best_model_lstm_2.h5\", model_url)\n","model = load_model(model_path)"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2022-04-12T16:13:40.93008Z","iopub.status.busy":"2022-04-12T16:13:40.929753Z","iopub.status.idle":"2022-04-12T16:13:41.680705Z","shell.execute_reply":"2022-04-12T16:13:41.67963Z","shell.execute_reply.started":"2022-04-12T16:13:40.930047Z"},"trusted":true},"outputs":[],"source":["results(data, window_size, future, scaler, 'Volume USDT', X_train, y_train, df_train, date_train, model)"]},{"cell_type":"markdown","metadata":{},"source":["# Acknowledgments\n","\n","Thanks to Taha Bouhsine for creating the open-source course [ WTS2: Time Series Forecasting with TF ](https://www.kaggle.com/code/skywolfmo/wts2-time-series-forecasting-with-tf). It inspires the majority of the content in this chapter."]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.9.16"}},"nbformat":4,"nbformat_minor":4}