{"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":"# 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\n\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-23T11:35:40.835095Z","iopub.execute_input":"2022-01-23T11:35:40.835590Z","iopub.status.idle":"2022-01-23T11:35:40.844987Z","shell.execute_reply.started":"2022-01-23T11:35:40.835522Z","shell.execute_reply":"2022-01-23T11:35:40.844110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_features_for_test(data):\n    # FE for data as row of DataFrame\n    \n    # Two new features from the competition tutorial\n    #df_feat = data[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP', 'timestamp']].copy()\n    df_feat = data[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']].copy()\n    df_feat['Upper_Shadow'] = df_feat['High'] - np.maximum(df_feat['Close'], df_feat['Open'])\n    df_feat['Lower_Shadow'] = np.minimum(df_feat['Close'], df_feat['Open']) - df_feat['Low']\n    \n    # Thanks to https://www.kaggle.com/code1110/gresearch-simple-lgb-starter\n    df_feat['lower_shadow'] = np.minimum(df_feat['Close'], df_feat['Open']) - df_feat['Low']\n    df_feat['high2low'] = df_feat['High'] / df_feat['Low']\n    df_feat['volume2count'] = df_feat['Volume'] / (df_feat['Count'] + 1)\n    \n    \n    return df_feat.values","metadata":{"execution":{"iopub.status.busy":"2022-01-23T11:35:40.849596Z","iopub.execute_input":"2022-01-23T11:35:40.849899Z","iopub.status.idle":"2022-01-23T11:35:40.863816Z","shell.execute_reply.started":"2022-01-23T11:35:40.849850Z","shell.execute_reply":"2022-01-23T11:35:40.862796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gresearch_crypto","metadata":{"execution":{"iopub.status.busy":"2022-01-23T11:35:40.865095Z","iopub.execute_input":"2022-01-23T11:35:40.865545Z","iopub.status.idle":"2022-01-23T11:35:40.880783Z","shell.execute_reply.started":"2022-01-23T11:35:40.865514Z","shell.execute_reply":"2022-01-23T11:35:40.880158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prediction and submission\nenv = gresearch_crypto.make_env()\niter_test = env.iter_test()\n\nfrom keras.models import load_model\nimport time\nmodels = {}\nfor i in range(14):\n    model_name = f'../input/gresearchcryptomodelv2/model-{i}.hdf5'\n    #print(model_name)\n    models[i] = load_model(model_name)\n\ntime_taken = []\nn_steps = 12\nfor i, (df_test, df_pred) in enumerate(iter_test):\n    for j, row in df_test.iterrows():\n        \n        try:\n            #init_time = time.time()\n            model = models[row['Asset_ID']]\n            x_test = get_features_for_test(row)\n            x_test = x_test.reshape((1, n_steps))\n            y_pred = model.predict([x_test])[0]\n\n            df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = y_pred\n            #time_taken.append(time.time() - init_time)\n        \n        except:\n            print(f'{i}-th iteration of the test dataset, {j}-th row - there was the exception, then set Target = 0')\n            df_pred.loc[df_pred['row_id'] == row['row_id'], 'Target'] = 0\n            \n#         # Print just one sample row to get a feeling of what it looks like        \n#         if i == 0 and j == 0:\n#             print('Example of the x_test data')\n#             display(x_test)\n\n#     # Display the first prediction dataframe\n#     if i == 0:\n#         print('Example of the prediction for test data')\n#         display(df_pred)\n    df_pred['Target'] = df_pred['Target'].fillna(0)\n\n    # Send submissions\n    env.predict(df_pred)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T11:35:51.177910Z","iopub.execute_input":"2022-01-23T11:35:51.179433Z","iopub.status.idle":"2022-01-23T11:35:57.777188Z","shell.execute_reply.started":"2022-01-23T11:35:51.179377Z","shell.execute_reply":"2022-01-23T11:35:57.776162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -ltr","metadata":{"execution":{"iopub.status.busy":"2022-01-23T11:36:08.278092Z","iopub.execute_input":"2022-01-23T11:36:08.278381Z","iopub.status.idle":"2022-01-23T11:36:09.086780Z","shell.execute_reply.started":"2022-01-23T11:36:08.278338Z","shell.execute_reply":"2022-01-23T11:36:09.085715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}