{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import LSTM,Dense,Dropout\nfrom keras.models import load_model, Model\nfrom sklearn.model_selection import train_test_split\n\nimport pandas as pd\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/predict-volcanic-eruptions-ingv-oe/train/416906269.csv\")\ndf.fillna(0, inplace=True)\nprint(df)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/predict-volcanic-eruptions-ingv-oe/train.csv\")\ndf.fillna(0, inplace=True)\nprint(df)\n# train_x = \nlist1 = []\nfor i, j in zip(df[\"segment_id\"].unique().tolist(), df[\"time_to_eruption\"].unique().tolist()):\n    if j!=0:\n        dftemp = pd.read_csv(\"/kaggle/input/predict-volcanic-eruptions-ingv-oe/train/%s.csv\"%i)\n        dftemp.fillna(0, inplace=True)\n        total_data = np.concatenate((dftemp.abs().mean().to_numpy(),\n                                     dftemp.std().to_numpy(),\n                                     dftemp.mean().to_numpy(),\n                                     dftemp.var().to_numpy(),\n                                     dftemp.min().to_numpy(),\n                                     dftemp.max().to_numpy(),\n                                     dftemp.median().to_numpy(),\n                                     dftemp.quantile([0.1,0.25,0.5,0.75,0.9]).to_numpy().reshape(1,-1)[0])).tolist()\n        print(total_data)\n\n        print(i)\n        list1.append(total_data)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import Input, Dense\nfrom keras.models import Model\nfrom keras.layers.core import Flatten, Dense, Dropout, Activation\nfrom keras.layers.normalization import  BatchNormalization  as bn\nfrom  keras.layers.pooling import MaxPooling1D as pool\n\nimport  keras.utils \n\nfrom sklearn.model_selection import train_test_split\n\n# print(list1)\n# print(df[\"time_to_eruption\"].tolist())\ntrain_x, test_x, train_y, test_y = train_test_split(list1,\n                                    df[\"time_to_eruption\"].tolist(),\n                                    test_size=0.1,\n                                    shuffle=True)\n# This returns a tensor\ninputs = Input(shape=(np.array(train_x).shape[1],))\n# x = bn()(x)\n# del x\n# a layer instance is callable on a tensor, and returns a tensor\nx = Dense(1000, activation='relu')(inputs)\n# x = bn()(x)\n# x = Activation('relu')(x)\n\n# x = Activation('sigmoid')(x)\n# x = Dense(1000)(x)\n\n\n# x = Dense(128)(x)\n# x = bn()(x)\nx = Dropout(0.7)(x)\n# x = Dense(64)(x)\n# x = bn()(x)\n# x = Activation('relu')(x)\n# x = Dense(32)(x)\n# x = bn()(x)\n# x = Activation('linear')(x)\npredictions = Dense(1, activation='relu')(x)\n# predictions = Dense(10, activation='softmax')(x)\n\n# This creates a model that includes\n# the Input layer and three Dense layers\nmodel = Model(inputs=inputs, outputs=predictions)\nmodel.compile(optimizer='adam',\n              loss='mean_absolute_error',\n             metrics=['mae'])\n# model.compile(optimizer='rmsprop',\n#               loss='sparse_categorical_crossentropy',\n#              metrics=['acc'])\nmodel.fit(train_x,train_y, validation_data=(test_x, test_y)\n          ,batch_size=8,epochs=600)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nsample_submission_df=pd.read_csv('../input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv')\nn_f=12\ntotal_data_test_=np.empty((sample_submission_df.shape[0],n_f*10))\nfor i_,seg_ in enumerate(sample_submission_df['segment_id']):\n    the_df=pd.read_csv(f'/kaggle/input/predict-volcanic-eruptions-ingv-oe/test/{seg_}.csv').fillna(0)\n    total_data_test_[i_,:]=np.concatenate((the_df.abs().mean().to_numpy(),\n                                    the_df.std().to_numpy(),\n                                    the_df.mean().to_numpy(),\n                                    the_df.var().to_numpy(),\n                                    the_df.min().to_numpy(),\n                                    the_df.max().to_numpy(),\n                                    the_df.median().to_numpy(),\n                                    the_df.quantile([0.1,0.25,0.5,0.75,0.9]).to_numpy().reshape(1,-1)[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission_df1=pd.read_csv('../input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv')\nsample_submission_df['time_to_eruption']=model.predict(total_data_test_)\nsample_submission_df.to_csv('/kaggle/working/submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_df=pd.read_csv(f'/kaggle/input/predict-volcanic-eruptions-ingv-oe/test/{1001028887}.csv').fillna(0)\nprint(the_df)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}