{"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":"Here I tried to use Light AutoML library developed by Sber. But as competition rules prohibits use of internet, we can't use it in submission.\nSo play with it and see what results you can get!","metadata":{}},{"cell_type":"code","source":"!pip install -U lightautoml","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import traceback\nimport numpy as np\nimport pandas as pd\nimport datatable as dt\nimport gresearch_crypto\n\npath = \"../input/g-research-crypto-forecasting/\"\ndf_train = pd.read_csv(path + \"train.csv\")\ndf_test = pd.read_csv(path + \"example_test.csv\")\ndf_asset_details = pd.read_csv(path + \"asset_details.csv\")\ndf_supp_train = pd.read_csv(path + \"supplemental_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-17T11:43:54.304656Z","iopub.execute_input":"2022-01-17T11:43:54.304955Z","iopub.status.idle":"2022-01-17T11:45:05.932275Z","shell.execute_reply.started":"2022-01-17T11:43:54.304928Z","shell.execute_reply":"2022-01-17T11:45:05.93111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T11:45:58.92682Z","iopub.execute_input":"2022-01-17T11:45:58.927169Z","iopub.status.idle":"2022-01-17T11:45:58.968594Z","shell.execute_reply.started":"2022-01-17T11:45:58.927135Z","shell.execute_reply":"2022-01-17T11:45:58.967249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_asset_details","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:31:56.525879Z","iopub.execute_input":"2022-01-14T11:31:56.526408Z","iopub.status.idle":"2022-01-14T11:31:56.540134Z","shell.execute_reply.started":"2022-01-14T11:31:56.526371Z","shell.execute_reply":"2022-01-14T11:31:56.538966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def hlco_ratio(df): return (df['High'] - df['Low'])/(df['Close'] - df['Open'])\ndef upper_shadow(df): return df['High'] - np.maximum(df['Close'], df['Open'])\ndef lower_shadow(df): return np.minimum(df['Close'], df['Open']) - df['Low']\n\ndef get_features(df):\n    df_feat = df[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP', 'Target', 'Asset_ID', 'timestamp']].copy()\n    df_feat['Upper_Shadow'] = upper_shadow(df_feat)\n    df_feat['Lower_Shadow'] = lower_shadow(df_feat)\n    df_feat['hlco_ration'] = hlco_ratio(df_feat)\n    return df_feat\n","metadata":{"execution":{"iopub.status.busy":"2022-01-17T11:47:56.646593Z","iopub.execute_input":"2022-01-17T11:47:56.646893Z","iopub.status.idle":"2022-01-17T11:47:56.657075Z","shell.execute_reply.started":"2022-01-17T11:47:56.646858Z","shell.execute_reply":"2022-01-17T11:47:56.656147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.replace([np.inf, -np.inf], np.nan, inplace=True)\ndf_train = df_train.dropna(how=\"any\")\n\ntrain_data = get_features(df_train)\ntrain_data = df_train.sample(frac = 0.8)\nvalid_data = df_train.drop(train_data.index)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T11:48:14.991573Z","iopub.execute_input":"2022-01-17T11:48:14.99187Z","iopub.status.idle":"2022-01-17T11:48:31.345957Z","shell.execute_reply.started":"2022-01-17T11:48:14.99184Z","shell.execute_reply":"2022-01-17T11:48:31.345411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T14:27:15.031676Z","iopub.execute_input":"2022-01-17T14:27:15.032404Z","iopub.status.idle":"2022-01-17T14:27:15.046349Z","shell.execute_reply.started":"2022-01-17T14:27:15.032375Z","shell.execute_reply":"2022-01-17T14:27:15.044793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T10:00:11.862718Z","iopub.execute_input":"2022-01-17T10:00:11.86303Z","iopub.status.idle":"2022-01-17T10:00:11.88923Z","shell.execute_reply.started":"2022-01-17T10:00:11.862998Z","shell.execute_reply":"2022-01-17T10:00:11.888456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightautoml.automl.presets.tabular_presets import TabularAutoML\nfrom lightautoml.tasks import Task\nfrom scipy.spatial.distance import correlation\nimport scipy\n\nautoml = {}\n\ndef metric(y_true, y_pred):\n    print(np.std(y_pred))\n    if np.std(y_pred) == 0.:\n        return 0\n    p = scipy.stats.pearsonr(y_true, y_pred)[0]\n    print(f'pearson {p}')\n    return p\n\nfor asset_id, asset_name in zip(df_asset_details['Asset_ID'], df_asset_details['Asset_Name']):\n    automl[asset_id] = TabularAutoML(\n        task = Task(\n            name = 'reg')\n    #     general_params = {'nested_cv': False},\n    )\n\n    btc_data = train_data[train_data['Asset_ID'] == asset_id]\n\n    oof_pred = automl[asset_id].fit_predict(\n        btc_data,\n        roles = {'target': 'Target', 'drop': ['timestamp', 'Asset_ID']},\n        valid_data = valid_data[valid_data['Asset_ID'] == asset_id]\n    )\n# test_pred = automl[asset_id].predict(valid_data)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T14:01:53.306417Z","iopub.execute_input":"2022-01-17T14:01:53.306756Z","iopub.status.idle":"2022-01-17T14:19:00.224108Z","shell.execute_reply.started":"2022-01-17T14:01:53.306723Z","shell.execute_reply":"2022-01-17T14:19:00.223019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-17T12:22:38.996791Z","iopub.execute_input":"2022-01-17T12:22:38.99716Z","iopub.status.idle":"2022-01-17T12:22:39.005509Z","shell.execute_reply.started":"2022-01-17T12:22:38.997121Z","shell.execute_reply":"2022-01-17T12:22:39.004012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from lightautoml.automl.presets.tabular_presets import TabularAutoML\n# from lightautoml.tasks import Task\n# from scipy.spatial.distance import correlation\n\n# automl = {}\n\n# for asset_id, asset_name in zip(df_asset_details['Asset_ID'], df_asset_details['Asset_Name']):\n#     automl[asset_id] = TabularAutoML(\n#         task = Task(\n#             name = 'reg',\n#             general_params = {'nested_cv': False, 'use_algos': [['linear_l2', 'lgb', 'lgb_tuned']]},\n#             metric = lambda y_true, y_pred: correlation(y_true, (y_pred > 0.5)*1))\n#     )\n#     oof_pred = automl[asset_id].fit_predict(\n#         train_data[train_data['Asset_ID'] == asset_id],\n#         roles = {'target': 'Target', 'drop': ['timestamp', 'Asset_ID']}\n#     )\n#     test_pred = automl[asset_id].predict(valid_data)","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:21:28.364482Z","iopub.execute_input":"2022-01-14T11:21:28.365539Z","iopub.status.idle":"2022-01-14T11:21:40.559303Z","shell.execute_reply.started":"2022-01-14T11:21:28.365492Z","shell.execute_reply":"2022-01-14T11:21:40.557892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# valid_target = valid_data[\"Target\"].to_numpy()\n# preds = [n.data[0] for n in test_pred]","metadata":{"execution":{"iopub.status.busy":"2022-01-17T13:14:02.389891Z","iopub.execute_input":"2022-01-17T13:14:02.391162Z","iopub.status.idle":"2022-01-17T13:14:02.39694Z","shell.execute_reply.started":"2022-01-17T13:14:02.39107Z","shell.execute_reply":"2022-01-17T13:14:02.395509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n# plt.figure(figsize=(12,6))\n# plt.plot(valid_target)\n# plt.plot(preds)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T14:23:28.64272Z","iopub.execute_input":"2022-01-17T14:23:28.642996Z","iopub.status.idle":"2022-01-17T14:23:30.227419Z","shell.execute_reply.started":"2022-01-17T14:23:28.642969Z","shell.execute_reply":"2022-01-17T14:23:30.225463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = valid_data.iloc[2].drop('Target')\ny_pred = automl[1].predict(pd.DataFrame([x_test]))[0].data[0]\nprint(x_test)\nprint(y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T14:28:12.844136Z","iopub.execute_input":"2022-01-17T14:28:12.845388Z","iopub.status.idle":"2022-01-17T14:28:12.871925Z","shell.execute_reply.started":"2022-01-17T14:28:12.845309Z","shell.execute_reply":"2022-01-17T14:28:12.871075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Score your model","metadata":{}},{"cell_type":"code","source":"corr = np.corrcoef(preds, valid_target)\nprint('Test score for BTC: ', f\"{corr[0,1]:.4f}\")\nimport scipy\ncorr_p = scipy.stats.pearsonr(preds, valid_target)\ncorr_p[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-17T14:22:15.762323Z","iopub.execute_input":"2022-01-17T14:22:15.762724Z","iopub.status.idle":"2022-01-17T14:22:16.18676Z","shell.execute_reply.started":"2022-01-17T14:22:15.762684Z","shell.execute_reply":"2022-01-17T14:22:16.186101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Results Submission","metadata":{}},{"cell_type":"code","source":"env = gresearch_crypto.make_env()\niter_test = env.iter_test()\n\nfor i, (df_test, df_pred) in enumerate(iter_test):\n    for j , row in df_test.iterrows():        \n        if automl[row['Asset_ID']] is not None:\n            try:\n                model = automl[row['Asset_ID']]\n                x_test = get_features(row)\n                y_pred = model.predict(pd.DataFrame([x_test]))[0].data[0]\n                df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = y_pred\n            except:\n                df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = 0\n                traceback.print_exc()\n        else: \n            df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = 0        \n    env.predict(df_pred)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-14T11:24:28.073375Z","iopub.execute_input":"2022-01-14T11:24:28.074151Z","iopub.status.idle":"2022-01-14T11:24:28.88556Z","shell.execute_reply.started":"2022-01-14T11:24:28.074094Z","shell.execute_reply":"2022-01-14T11:24:28.88453Z"},"trusted":true},"execution_count":null,"outputs":[]}]}