{"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":"code","source":"from datetime import datetime\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport gc\n\nfrom xgboost import XGBRegressor\nfrom lightgbm import LGBMRegressor\n\nimport gresearch_crypto\nenv = gresearch_crypto.make_env()\niter_test = env.iter_test()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-06T14:03:40.967843Z","iopub.execute_input":"2022-01-06T14:03:40.968324Z","iopub.status.idle":"2022-01-06T14:03:40.996764Z","shell.execute_reply.started":"2022-01-06T14:03:40.968286Z","shell.execute_reply":"2022-01-06T14:03:40.99597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_time_features(df):\n    # Feature engineering\n    df = df.assign(ds=df.index.values.astype('datetime64[s]'))\n\n    # Calendar features\n    # df = df.assign(quarter = pd.to_datetime(df['ds']).dt.quarter)\n    df = df.assign(month = df['ds'].dt.month)\n#     df = df.assign(year = df['ds'].dt.year)\n    df = df.assign(dayofweek = df['ds'].dt.dayofweek)\n    df = df.assign(dayofmonth = df['ds'].dt.day)\n    df = df.assign(dayofyear = df['ds'].dt.dayofyear)\n\n    df = df.assign(hour = df['ds'].dt.hour)\n    df = df.assign(minute = df['ds'].dt.minute)\n\n    return df\n\n\ndef plot_ts(df, c='Target', n_cols = 4):\n    assets = df.Asset_ID.unique()\n\n    n_rows = int(np.ceil(len(assets) /n_cols))\n\n    f,ax = plt.subplots(n_rows, n_cols, figsize=(20,10))\n\n    i, j = 0, 0\n\n    for asset in assets:\n        # Retrieve name from the asset id\n        asset_name = asset_details_df[asset_details_df.Asset_ID == asset].Asset_Name.iloc[0]\n\n        # Filter dataframe to get only data from asset id\n        sub_df = df[df.Asset_ID == asset]\n\n        ax[i][j].plot(sub_df[c], label=asset_name)\n        ax[i][j].legend()\n    #     ax[i][j].xlabel('Time')\n    #     ax[i][j].ylabel(asset_name)\n\n        j += 1\n        j = j%n_cols\n\n        if j == 0:\n            i += 1","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:03:41.21971Z","iopub.execute_input":"2022-01-06T14:03:41.22003Z","iopub.status.idle":"2022-01-06T14:03:41.236292Z","shell.execute_reply.started":"2022-01-06T14:03:41.219998Z","shell.execute_reply":"2022-01-06T14:03:41.234406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder = '/kaggle/input/g-research-crypto-forecasting/'\ntrain_df = pd.read_csv(folder + 'train.csv')\nasset_details_df = pd.read_csv(folder + 'asset_details.csv')\nexample_test_df = pd.read_csv(folder + 'example_test.csv')\n# example_submission_df = pd.read_csv(folder + 'example_sample_submission.csv')\nsupplemental_train_df = pd.read_csv(folder + 'supplemental_train.csv')\n# asset_details_df = pd.read_csv(folder + 'asset_details.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:03:41.361625Z","iopub.execute_input":"2022-01-06T14:03:41.361834Z","iopub.status.idle":"2022-01-06T14:04:15.738871Z","shell.execute_reply.started":"2022-01-06T14:03:41.36181Z","shell.execute_reply":"2022-01-06T14:04:15.738136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_test_df.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:04:15.740579Z","iopub.execute_input":"2022-01-06T14:04:15.74082Z","iopub.status.idle":"2022-01-06T14:04:15.753701Z","shell.execute_reply.started":"2022-01-06T14:04:15.740787Z","shell.execute_reply":"2022-01-06T14:04:15.752979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:04:15.75503Z","iopub.execute_input":"2022-01-06T14:04:15.755489Z","iopub.status.idle":"2022-01-06T14:04:15.775758Z","shell.execute_reply.started":"2022-01-06T14:04:15.755445Z","shell.execute_reply":"2022-01-06T14:04:15.774999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_details_df.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:04:15.777724Z","iopub.execute_input":"2022-01-06T14:04:15.778026Z","iopub.status.idle":"2022-01-06T14:04:15.788813Z","shell.execute_reply.started":"2022-01-06T14:04:15.777992Z","shell.execute_reply":"2022-01-06T14:04:15.788035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"supplemental_train_df.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:04:15.790402Z","iopub.execute_input":"2022-01-06T14:04:15.791372Z","iopub.status.idle":"2022-01-06T14:04:15.805067Z","shell.execute_reply.started":"2022-01-06T14:04:15.791301Z","shell.execute_reply":"2022-01-06T14:04:15.804309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cutoff_ds = '2021-01-01'\ntrain_df = train_df.set_index(\"timestamp\")\ntrain_df = train_df.assign(ds=train_df.index.values.astype('datetime64[s]'))\ntrain_df = train_df[train_df.ds >= cutoff_ds]\ntrain_df = add_time_features(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:04:15.806429Z","iopub.execute_input":"2022-01-06T14:04:15.80686Z","iopub.status.idle":"2022-01-06T14:04:25.183628Z","shell.execute_reply.started":"2022-01-06T14:04:15.80671Z","shell.execute_reply":"2022-01-06T14:04:25.182883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df[(~train_df.Target.isna()) & (train_df.VWAP != np.float('inf'))].drop(['ds', 'Target'], axis=1)\ny = train_df[(~train_df.Target.isna()) & (train_df.VWAP != np.float('inf'))]['Target']\n\n# model = XGBRegressor()\nlgb_params = {\n    \"objective\": \"regression\",\n    \"n_estimators\" : 2000,\n    \"num_leaves\" : 300,\n    \"learning_rate\" : 0.09,\n    \"random_seed\" : 50,\n#     \"device\": 'gpu'\n}\n\nmodel = LGBMRegressor(**lgb_params)\nmodel.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:04:25.185025Z","iopub.execute_input":"2022-01-06T14:04:25.185294Z","iopub.status.idle":"2022-01-06T14:05:14.03357Z","shell.execute_reply.started":"2022-01-06T14:04:25.18526Z","shell.execute_reply":"2022-01-06T14:05:14.0329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (test_df, sample_prediction_df) in iter_test:\n    test_df = test_df.set_index(\"timestamp\")\n    test_df = test_df.assign(ds=test_df.index.values.astype('datetime64[s]'))\n    test_df = add_time_features(test_df)\n    sample_prediction_df['Target'] = model.predict(test_df.drop(['ds', 'row_id'], axis=1))\n    env.predict(sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T12:42:04.464443Z","iopub.execute_input":"2022-01-06T12:42:04.465552Z","iopub.status.idle":"2022-01-06T12:42:04.557452Z","shell.execute_reply.started":"2022-01-06T12:42:04.465505Z","shell.execute_reply":"2022-01-06T12:42:04.556517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}