{"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":"# G-Research | XGBoost\n\n### Acknowledgements 😍\nMy acknowledgments are given to:\n\n* [Yam Peleg. G-Research: XGBoost with GPU (Fit in 1min).](https://www.kaggle.com/yamqwe/g-research-xgboost-with-gpu-fit-in-1min)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"### My Previous Competition Notebooks\nHere you can check my other notebooks for this competition: \n* [G-Research Forecast | Overlap | Score = 0.9999](https://www.kaggle.com/maricinnamon/g-research-forecast-overlap-score-0-9999)\n* [G-Research | Exploratory Data Analysis | Lin Regr](https://www.kaggle.com/maricinnamon/g-research-exploratory-data-analysis-lin-regr)","metadata":{}},{"cell_type":"markdown","source":"# 1. Libraries","metadata":{}},{"cell_type":"code","source":"import traceback\nimport numpy as np\nimport pandas as pd\nimport xgboost as xgb\nimport datatable as dt\nimport gresearch_crypto\nfrom lightgbm import LGBMRegressor","metadata":{"execution":{"iopub.status.busy":"2022-01-26T14:21:25.125715Z","iopub.execute_input":"2022-01-26T14:21:25.126023Z","iopub.status.idle":"2022-01-26T14:21:25.205469Z","shell.execute_reply.started":"2022-01-26T14:21:25.125980Z","shell.execute_reply":"2022-01-26T14:21:25.204684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Read data\nHere we are going to use an **extra dataset**, that you can add to your notebook: [Cryptocurrency extra data - Binance Coin](https://www.kaggle.com/yamqwe/cryptocurrency-extra-data-binance-coin). This dataset is an extra updating dataset for the G-Research Crypto Forecasting competition.\n","metadata":{}},{"cell_type":"code","source":"TRAIN_JAY = '../input/cryptocurrency-extra-data-binance-coin/orig_train.jay'\nASSET_DETAILS_JAY = '../input/cryptocurrency-extra-data-binance-coin/orig_asset_details.jay'","metadata":{"execution":{"iopub.status.busy":"2022-01-26T14:14:20.815636Z","iopub.execute_input":"2022-01-26T14:14:20.816226Z","iopub.status.idle":"2022-01-26T14:14:20.820479Z","shell.execute_reply.started":"2022-01-26T14:14:20.816191Z","shell.execute_reply":"2022-01-26T14:14:20.819519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = dt.fread('../input/cryptocurrency-extra-data-binance-coin/orig_train.jay').to_pandas()\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-26T14:14:22.606882Z","iopub.execute_input":"2022-01-26T14:14:22.607415Z","iopub.status.idle":"2022-01-26T14:14:42.326546Z","shell.execute_reply.started":"2022-01-26T14:14:22.607377Z","shell.execute_reply":"2022-01-26T14:14:42.325870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_asset_details = dt.fread('../input/cryptocurrency-extra-data-binance-coin/orig_asset_details.jay').to_pandas().sort_values(\"Asset_ID\")\ndf_asset_details.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-26T14:18:15.494305Z","iopub.execute_input":"2022-01-26T14:18:15.494558Z","iopub.status.idle":"2022-01-26T14:18:15.514140Z","shell.execute_reply.started":"2022-01-26T14:18:15.494530Z","shell.execute_reply":"2022-01-26T14:18:15.513475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Feature Engineering","metadata":{}},{"cell_type":"code","source":"# Two new features from the competition tutorial\ndef upper_shadow(df):\n    return df['High'] - np.maximum(df['Close'], df['Open'])\n\n\ndef lower_shadow(df):\n    return np.minimum(df['Close'], df['Open']) - df['Low']\n\n\n# A utility function to build features from the original df\n# It works for rows to, so we can reutilize it.\ndef get_features(df):\n    df_feat = df[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']].copy()\n    df_feat['Upper_Shadow'] = upper_shadow(df_feat)\n    df_feat['Lower_Shadow'] = lower_shadow(df_feat)\n    return df_feat","metadata":{"execution":{"iopub.status.busy":"2022-01-26T14:21:47.329010Z","iopub.execute_input":"2022-01-26T14:21:47.329615Z","iopub.status.idle":"2022-01-26T14:21:47.336310Z","shell.execute_reply.started":"2022-01-26T14:21:47.329574Z","shell.execute_reply":"2022-01-26T14:21:47.335190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Define Train Function","metadata":{}},{"cell_type":"code","source":"def get_Xy_and_model_for_asset(df_train, asset_id):\n    df = df_train[df_train[\"Asset_ID\"] == asset_id]\n    \n    # TODO: Try different features here!\n    df_proc = get_features(df)\n    df_proc['y'] = df['Target']\n    df_proc = df_proc.dropna(how=\"any\")\n    \n    X = df_proc.drop(\"y\", axis=1)\n    y = df_proc[\"y\"]\n    \n    model = xgb.XGBRegressor(\n        n_estimators=500,\n        max_depth=11,\n        learning_rate=0.05,\n        subsample=0.9,\n        colsample_bytree=0.7,\n        missing=-999,\n        random_state=2020,\n        # tree_method='gpu_hist'  # THE MAGICAL PARAMETER\n    )\n    model.fit(X, y)\n\n    return X, y, model\n","metadata":{"execution":{"iopub.status.busy":"2022-01-26T14:22:37.777948Z","iopub.execute_input":"2022-01-26T14:22:37.778203Z","iopub.status.idle":"2022-01-26T14:22:37.784852Z","shell.execute_reply.started":"2022-01-26T14:22:37.778175Z","shell.execute_reply":"2022-01-26T14:22:37.783922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loop over all assets","metadata":{}},{"cell_type":"code","source":"Xs = {}\nys = {}\nmodels = {}\n\nfor asset_id, asset_name in zip(df_asset_details['Asset_ID'], \n                                df_asset_details['Asset_Name']):\n    \n    print(f\"Training model for {asset_name:<16} (ID={asset_id:<2})\")\n    \n    try:\n        X, y, model = get_Xy_and_model_for_asset(df_train, asset_id)    \n        Xs[asset_id], ys[asset_id], models[asset_id] = X, y, model\n    except:         \n        Xs[asset_id], ys[asset_id], models[asset_id] = None, None, None   ","metadata":{"execution":{"iopub.status.busy":"2022-01-26T14:25:09.491656Z","iopub.execute_input":"2022-01-26T14:25:09.492434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the model interface\nx = get_features(df_train.iloc[1])\ny_pred = models[0].predict(pd.DataFrame([x]))\ny_pred[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Submit to Cometition","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        \n        if models[row['Asset_ID']] is not None:\n            try:\n                model = models[row['Asset_ID']]\n                x_test = get_features(row)\n                y_pred = model.predict(pd.DataFrame([x_test]))[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        \n    env.predict(df_pred)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### *If you liked it, please, make an upvote* 💖","metadata":{}}]}