{"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\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\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-29T14:36:31.051450Z","iopub.execute_input":"2021-10-29T14:36:31.051711Z","iopub.status.idle":"2021-10-29T14:36:31.055494Z","shell.execute_reply.started":"2021-10-29T14:36:31.051683Z","shell.execute_reply":"2021-10-29T14:36:31.054883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data  = pd.read_csv(f'{data_dir}/test/1067970.csv')\ndef show_sensors(df1,df2):\n    f, axes = plt.subplots(10, 2)\n    f.set_size_inches((16, 10)) \n    f.tight_layout()\n    plt.subplots_adjust(bottom=-0.4)\n    for i in range(1,11):\n        axes[i-1][0].plot(df1[i-1].values,c = 'r')\n        axes[i-1][0].set_title('Sensor_'+str(i))\n        axes[i-1][0].set_xlabel('time')\n    for i in range(1,11):\n        axes[i-1][1].plot(df2[i-1].values,c = 'y')\n        axes[i-1][1].set_title('Sensor_'+str(i))\n        axes[i-1][1].set_xlabel('time')","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:37:09.470293Z","iopub.execute_input":"2021-10-29T14:37:09.470550Z","iopub.status.idle":"2021-10-29T14:37:09.605561Z","shell.execute_reply.started":"2021-10-29T14:37:09.470521Z","shell.execute_reply":"2021-10-29T14:37:09.604863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import GRU,RepeatVector,Dense,LSTM,TimeDistributed\nfrom sklearn.preprocessing import StandardScaler;\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import optimizers\ndata_dir = '../input/predict-volcanic-eruptions-ingv-oe' ","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:37:06.544461Z","iopub.execute_input":"2021-10-29T14:37:06.544740Z","iopub.status.idle":"2021-10-29T14:37:06.848242Z","shell.execute_reply.started":"2021-10-29T14:37:06.544709Z","shell.execute_reply":"2021-10-29T14:37:06.847517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xmodel = Sequential()\nXmodel.add(LSTM(300,input_shape=(501,10)))\nXmodel.add(Dense(1))\noptimizers.SGD(learning_rate=0.000000001)\nXmodel.compile(optimizer='sgd',loss='mean_squared_error')\nXmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:37:14.661841Z","iopub.execute_input":"2021-10-29T14:37:14.662367Z","iopub.status.idle":"2021-10-29T14:37:17.512984Z","shell.execute_reply.started":"2021-10-29T14:37:14.662331Z","shell.execute_reply":"2021-10-29T14:37:17.512295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"re Y","metadata":{}},{"cell_type":"code","source":"y_train_mix = pd.read_csv(f'{data_dir}/train.csv');\ny_rescaler = StandardScaler();\ny_rescaler.fit((y_train_mix['time_to_eruption'].values).reshape(y_train_mix.shape[0],1));","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:37:20.372131Z","iopub.execute_input":"2021-10-29T14:37:20.372651Z","iopub.status.idle":"2021-10-29T14:37:20.393293Z","shell.execute_reply.started":"2021-10-29T14:37:20.372600Z","shell.execute_reply":"2021-10-29T14:37:20.392683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"re X","metadata":{}},{"cell_type":"code","source":"rex = pd.read_csv(f'{data_dir}/train/603314.csv');\nx_rescaler = StandardScaler();\nx_rescaler.fit(rex.values);","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:37:22.594305Z","iopub.execute_input":"2021-10-29T14:37:22.594685Z","iopub.status.idle":"2021-10-29T14:37:22.773435Z","shell.execute_reply.started":"2021-10-29T14:37:22.594620Z","shell.execute_reply":"2021-10-29T14:37:22.772501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"read csv","metadata":{}},{"cell_type":"code","source":"x = pd.read_csv('../input/dataml/X_sp500.csv');\nx = x_rescaler.transform(x)\nx = x.reshape(4431, 501, 10)\n\ny = pd.read_csv('../input/dataml/Y_sp500.csv');\ny = y_rescaler.transform(y).reshape(4431,1)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:37:25.059540Z","iopub.execute_input":"2021-10-29T14:37:25.059936Z","iopub.status.idle":"2021-10-29T14:37:29.047097Z","shell.execute_reply.started":"2021-10-29T14:37:25.059901Z","shell.execute_reply":"2021-10-29T14:37:29.046386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x[:3500]\nx_test = x[3500:]\ny_train = y[:3500]\ny_test = y[3500:]\nprint(x_train.shape,x_test.shape)\nprint(y_train.shape,y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:37:32.916733Z","iopub.execute_input":"2021-10-29T14:37:32.917379Z","iopub.status.idle":"2021-10-29T14:37:32.924451Z","shell.execute_reply.started":"2021-10-29T14:37:32.917344Z","shell.execute_reply":"2021-10-29T14:37:32.923527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape,y.shape)\nhist = Xmodel.fit(x_train, y_train, batch_size=50, epochs=500)\nplt.plot(hist.history['loss'])\n\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train_loss, val_loss'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-29T15:07:26.961190Z","iopub.execute_input":"2021-10-29T15:07:26.961457Z","iopub.status.idle":"2021-10-29T15:36:23.508041Z","shell.execute_reply.started":"2021-10-29T15:07:26.961425Z","shell.execute_reply":"2021-10-29T15:36:23.507263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(y_rescaler.inverse_transform(model.predict(x_test)))\n# print(y_rescaler.inverse_transform(y_test))\n# mean_squared_error(model.predict(x_test),y_test)\n# print(x_test.shape)\n# i = 50\n# xp = (x_rescaler.inverse_transform(x_test[i]))\n# xt = x_test[i].reshape(1,501,10)\n# show_sensors(pd.DataFrame(xp),pd.DataFrame(x_test[i]))\n# print(y_rescaler.inverse_transform(model.predict(xt)),y_rescaler.inverse_transform(y_test[i]))","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:18:15.285716Z","iopub.status.idle":"2021-10-29T14:18:15.286046Z","shell.execute_reply.started":"2021-10-29T14:18:15.285875Z","shell.execute_reply":"2021-10-29T14:18:15.285891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xmodel.save('volcano_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-10-29T15:38:06.624773Z","iopub.execute_input":"2021-10-29T15:38:06.625395Z","iopub.status.idle":"2021-10-29T15:38:06.653093Z","shell.execute_reply.started":"2021-10-29T15:38:06.625356Z","shell.execute_reply":"2021-10-29T15:38:06.652325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nmodel = keras.models.load_model('./model_vocanic')","metadata":{"execution":{"iopub.status.busy":"2021-10-29T14:22:11.587125Z","iopub.execute_input":"2021-10-29T14:22:11.587407Z","iopub.status.idle":"2021-10-29T14:22:11.594755Z","shell.execute_reply.started":"2021-10-29T14:22:11.587378Z","shell.execute_reply":"2021-10-29T14:22:11.594185Z"},"trusted":true},"execution_count":null,"outputs":[]}]}