{"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 Crypto - Beginners Pipeline \n\nWe train regressor from different algorithms, one is `LGBMRegressor`, `XGBRegressor` & last `CatBoostRegressor`. Dataset of features from input frame are (`['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']`), we get the predictions correctly using the iterator and we submit.\n\n## References:\n* [G-Research Crypto - Starter XGB Pipeline](https://www.kaggle.com/tarlannazarov/g-research-crypto-starter-xgb-pipeline)\n* [Basic Submission Template](https://www.kaggle.com/sohier/basic-submission-template)\n* [CatBoost Regressor](https://www.kaggle.com/yamqwe/crypto-prediction-catboost-regressor#Predict-&-submit)","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:53:40.783338Z","iopub.execute_input":"2021-11-18T08:53:40.783893Z","iopub.status.idle":"2021-11-18T08:53:40.821785Z","shell.execute_reply.started":"2021-11-18T08:53:40.783762Z","shell.execute_reply":"2021-11-18T08:53:40.820845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Different Regressor libraries \nfrom lightgbm import LGBMRegressor # LGBM Regressor\nimport xgboost as xgb # XGB Regressor\nfrom catboost import CatBoostRegressor # CatBoost Resgressor\nimport gresearch_crypto\nimport traceback","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:53:40.823389Z","iopub.execute_input":"2021-11-18T08:53:40.823664Z","iopub.status.idle":"2021-11-18T08:53:43.193436Z","shell.execute_reply.started":"2021-11-18T08:53:40.823634Z","shell.execute_reply":"2021-11-18T08:53:43.192744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading & Feature Modification","metadata":{}},{"cell_type":"code","source":"# Read the Crypto dataset files\ndf_train = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/train.csv')\ndf_asset = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/asset_details.csv').sort_values(\"Asset_ID\")","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:53:43.194862Z","iopub.execute_input":"2021-11-18T08:53:43.195324Z","iopub.status.idle":"2021-11-18T08:54:47.237128Z","shell.execute_reply.started":"2021-11-18T08:53:43.195291Z","shell.execute_reply":"2021-11-18T08:54:47.235922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Modify the training dataset\ndf_train.replace([np.inf, -np.inf], np.nan, inplace=True)\ndf_train = df_train.dropna()\n\n# Modify the Assest Dataset\ndf_asset.replace([np.inf, -np.inf], np.nan, inplace=True)\ndf_asset = df_asset.dropna()","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:54:47.240875Z","iopub.execute_input":"2021-11-18T08:54:47.241236Z","iopub.status.idle":"2021-11-18T08:54:50.628963Z","shell.execute_reply.started":"2021-11-18T08:54:47.241189Z","shell.execute_reply":"2021-11-18T08:54:50.62797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Asset dataset details are following.\ndf_asset","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:54:50.630436Z","iopub.execute_input":"2021-11-18T08:54:50.630761Z","iopub.status.idle":"2021-11-18T08:54:50.652488Z","shell.execute_reply.started":"2021-11-18T08:54:50.6307Z","shell.execute_reply":"2021-11-18T08:54:50.651809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Utility Function\nThese function are used to create the some new features which are useful for the model.\nSpecial thanks to @DATAISTA0 (JULIÁN PELLER) [notebook](https://www.kaggle.com/julian3833/g-research-starter-lgbm-pipeline) for functionality.","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\ndef lower_shadow(df):\n    return np.minimum(df['Close'], df['Open']) - df['Low']\n\n# Extract Asset features\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\n\n# Extract features from dataset\ndef get_Xy_features(df_train, df_asset_id):\n    # Modfify with respect to 'asset ID'\n    df = df_train[df_train[\"Asset_ID\"] == df_asset_id]\n    df_proc = get_features(df)\n    df_proc['y'] = df['Target']\n    df_proc = df_proc.dropna(how=\"any\")\n    \n    # X, y features from the dataset.\n    X = df_proc.drop(\"y\", axis=1)\n    y = df_proc[\"y\"]\n    \n    return X, y","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:54:50.654071Z","iopub.execute_input":"2021-11-18T08:54:50.655916Z","iopub.status.idle":"2021-11-18T08:54:50.66626Z","shell.execute_reply.started":"2021-11-18T08:54:50.655864Z","shell.execute_reply":"2021-11-18T08:54:50.665258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Implementation: LGBM Regressor","metadata":{}},{"cell_type":"code","source":"# LGBM Regressor Model Function\ndef LGBMRegressor_model(X, y):\n        \n    # LGBM Regresor Model \n    model = LGBMRegressor(n_estimators=40) # 40000\n    model.fit(X, y)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:54:50.667588Z","iopub.execute_input":"2021-11-18T08:54:50.66791Z","iopub.status.idle":"2021-11-18T08:54:50.684886Z","shell.execute_reply.started":"2021-11-18T08:54:50.667868Z","shell.execute_reply":"2021-11-18T08:54:50.683814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calling the implemenation of LGBM Regressor\nmodel_lgb = {}\n\nfor asset_id, asset_name in zip(df_asset['Asset_ID'], df_asset['Asset_Name']):\n    print(f\"Training model for {asset_name:<16} (ID={asset_id:<2})\")\n    X, y = get_Xy_features(df_train, asset_id)\n    model = LGBMRegressor_model(X, y)\n    \n    model_lgb[asset_id] = model","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:54:50.686213Z","iopub.execute_input":"2021-11-18T08:54:50.686708Z","iopub.status.idle":"2021-11-18T08:55:24.058609Z","shell.execute_reply.started":"2021-11-18T08:54:50.686672Z","shell.execute_reply":"2021-11-18T08:55:24.057868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking LGBM Regressor Model interface\nx = get_features(df_train.iloc[1])\ny_pred_lgb = model_lgb[0].predict([x])\ny_pred_lgb[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:55:24.06001Z","iopub.execute_input":"2021-11-18T08:55:24.060747Z","iopub.status.idle":"2021-11-18T08:55:24.072066Z","shell.execute_reply.started":"2021-11-18T08:55:24.060692Z","shell.execute_reply":"2021-11-18T08:55:24.071314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Delete unused dataset\ndel x\ndel y_pred_lgb","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:55:24.077642Z","iopub.execute_input":"2021-11-18T08:55:24.078267Z","iopub.status.idle":"2021-11-18T08:55:24.082143Z","shell.execute_reply.started":"2021-11-18T08:55:24.07822Z","shell.execute_reply":"2021-11-18T08:55:24.08148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Implementation: XGB Regressor","metadata":{}},{"cell_type":"code","source":"# LGBM Regressor Model Function\ndef XGBRegressor_model(X, y):\n    \n    model = xgb.XGBRegressor(\n    n_estimators=50, #500\n    learning_rate=0.05,\n    max_depth=12,\n    subsample=0.9,\n    colsample_bytree=0.7,\n    missing=-999,\n    random_state=1111,\n    tree_method='hist'  \n    )\n    \n    model.fit(X, y)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:55:24.085374Z","iopub.execute_input":"2021-11-18T08:55:24.085627Z","iopub.status.idle":"2021-11-18T08:55:24.095435Z","shell.execute_reply.started":"2021-11-18T08:55:24.085596Z","shell.execute_reply":"2021-11-18T08:55:24.094302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calling the implemenation of XGB Regressor\nmodel_xgb = {}\n\nfor asset_id, asset_name in zip(df_asset['Asset_ID'], df_asset['Asset_Name']):\n    print(f\"Training model for {asset_name:<16} (ID={asset_id:<2})\")\n    X, y = get_Xy_features(df_train, asset_id)\n    model = XGBRegressor_model(X, y)\n    \n    model_xgb[asset_id] = model","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:55:24.096787Z","iopub.execute_input":"2021-11-18T08:55:24.097247Z","iopub.status.idle":"2021-11-18T08:56:07.181898Z","shell.execute_reply.started":"2021-11-18T08:55:24.097212Z","shell.execute_reply":"2021-11-18T08:56:07.18108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking XGB Regressor Model interface\nx = get_features(df_train.iloc[1])\ny_pred_xgb = model_xgb[0].predict(pd.DataFrame([x]))\ny_pred_xgb[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:56:07.185173Z","iopub.execute_input":"2021-11-18T08:56:07.187419Z","iopub.status.idle":"2021-11-18T08:56:07.238174Z","shell.execute_reply.started":"2021-11-18T08:56:07.187371Z","shell.execute_reply":"2021-11-18T08:56:07.237221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Delete unused dataset\ndel x\ndel y_pred_xgb","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:56:07.239459Z","iopub.execute_input":"2021-11-18T08:56:07.239779Z","iopub.status.idle":"2021-11-18T08:56:07.245012Z","shell.execute_reply.started":"2021-11-18T08:56:07.239738Z","shell.execute_reply":"2021-11-18T08:56:07.243937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Implementation: CatBoost Regressor","metadata":{}},{"cell_type":"code","source":"# Features for CatBoost Regressor\ndef get_features_catboost(df,row=False):\n    df_feat = df[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP', \"timestamp\"]].copy()\n    df_feat['Upper_Shadow'] = upper_shadow(df_feat)\n    df_feat['Lower_Shadow'] = lower_shadow(df_feat)\n    \n    ## Add some more feats\n    df_feat[\"high_div_low\"] = df_feat[\"High\"]/df_feat[\"Low\"]\n    df_feat[\"open_sub_close\"] = df_feat[\"Open\"]-df_feat[\"Close\"]\n\n    ## possible seasonality, datetime  features (unlikely to me meaningful, given very short time-frames)\n    times = pd.to_datetime(df_feat[\"timestamp\"],unit=\"s\",infer_datetime_format=True)\n    if row:\n        df_feat[\"hour\"] = times.hour\n        df_feat[\"dayofweek\"] = times.dayofweek \n        df_feat[\"day\"] = times.day \n    else:\n        df_feat[\"hour\"] = times.dt.hour\n        df_feat[\"dayofweek\"] = times.dt.dayofweek \n        df_feat[\"day\"] = times.dt.day \n    df_feat = df_feat.drop(columns = \"timestamp\")\n    return df_feat\n\n# X, y Features from dataset\ndef get_Xy_and_Catboost(df_train, asset_id):\n    df = df_train[df_train[\"Asset_ID\"] == asset_id]\n    \n    df_proc = get_features_catboost(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    return X, y","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:56:07.246543Z","iopub.execute_input":"2021-11-18T08:56:07.247031Z","iopub.status.idle":"2021-11-18T08:56:07.261776Z","shell.execute_reply.started":"2021-11-18T08:56:07.246987Z","shell.execute_reply":"2021-11-18T08:56:07.260798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Implementation of CatBoost Regressor\ndef CatBoostRegressor_model(X, y):\n    model = CatBoostRegressor(iterations=20, learning_rate=0.05, depth=10, random_seed=42) #1000\n    model.fit(X, y)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:56:07.265279Z","iopub.execute_input":"2021-11-18T08:56:07.26589Z","iopub.status.idle":"2021-11-18T08:56:07.273356Z","shell.execute_reply.started":"2021-11-18T08:56:07.265853Z","shell.execute_reply":"2021-11-18T08:56:07.272414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calling of CatBoost Regressor Model\nmodel_cat = {}\n\nfor asset_id, asset_name in zip(df_asset['Asset_ID'], df_asset['Asset_Name']):\n    print(f\"Training model for {asset_name:<16} (ID={asset_id:<2})\")\n    X, y = get_Xy_and_Catboost(df_train, asset_id)\n    model = CatBoostRegressor_model(X, y)\n    \n    model_cat[asset_id] = model","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:56:07.274873Z","iopub.execute_input":"2021-11-18T08:56:07.275104Z","iopub.status.idle":"2021-11-18T08:58:17.737473Z","shell.execute_reply.started":"2021-11-18T08:56:07.27507Z","shell.execute_reply":"2021-11-18T08:58:17.736639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the CatBoost model interface\nx = get_features_catboost(df_train.iloc[1],row=True)\ny_pred_cat = model_cat[0].predict([x])\ny_pred_cat[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:58:17.738656Z","iopub.execute_input":"2021-11-18T08:58:17.738922Z","iopub.status.idle":"2021-11-18T08:58:17.755644Z","shell.execute_reply.started":"2021-11-18T08:58:17.73889Z","shell.execute_reply":"2021-11-18T08:58:17.754604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Delete unused dataset\ndel x\ndel y_pred_cat","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:58:17.75685Z","iopub.execute_input":"2021-11-18T08:58:17.757139Z","iopub.status.idle":"2021-11-18T08:58:17.761421Z","shell.execute_reply.started":"2021-11-18T08:58:17.75706Z","shell.execute_reply":"2021-11-18T08:58:17.760601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Final Prediction: LGBM + XGB + CatBoost","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    df_pred_lgbr = df_pred\n    df_pred_xgbr = df_pred\n    df_pred_cbtr = df_pred\n    for j , row in df_test.iterrows():\n        \n        # LGBM Regressor\n        model_lgbr = model_lgb[row['Asset_ID']]\n        x_test_lgbr = get_features(row)\n        y_pred_lgbr = model_lgbr.predict([x_test_lgbr])[0]\n        \n        df_pred_lgbr.loc[df_pred_lgbr['row_id'] == row['row_id'], 'Target'] = y_pred_lgbr\n        \n        # XGB Regressor\n        model_xgbr = model_xgb[row['Asset_ID']]\n        x_test_xgbr = get_features(row)\n        y_pred_xgbr = model_xgbr.predict(pd.DataFrame([x_test_xgbr]))[0]\n        \n        df_pred_xgbr.loc[df_pred_xgbr['row_id'] == row['row_id'], 'Target'] = y_pred_xgbr\n        \n        # CatBoost Regressor\n        model_cbtr = model_cat[row['Asset_ID']]\n        x_test_cbtr = get_features_catboost(row, row=True)\n        y_pred_cbtr = model_cbtr.predict([x_test_cbtr])[0]\n        \n        df_pred_cbtr.loc[df_pred_cbtr['row_id'] == row['row_id'], 'Target'] = y_pred_cbtr\n\n    # Display the first prediction dataframe\n    if i == 0:\n        pred_final = 0.5 * df_pred_lgbr + 0.2 * df_pred_xgbr +  0.3 * df_pred_cbtr\n        display(pred_final)\n\n    # Send submissions\n    env.predict(pred_final)","metadata":{"execution":{"iopub.status.busy":"2021-11-18T08:58:17.762872Z","iopub.execute_input":"2021-11-18T08:58:17.763094Z","iopub.status.idle":"2021-11-18T08:58:20.138746Z","shell.execute_reply.started":"2021-11-18T08:58:17.763068Z","shell.execute_reply":"2021-11-18T08:58:20.13804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**In the Final prediction there are some computational error occurs due to whcih submission is not happening but still if you like the effort please upvote.!!!**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}