{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import and Load Dataset","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-01-11T06:01:58.769466Z","iopub.execute_input":"2022-01-11T06:01:58.770153Z","iopub.status.idle":"2022-01-11T06:01:58.77415Z","shell.execute_reply.started":"2022-01-11T06:01:58.770111Z","shell.execute_reply":"2022-01-11T06:01:58.773311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-11T06:01:58.776804Z","iopub.execute_input":"2022-01-11T06:01:58.777366Z","iopub.status.idle":"2022-01-11T06:01:59.949028Z","shell.execute_reply.started":"2022-01-11T06:01:58.777322Z","shell.execute_reply":"2022-01-11T06:01:59.948052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_folder = \"../input/g-research-crypto-forecasting/\"\n!ls $data_folder","metadata":{"execution":{"iopub.status.busy":"2022-01-11T06:01:59.95052Z","iopub.execute_input":"2022-01-11T06:01:59.950868Z","iopub.status.idle":"2022-01-11T06:02:00.755512Z","shell.execute_reply.started":"2022-01-11T06:01:59.950835Z","shell.execute_reply":"2022-01-11T06:02:00.754121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crypto_df = pd.read_csv(data_folder + 'train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-11T06:02:00.757759Z","iopub.execute_input":"2022-01-11T06:02:00.75814Z","iopub.status.idle":"2022-01-11T06:02:55.20273Z","shell.execute_reply.started":"2022-01-11T06:02:00.758092Z","shell.execute_reply":"2022-01-11T06:02:55.201674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utils","metadata":{}},{"cell_type":"code","source":"dataset_ID = [crypto_df[crypto_df.Asset_ID==i] for i in range(14)]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\n# auxiliary function, from datetime to timestamp\ntotimestamp = lambda s: np.int32(time.mktime(datetime.strptime(s, \"%d/%m/%Y\").timetuple()))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_features(df):\n    df = df[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']].copy()\n    # TODO : Try several other features here !\n    return df\n","metadata":{"execution":{"iopub.status.busy":"2022-01-11T06:03:00.22145Z","iopub.execute_input":"2022-01-11T06:03:00.221776Z","iopub.status.idle":"2022-01-11T06:03:00.234098Z","shell.execute_reply.started":"2022-01-11T06:03:00.221748Z","shell.execute_reply":"2022-01-11T06:03:00.233135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ndef dataset_split(df):\n    dataset = df.dropna() # only use target is not NAN data\n    X, y = get_features(dataset), dataset[\"Target\"]\n    # For train input, only previous data than testset, set shuffle False.\n    X_train, X_test,y_train, y_test = train_test_split(X, y, test_size = 0.2, shuffle=False)\n    X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size = 0.5, shuffle=False)\n    X = {\"train\":X_train, \"eval\":X_val, \"test\": X_test}\n    y = {\"train\":y_train, \"eval\":y_val, \"test\": y_test}\n    return X, y","metadata":{"execution":{"iopub.status.busy":"2022-01-11T07:09:47.305983Z","iopub.execute_input":"2022-01-11T07:09:47.306271Z","iopub.status.idle":"2022-01-11T07:09:47.313189Z","shell.execute_reply.started":"2022-01-11T07:09:47.306235Z","shell.execute_reply":"2022-01-11T07:09:47.311842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model train function - Ensemble LGBM & XGboost\n","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBRegressor\nfrom lightgbm import LGBMRegressor","metadata":{"execution":{"iopub.status.busy":"2022-01-11T06:49:07.278371Z","iopub.execute_input":"2022-01-11T06:49:07.278781Z","iopub.status.idle":"2022-01-11T06:49:07.28454Z","shell.execute_reply.started":"2022-01-11T06:49:07.278743Z","shell.execute_reply":"2022-01-11T06:49:07.283317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import early_stopping as lgbm_early_stopping\nfrom xgboost.callback import EarlyStopping as xgb_early_stopping\n# early stopping modeling fitting. Model will train until validation socre doesn;t improve.","metadata":{"execution":{"iopub.status.busy":"2022-01-11T07:13:53.928951Z","iopub.execute_input":"2022-01-11T07:13:53.929243Z","iopub.status.idle":"2022-01-11T07:13:53.934211Z","shell.execute_reply.started":"2022-01-11T07:13:53.929213Z","shell.execute_reply":"2022-01-11T07:13:53.933178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lgbm_train(X: dict, y:dict, lr, verbose=0):\n    lgbm_model = LGBMRegressor(learning_rate=lr, verbosity=verbose)\n\n    print(\"\\nLGBM fitting\\n\")\n    lgbm_model.fit(X[\"train\"], y['train'], eval_set=[(X[\"eval\"],y[\"eval\"])], \n                    callbacks=[lgbm_early_stopping(20, first_metric_only= True)])\n    \n    return lgbm_model\n\ndef xgb_train(X: dict, y:dict, lr, verbose=0):\n    xgb_model = XGBRegressor(learning_rate=lr, verbosity=verbose)\n\n    print(\"\\nXGBoost fitting\\n\")\n    xgb_model.fit(X[\"train\"], y['train'], eval_set=[(X[\"eval\"],y[\"eval\"])], \n                   callbacks=[xgb_early_stopping(20, save_best = True)])\n    \n    return xgb_model","metadata":{"execution":{"iopub.status.busy":"2022-01-11T07:52:02.430979Z","iopub.status.idle":"2022-01-11T07:52:02.431761Z","shell.execute_reply.started":"2022-01-11T07:52:02.43148Z","shell.execute_reply":"2022-01-11T07:52:02.431509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Asset By Asset, Train and predict","metadata":{}},{"cell_type":"markdown","source":"### dataset timestamp info","metadata":{}},{"cell_type":"code","source":"beg = dataset_ID[0]['timestamp'].iloc[0].astype('datetime64[s]')\nend = dataset_ID[0]['timestamp'].iloc[-1].astype('datetime64[s]')\n\nprint('Asset 0 data goes from ', beg, 'to ', end)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T07:42:09.312499Z","iopub.execute_input":"2022-01-11T07:42:09.31358Z","iopub.status.idle":"2022-01-11T07:42:09.319931Z","shell.execute_reply.started":"2022-01-11T07:42:09.313541Z","shell.execute_reply":"2022-01-11T07:42:09.319248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train","metadata":{}},{"cell_type":"code","source":"lgbm_models = []\nxgb_models = []\nfor i in range(14):\n    print(f\"Current Asset: {i}\")\n    dataset = dataset_ID[i]\n    X,y = dataset_split(dataset)\n    # Customize lr.\n    lgbm = lgbm_train(X,y, lr=0.1)\n    xgb = xgb_train(X,y, lr=0.1)\n    lgbm_models.append(lgbm)\n    xgb_models.append(xgb)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T07:52:22.480709Z","iopub.execute_input":"2022-01-11T07:52:22.481013Z","iopub.status.idle":"2022-01-11T08:03:08.160594Z","shell.execute_reply.started":"2022-01-11T07:52:22.480984Z","shell.execute_reply":"2022-01-11T08:03:08.15871Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-01-11T08:17:48.418237Z","iopub.execute_input":"2022-01-11T08:17:48.419581Z","iopub.status.idle":"2022-01-11T08:17:48.427093Z","shell.execute_reply.started":"2022-01-11T08:17:48.419527Z","shell.execute_reply":"2022-01-11T08:17:48.426251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for example, asset = 5 plotting. Customize Possible. [0,13]\nasset_index = 5\n\ndataset = dataset_ID[asset_index]\nX,y = dataset_split(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-01-11T08:17:48.662452Z","iopub.execute_input":"2022-01-11T08:17:48.662752Z","iopub.status.idle":"2022-01-11T08:17:49.166709Z","shell.execute_reply.started":"2022-01-11T08:17:48.662719Z","shell.execute_reply":"2022-01-11T08:17:49.166057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_models[asset_index].predict(X[\"test\"].to_numpy())","metadata":{"execution":{"iopub.status.busy":"2022-01-11T08:19:40.905733Z","iopub.execute_input":"2022-01-11T08:19:40.906199Z","iopub.status.idle":"2022-01-11T08:19:40.933155Z","shell.execute_reply.started":"2022-01-11T08:19:40.906166Z","shell.execute_reply":"2022-01-11T08:19:40.931988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title(\"LGBM Results\")\nplt.plot(y['test'].to_numpy(), label=\"Ground Truth\")\nplt.plot(lgbm_models[asset_index].predict(X[\"test\"].to_numpy()), label=\"Prediction\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T08:20:22.117054Z","iopub.execute_input":"2022-01-11T08:20:22.11738Z","iopub.status.idle":"2022-01-11T08:20:22.569594Z","shell.execute_reply.started":"2022-01-11T08:20:22.11735Z","shell.execute_reply":"2022-01-11T08:20:22.568503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Zoom In LGBM Results","metadata":{}},{"cell_type":"code","source":"plt.title(\"LGBM Results\")\nplt.plot(y['test'][100:200].to_numpy(), label=\"Ground Truth\")\nplt.plot(lgbm_models[asset_index].predict(X[\"test\"][100:200].to_numpy()), label=\"Prediction\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T08:22:25.011772Z","iopub.execute_input":"2022-01-11T08:22:25.012395Z","iopub.status.idle":"2022-01-11T08:22:25.238686Z","shell.execute_reply.started":"2022-01-11T08:22:25.012343Z","shell.execute_reply":"2022-01-11T08:22:25.237544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title(\"XGBoost Results\")\nplt.plot(y['test'].to_numpy(), label=\"Ground Truth\")\nplt.plot(xgb_models[asset_index].predict(X[\"test\"].to_numpy()), label=\"Prediction\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T08:20:35.320856Z","iopub.execute_input":"2022-01-11T08:20:35.321447Z","iopub.status.idle":"2022-01-11T08:20:35.877233Z","shell.execute_reply.started":"2022-01-11T08:20:35.321399Z","shell.execute_reply":"2022-01-11T08:20:35.876183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Zoom In XGBoost Results","metadata":{}},{"cell_type":"code","source":"plt.title(\"XGBoost Results\")\nplt.plot(y['test'][100:200].to_numpy(), label=\"Ground Truth\")\nplt.plot(xgb_models[asset_index].predict(X[\"test\"][100:200].to_numpy()), label=\"Prediction\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-11T08:21:26.585186Z","iopub.execute_input":"2022-01-11T08:21:26.585516Z","iopub.status.idle":"2022-01-11T08:21:26.798137Z","shell.execute_reply.started":"2022-01-11T08:21:26.585486Z","shell.execute_reply":"2022-01-11T08:21:26.797512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}