{"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 Forecasting | \"data leakage\" | Public Leaderboard = 0.9999\n\n### Acknowledgements\nMy acknowledgments are given to: [dataista0 (Julián Peller). 🪙💲 G-Research- Using the overlap fully [LB=0.99]](https://www.kaggle.com/julian3833/g-research-using-the-overlap-fully-lb-0-99). I saw his public notebook and decided to copy and try by myself.\n\n> As author says, \"the test period is contained in the train data to get the closest point for each test row. It is not actually leaky because the fact that the LB is part of the public data was publicly disclosed by the organizers.\"\n\nAs you can see, this code doesn't include any model training. And, I think, that it will not suitable for private leaderboard, if it will be calculated with another set of data. That is why it is just an example, how to submit to the competition without any difficult work. ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport gresearch_crypto","metadata":{"execution":{"iopub.status.busy":"2022-01-19T10:19:25.638738Z","iopub.execute_input":"2022-01-19T10:19:25.639116Z","iopub.status.idle":"2022-01-19T10:19:25.692776Z","shell.execute_reply.started":"2022-01-19T10:19:25.639005Z","shell.execute_reply":"2022-01-19T10:19:25.692073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = gresearch_crypto.make_env()\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T10:19:25.69411Z","iopub.execute_input":"2022-01-19T10:19:25.694444Z","iopub.status.idle":"2022-01-19T10:19:25.698402Z","shell.execute_reply.started":"2022-01-19T10:19:25.694416Z","shell.execute_reply":"2022-01-19T10:19:25.697776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/train.csv', \n                 usecols=['Target', \n                          'Asset_ID',\n                          'timestamp'], \n                 dtype={'Asset_ID': 'int8'})\n\ndf['datetime'] = pd.to_datetime(df['timestamp'], unit='s')\n\ndf = df.set_index('datetime').drop('timestamp', axis=1)\ndf = df[(df.index.year == 2021) & (df.index.month > 5)]\n\ndfs = {asset_id: df[df['Asset_ID'] == asset_id].resample('1min').interpolate().copy() for asset_id in df['Asset_ID'].unique()}\n\ndel df","metadata":{"execution":{"iopub.status.busy":"2022-01-19T10:19:25.699545Z","iopub.execute_input":"2022-01-19T10:19:25.699972Z","iopub.status.idle":"2022-01-19T10:20:22.071001Z","shell.execute_reply.started":"2022-01-19T10:19:25.699942Z","shell.execute_reply":"2022-01-19T10:20:22.070092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for df_test, df_pred in iter_test:\n    df_test['datetime'] = pd.to_datetime(df_test['timestamp'], unit='s')\n    for _, row in df_test.iterrows():\n        try:\n            df = dfs[row['Asset_ID']]\n            closest_train_sample = df.iloc[df.index.get_loc(row['datetime'], method='nearest')]\n            df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = closest_train_sample['Target']\n        except:\n            df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = 0\n    df_pred['Target'] = df_pred['Target'].fillna(0)\n    env.predict(df_pred)","metadata":{"execution":{"iopub.status.busy":"2022-01-19T10:20:22.072701Z","iopub.execute_input":"2022-01-19T10:20:22.072953Z","iopub.status.idle":"2022-01-19T10:20:22.337467Z","shell.execute_reply.started":"2022-01-19T10:20:22.072926Z","shell.execute_reply":"2022-01-19T10:20:22.336622Z"},"trusted":true},"execution_count":null,"outputs":[]}]}