{"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":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport time\nfrom datetime import datetime\nfrom sklearn.preprocessing import MinMaxScaler\nfrom keras.preprocessing.sequence import TimeseriesGenerator\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.models import Sequential\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard\nfrom keras.layers import SimpleRNN, LSTM, Dense, Dropout, Masking, Embedding, Dense, Activation, Flatten, LSTM, TimeDistributed, RepeatVector,Input","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":8.852088,"end_time":"2022-01-18T13:31:52.355646","exception":false,"start_time":"2022-01-18T13:31:43.503558","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:02:56.504455Z","iopub.execute_input":"2022-01-19T11:02:56.504828Z","iopub.status.idle":"2022-01-19T11:02:58.210187Z","shell.execute_reply.started":"2022-01-19T11:02:56.504760Z","shell.execute_reply":"2022-01-19T11:02:58.209429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/train.csv')\ndf_details = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/asset_details.csv')\ndf_extest = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/example_test.csv')\ndf_sup = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/supplemental_train.csv')\ndf_exsamp = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/example_sample_submission.csv')","metadata":{"papermill":{"duration":76.927381,"end_time":"2022-01-18T13:33:09.304487","exception":false,"start_time":"2022-01-18T13:31:52.377106","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:03:17.285737Z","iopub.execute_input":"2022-01-19T11:03:17.286151Z","iopub.status.idle":"2022-01-19T11:04:01.721487Z","shell.execute_reply.started":"2022-01-19T11:03:17.286122Z","shell.execute_reply":"2022-01-19T11:04:01.720313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"// TRAINING DATA //\")\nprint()\nprint(df_train.head(5))\nprint()\nprint(\"-----------\")\nprint(\"// SUPLEMENTAL TRAIN //\")\nprint()\nprint(df_sup.head(5))\nprint()\nprint(\"-----------\")\nprint(\"// ASSET DETAILS //\")\nprint()\nprint(df_details.head(5))\nprint()\nprint(\"-----------\")\nprint(\"// EXAMPLE TEST //\")\nprint()\nprint(df_extest.head(5))\nprint()\nprint(\"-----------\")\nprint(\"// EXAMPLE SAMPLE //\")\nprint()\nprint(df_exsamp.head(5))\nprint()\nprint(\"-----------\")","metadata":{"papermill":{"duration":0.076366,"end_time":"2022-01-18T13:33:09.400013","exception":false,"start_time":"2022-01-18T13:33:09.323647","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:09:02.757015Z","iopub.execute_input":"2022-01-19T11:09:02.757288Z","iopub.status.idle":"2022-01-19T11:09:02.797444Z","shell.execute_reply.started":"2022-01-19T11:09:02.757260Z","shell.execute_reply":"2022-01-19T11:09:02.796713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA & PREPROCESSING","metadata":{}},{"cell_type":"code","source":"df_train","metadata":{"papermill":{"duration":0.05408,"end_time":"2022-01-18T13:33:09.475067","exception":false,"start_time":"2022-01-18T13:33:09.420987","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:09:16.213737Z","iopub.execute_input":"2022-01-19T11:09:16.213989Z","iopub.status.idle":"2022-01-19T11:09:16.237605Z","shell.execute_reply.started":"2022-01-19T11:09:16.213961Z","shell.execute_reply":"2022-01-19T11:09:16.236939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-01-19T11:09:33.105242Z","iopub.execute_input":"2022-01-19T11:09:33.105824Z","iopub.status.idle":"2022-01-19T11:09:33.411173Z","shell.execute_reply.started":"2022-01-19T11:09:33.105796Z","shell.execute_reply":"2022-01-19T11:09:33.410307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bnc = df_train[df_train['Asset_ID']==0]\nbtc = df_train[df_train['Asset_ID']==1]\nbcs = df_train[df_train['Asset_ID']==2]\ncrd = df_train[df_train['Asset_ID']==3]\ndog = df_train[df_train['Asset_ID']==4]\neos = df_train[df_train['Asset_ID']==5]\neth = df_train[df_train['Asset_ID']==6]\netc = df_train[df_train['Asset_ID']==7]\niot = df_train[df_train['Asset_ID']==8]\nltc = df_train[df_train['Asset_ID']==9]\nmkr = df_train[df_train['Asset_ID']==10]\nmnr = df_train[df_train['Asset_ID']==11]\nstl = df_train[df_train['Asset_ID']==12]\ntrn = df_train[df_train['Asset_ID']==13]","metadata":{"papermill":{"duration":4.414514,"end_time":"2022-01-18T13:33:13.911036","exception":false,"start_time":"2022-01-18T13:33:09.496522","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:12:20.346690Z","iopub.execute_input":"2022-01-19T11:12:20.346948Z","iopub.status.idle":"2022-01-19T11:12:22.528129Z","shell.execute_reply.started":"2022-01-19T11:12:20.346921Z","shell.execute_reply":"2022-01-19T11:12:22.527170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bnc = bnc.reindex(range(bnc.index[0],bnc.index[-1]+60,60),method='pad')\nbtc = btc.reindex(range(btc.index[0],btc.index[-1]+60,60),method='pad')\nbcs = bcs.reindex(range(bcs.index[0],bcs.index[-1]+60,60),method='pad')\ncrd = crd.reindex(range(crd.index[0],crd.index[-1]+60,60),method='pad')\ndog = dog.reindex(range(dog.index[0],dog.index[-1]+60,60),method='pad')\neos = eos.reindex(range(eos.index[0],eos.index[-1]+60,60),method='pad')\neth = eth.reindex(range(eth.index[0],eth.index[-1]+60,60),method='pad')\netc = etc.reindex(range(etc.index[0],etc.index[-1]+60,60),method='pad')\niot = iot.reindex(range(iot.index[0],iot.index[-1]+60,60),method='pad')\nltc = ltc.reindex(range(ltc.index[0],ltc.index[-1]+60,60),method='pad')\nmkr = mkr.reindex(range(mkr.index[0],mkr.index[-1]+60,60),method='pad')\nmnr = mnr.reindex(range(mnr.index[0],mnr.index[-1]+60,60),method='pad')\nstl = stl.reindex(range(stl.index[0],stl.index[-1]+60,60),method='pad')\ntrn = trn.reindex(range(trn.index[0],trn.index[-1]+60,60),method='pad')\n","metadata":{"papermill":{"duration":3.995685,"end_time":"2022-01-18T13:33:17.925803","exception":false,"start_time":"2022-01-18T13:33:13.930118","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:12:49.719939Z","iopub.execute_input":"2022-01-19T11:12:49.720202Z","iopub.status.idle":"2022-01-19T11:12:52.539567Z","shell.execute_reply.started":"2022-01-19T11:12:49.720174Z","shell.execute_reply":"2022-01-19T11:12:52.538820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"papermill":{"duration":0.045858,"end_time":"2022-01-18T13:33:17.990679","exception":false,"start_time":"2022-01-18T13:33:17.944821","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:13:33.594829Z","iopub.execute_input":"2022-01-19T11:13:33.595145Z","iopub.status.idle":"2022-01-19T11:13:33.620063Z","shell.execute_reply.started":"2022-01-19T11:13:33.595102Z","shell.execute_reply":"2022-01-19T11:13:33.618721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Target'] = df_train.groupby(['Asset_ID'], sort=False)['Target'].apply(lambda x: x.fillna(x.mean()))","metadata":{"papermill":{"duration":14.134417,"end_time":"2022-01-18T13:33:32.145628","exception":false,"start_time":"2022-01-18T13:33:18.011211","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:13:53.023860Z","iopub.execute_input":"2022-01-19T11:13:53.024091Z","iopub.status.idle":"2022-01-19T11:14:04.466683Z","shell.execute_reply.started":"2022-01-19T11:13:53.024059Z","shell.execute_reply":"2022-01-19T11:14:04.466098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"papermill":{"duration":0.052911,"end_time":"2022-01-18T13:33:32.219617","exception":false,"start_time":"2022-01-18T13:33:32.166706","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:14:47.624033Z","iopub.execute_input":"2022-01-19T11:14:47.624271Z","iopub.status.idle":"2022-01-19T11:14:47.643635Z","shell.execute_reply.started":"2022-01-19T11:14:47.624247Z","shell.execute_reply":"2022-01-19T11:14:47.643076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = df_train[:100000]\ntrain","metadata":{"papermill":{"duration":0.049096,"end_time":"2022-01-18T13:33:32.289866","exception":false,"start_time":"2022-01-18T13:33:32.24077","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:15:28.059306Z","iopub.execute_input":"2022-01-19T11:15:28.059615Z","iopub.status.idle":"2022-01-19T11:15:28.077162Z","shell.execute_reply.started":"2022-01-19T11:15:28.059565Z","shell.execute_reply":"2022-01-19T11:15:28.076011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train.Target.copy()\ny_train.shape","metadata":{"papermill":{"duration":0.033396,"end_time":"2022-01-18T13:33:32.344819","exception":false,"start_time":"2022-01-18T13:33:32.311423","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:24:12.953734Z","iopub.execute_input":"2022-01-19T11:24:12.954772Z","iopub.status.idle":"2022-01-19T11:24:12.961472Z","shell.execute_reply.started":"2022-01-19T11:24:12.954736Z","shell.execute_reply":"2022-01-19T11:24:12.960567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CREATE THE MODEL","metadata":{}},{"cell_type":"code","source":"x_train = train.drop(['Target','Asset_ID','Open','High','Low'],axis = 1)\nprint(x_train.shape)\nprint(x_train.head(5))","metadata":{"papermill":{"duration":0.038959,"end_time":"2022-01-18T13:33:32.405135","exception":false,"start_time":"2022-01-18T13:33:32.366176","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:24:16.117942Z","iopub.execute_input":"2022-01-19T11:24:16.119095Z","iopub.status.idle":"2022-01-19T11:24:16.129793Z","shell.execute_reply.started":"2022-01-19T11:24:16.119063Z","shell.execute_reply":"2022-01-19T11:24:16.128911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.to_numpy().reshape(-1,5,1)\nx_train.shape","metadata":{"papermill":{"duration":0.034833,"end_time":"2022-01-18T13:33:32.460752","exception":false,"start_time":"2022-01-18T13:33:32.425919","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:39:55.768677Z","iopub.execute_input":"2022-01-19T11:39:55.768909Z","iopub.status.idle":"2022-01-19T11:39:55.775768Z","shell.execute_reply.started":"2022-01-19T11:39:55.768885Z","shell.execute_reply":"2022-01-19T11:39:55.775250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_x, val_x, train_y, val_y = train_test_split(x_train, y_train, test_size = 0.25, random_state=2)","metadata":{"papermill":{"duration":0.118274,"end_time":"2022-01-18T13:33:32.601668","exception":false,"start_time":"2022-01-18T13:33:32.483394","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:41:41.342421Z","iopub.execute_input":"2022-01-19T11:41:41.342840Z","iopub.status.idle":"2022-01-19T11:41:41.424481Z","shell.execute_reply.started":"2022-01-19T11:41:41.342798Z","shell.execute_reply":"2022-01-19T11:41:41.423698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Dense, LSTM, Dropout, GRU\nfrom keras.models import Sequential\nfrom tensorflow.keras.optimizers import SGD\n# The GRU architecture\nmodel = Sequential()\n# First GRU layer with Dropout regularisation\nmodel.add(GRU(units=50, return_sequences=True, input_shape=(train_x.shape[1],1), activation='tanh'))\nmodel.add(Dropout(0.2))\n# Second GRU layer\nmodel.add(GRU(units=50, return_sequences=True, input_shape=(train_x.shape[1],1), activation='tanh'))\nmodel.add(Dropout(0.2))\n# Third GRU layer\nmodel.add(GRU(units=50, return_sequences=True, input_shape=(train_x.shape[1],1), activation='tanh'))\nmodel.add(Dropout(0.2))\n# Fourth GRU layer\nmodel.add(GRU(units=50, activation='tanh'))\nmodel.add(Dropout(0.2))\n# The output layer\nmodel.add(Dense(units=1))","metadata":{"papermill":{"duration":1.122488,"end_time":"2022-01-18T13:33:33.747217","exception":false,"start_time":"2022-01-18T13:33:32.624729","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:41:44.429954Z","iopub.execute_input":"2022-01-19T11:41:44.430216Z","iopub.status.idle":"2022-01-19T11:41:45.467559Z","shell.execute_reply.started":"2022-01-19T11:41:44.430183Z","shell.execute_reply":"2022-01-19T11:41:45.466559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.metrics import accuracy\noptimizer = tf.keras.optimizers.Adam(learning_rate=0.001)\nmodel.compile(optimizer = optimizer, loss='mse', metrics = ['mae'])\n\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor=\"val_loss\",\n                          mode=\"min\",\n                          patience=5)\nmodel.summary()","metadata":{"papermill":{"duration":0.05289,"end_time":"2022-01-18T13:33:33.824553","exception":false,"start_time":"2022-01-18T13:33:33.771663","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:41:57.637896Z","iopub.execute_input":"2022-01-19T11:41:57.638156Z","iopub.status.idle":"2022-01-19T11:41:57.661472Z","shell.execute_reply.started":"2022-01-19T11:41:57.638127Z","shell.execute_reply":"2022-01-19T11:41:57.660473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x_train,y_train, batch_size=64,epochs=5,validation_data=(val_x,val_y), callbacks=[early_stopping], shuffle=True)","metadata":{"papermill":{"duration":250.5964,"end_time":"2022-01-18T13:37:44.445447","exception":false,"start_time":"2022-01-18T13:33:33.849047","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:42:10.375945Z","iopub.execute_input":"2022-01-19T11:42:10.376199Z","iopub.status.idle":"2022-01-19T11:46:05.888525Z","shell.execute_reply.started":"2022-01-19T11:42:10.376171Z","shell.execute_reply":"2022-01-19T11:46:05.888023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Diffining Figure\nimport matplotlib.pyplot as plt\nfigure = plt.figure(figsize=(20,7))\n\n#Adding Subplot 1 (For Accuracy)\nfigure.add_subplot(121)\n\nplt.plot(history.epoch,history.history['mae'],label = \"mae\") # Accuracy curve for training set\nplt.plot(history.epoch,history.history['val_mae'],label = \"val_mae\") # Accuracy curve for validation set\n\nplt.title(\"MAE Curve\",fontsize=18)\nplt.xlabel(\"Epochs\",fontsize=15)\nplt.ylabel(\"MAE\",fontsize=15)\nplt.grid(alpha=0.3)\nplt.legend()\n\n#Adding Subplot 1 (For Loss)\nfigure.add_subplot(122)\n\nplt.plot(history.epoch,history.history['loss'],label=\"loss\") # Loss curve for training set\nplt.plot(history.epoch,history.history['val_loss'],label=\"val_loss\") # Loss curve for validation set\n\nplt.title(\"Loss Curve\",fontsize=18)\nplt.xlabel(\"Epochs\",fontsize=15)\nplt.ylabel(\"Loss\",fontsize=15)\nplt.grid(alpha=0.3)\nplt.legend()\n\nplt.show()","metadata":{"papermill":{"duration":1.780118,"end_time":"2022-01-18T13:37:47.482968","exception":false,"start_time":"2022-01-18T13:37:45.70285","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:46:43.498701Z","iopub.execute_input":"2022-01-19T11:46:43.499031Z","iopub.status.idle":"2022-01-19T11:46:43.945529Z","shell.execute_reply.started":"2022-01-19T11:46:43.499007Z","shell.execute_reply":"2022-01-19T11:46:43.944283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mantap = model.predict(val_x)\nval_y, mantap","metadata":{"execution":{"iopub.status.busy":"2022-01-19T11:59:01.278286Z","iopub.execute_input":"2022-01-19T11:59:01.278554Z","iopub.status.idle":"2022-01-19T11:59:09.163273Z","shell.execute_reply.started":"2022-01-19T11:59:01.278506Z","shell.execute_reply":"2022-01-19T11:59:09.162518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gresearch_crypto\nenv = gresearch_crypto.make_env()   # initialize the environment\niter_test = env.iter_test()    # an iterator which loops over the test set and sample submission","metadata":{"papermill":{"duration":1.333238,"end_time":"2022-01-18T13:37:50.137525","exception":false,"start_time":"2022-01-18T13:37:48.804287","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:59:32.117858Z","iopub.execute_input":"2022-01-19T11:59:32.118275Z","iopub.status.idle":"2022-01-19T11:59:32.174661Z","shell.execute_reply.started":"2022-01-19T11:59:32.118247Z","shell.execute_reply":"2022-01-19T11:59:32.174064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 0\nfor (test_df, sample_prediction_df) in iter_test:\n#     x_train = train.drop(['Target','Asset_ID','Open','High','Low'],axis = 1)\n    test_df = test_df.drop(['Asset_ID','Open','High','Low','row_id'],axis = 1)\n    test_df = test_df.to_numpy().reshape(-1,5,1)\n    test_y = model.predict(test_df)\n    sample_prediction_df['Target'] = 0\n    sample_prediction_df['Target'] = test_y\n#     print(test_df.shape)\n#     print(sample_prediction_df.shape)\n#     print(sample_prediction_df)\n    env.predict(sample_prediction_df)\n\n    index += 1","metadata":{"papermill":{"duration":3.268563,"end_time":"2022-01-18T13:37:54.633657","exception":false,"start_time":"2022-01-18T13:37:51.365094","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-01-19T11:59:35.852059Z","iopub.execute_input":"2022-01-19T11:59:35.852411Z","iopub.status.idle":"2022-01-19T11:59:36.141023Z","shell.execute_reply.started":"2022-01-19T11:59:35.852374Z","shell.execute_reply":"2022-01-19T11:59:36.139869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}