{"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\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nplt.style.use('seaborn-white')\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nimport lightgbm as lgbm\nfrom lightgbm import LGBMRegressor\nfrom xgboost import XGBRegressor\nfrom math import isnan\n\nfile_list = []\nfile_list_train = []\nfile_list_test = []\n\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        file_list.append(os.path.join(dirname, filename))\n        \nPATH = '/kaggle/input/predict-volcanic-eruptions-ingv-oe/'\n\nfor dirname, _, filenames in os.walk('/kaggle/input/predict-volcanic-eruptions-ingv-oe/train'):\n    for filename in filenames:\n        file_list_train.append(os.path.join(dirname, filename))\n        \nfor dirname, _, filenames in os.walk('/kaggle/input/predict-volcanic-eruptions-ingv-oe/test'):\n    for filename in filenames:\n        file_list_test.append(os.path.join(dirname, filename))\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","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":10.478656,"end_time":"2021-12-17T18:49:15.206875","exception":false,"start_time":"2021-12-17T18:49:04.728219","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:07.573711Z","iopub.execute_input":"2021-12-21T13:50:07.574801Z","iopub.status.idle":"2021-12-21T13:50:18.056766Z","shell.execute_reply.started":"2021-12-21T13:50:07.574675Z","shell.execute_reply":"2021-12-21T13:50:18.055745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(file_list[2])\n\n\nprint(pd.read_csv(file_list[0]))\nprint(pd.read_csv(file_list[2]).isna().sum())","metadata":{"papermill":{"duration":0.187253,"end_time":"2021-12-17T18:49:15.425775","exception":false,"start_time":"2021-12-17T18:49:15.238522","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.058575Z","iopub.execute_input":"2021-12-21T13:50:18.058835Z","iopub.status.idle":"2021-12-21T13:50:18.253393Z","shell.execute_reply.started":"2021-12-21T13:50:18.058802Z","shell.execute_reply":"2021-12-21T13:50:18.252446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(file_list_train[0])\nprint(pd.read_csv(file_list_train[0]))","metadata":{"papermill":{"duration":0.16454,"end_time":"2021-12-17T18:49:15.621854","exception":false,"start_time":"2021-12-17T18:49:15.457314","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.254575Z","iopub.execute_input":"2021-12-21T13:50:18.254804Z","iopub.status.idle":"2021-12-21T13:50:18.386410Z","shell.execute_reply.started":"2021-12-21T13:50:18.254775Z","shell.execute_reply":"2021-12-21T13:50:18.385446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(file_list_train))","metadata":{"papermill":{"duration":0.040538,"end_time":"2021-12-17T18:49:15.692765","exception":false,"start_time":"2021-12-17T18:49:15.652227","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.388586Z","iopub.execute_input":"2021-12-21T13:50:18.388817Z","iopub.status.idle":"2021-12-21T13:50:18.395430Z","shell.execute_reply.started":"2021-12-21T13:50:18.388789Z","shell.execute_reply":"2021-12-21T13:50:18.394425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(file_list_test))","metadata":{"papermill":{"duration":0.040528,"end_time":"2021-12-17T18:49:15.764628","exception":false,"start_time":"2021-12-17T18:49:15.724100","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.396976Z","iopub.execute_input":"2021-12-21T13:50:18.397230Z","iopub.status.idle":"2021-12-21T13:50:18.409061Z","shell.execute_reply.started":"2021-12-21T13:50:18.397193Z","shell.execute_reply":"2021-12-21T13:50:18.408159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files_test = [file.split('/')[-1].split('.')[-2] for file in file_list_test]\nfiles_train = [file.split('/')[-1].split('.')[-2] for file in file_list_train]","metadata":{"papermill":{"duration":0.049179,"end_time":"2021-12-17T18:49:15.848004","exception":false,"start_time":"2021-12-17T18:49:15.798825","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.410086Z","iopub.execute_input":"2021-12-21T13:50:18.410868Z","iopub.status.idle":"2021-12-21T13:50:18.429295Z","shell.execute_reply.started":"2021-12-21T13:50:18.410822Z","shell.execute_reply":"2021-12-21T13:50:18.428183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(files_train[0:10])\nprint(files_test[0:10])","metadata":{"papermill":{"duration":0.041363,"end_time":"2021-12-17T18:49:15.922772","exception":false,"start_time":"2021-12-17T18:49:15.881409","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.430806Z","iopub.execute_input":"2021-12-21T13:50:18.431850Z","iopub.status.idle":"2021-12-21T13:50:18.445451Z","shell.execute_reply.started":"2021-12-21T13:50:18.431799Z","shell.execute_reply":"2021-12-21T13:50:18.444567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = set(files_test)\ntrain_set = set(files_train)\ninter = test_set.intersection(train_set)\n\nprint(inter)","metadata":{"papermill":{"duration":0.042373,"end_time":"2021-12-17T18:49:15.998186","exception":false,"start_time":"2021-12-17T18:49:15.955813","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.447006Z","iopub.execute_input":"2021-12-21T13:50:18.447305Z","iopub.status.idle":"2021-12-21T13:50:18.461129Z","shell.execute_reply.started":"2021-12-21T13:50:18.447273Z","shell.execute_reply":"2021-12-21T13:50:18.459978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(PATH+'train.csv')","metadata":{"papermill":{"duration":0.050114,"end_time":"2021-12-17T18:49:16.080662","exception":false,"start_time":"2021-12-17T18:49:16.030548","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.462405Z","iopub.execute_input":"2021-12-21T13:50:18.462659Z","iopub.status.idle":"2021-12-21T13:50:18.494432Z","shell.execute_reply.started":"2021-12-21T13:50:18.462629Z","shell.execute_reply":"2021-12-21T13:50:18.493552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.distplot(train['time_to_eruption'],\n            hist=True,\n            kde=True,\n            bins=100,\n            color='blue',\n            hist_kws={'edgecolor':'black'})","metadata":{"papermill":{"duration":0.716972,"end_time":"2021-12-17T18:49:16.830376","exception":false,"start_time":"2021-12-17T18:49:16.113404","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:18.497447Z","iopub.execute_input":"2021-12-21T13:50:18.497932Z","iopub.status.idle":"2021-12-21T13:50:19.203440Z","shell.execute_reply.started":"2021-12-21T13:50:18.497834Z","shell.execute_reply":"2021-12-21T13:50:19.202541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['time_to_eruption'].describe()","metadata":{"papermill":{"duration":0.050499,"end_time":"2021-12-17T18:49:16.915884","exception":false,"start_time":"2021-12-17T18:49:16.865385","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:19.205027Z","iopub.execute_input":"2021-12-21T13:50:19.205587Z","iopub.status.idle":"2021-12-21T13:50:19.218402Z","shell.execute_reply.started":"2021-12-21T13:50:19.205542Z","shell.execute_reply":"2021-12-21T13:50:19.216918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_segment_id = pd.read_csv(PATH+'test/473253715.csv')\n\ndf_segment_id.plot(figsize=(20,20),\n                  subplots=True,\n                  layout=(10,1),\n                  rot=0,\n                  lw=1,\n                  title='sergemnt id #473253715')\nplt.show()","metadata":{"papermill":{"duration":2.895509,"end_time":"2021-12-17T18:49:19.846176","exception":false,"start_time":"2021-12-17T18:49:16.950667","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:19.219884Z","iopub.execute_input":"2021-12-21T13:50:19.220141Z","iopub.status.idle":"2021-12-21T13:50:22.123662Z","shell.execute_reply.started":"2021-12-21T13:50:19.220111Z","shell.execute_reply":"2021-12-21T13:50:22.122505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_segment_id.columns","metadata":{"papermill":{"duration":0.052202,"end_time":"2021-12-17T18:49:19.942796","exception":false,"start_time":"2021-12-17T18:49:19.890594","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:22.125288Z","iopub.execute_input":"2021-12-21T13:50:22.126143Z","iopub.status.idle":"2021-12-21T13:50:22.131944Z","shell.execute_reply.started":"2021-12-21T13:50:22.126103Z","shell.execute_reply":"2021-12-21T13:50:22.131328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nstats = dict()\n\nstats[\"sensor_1\"] =  0\nstats[\"sensor_2\"] =  0\nstats[\"sensor_3\"] =  0\nstats[\"sensor_4\"] =  0\nstats[\"sensor_5\"] =  0\nstats[\"sensor_6\"] =  0\nstats[\"sensor_7\"] =  0\nstats[\"sensor_8\"] =  0\nstats[\"sensor_9\"] =  0\nstats[\"sensor_10\"] =  0\n\n\nfor i in file_list_test:\n    df_test_stat = pd.read_csv(i)\n    if df_test_stat[\"sensor_1\"].max() > 0:\n        stats[\"sensor_1\"] += 1\n    if df_test_stat[\"sensor_2\"].max() > 0:\n        stats[\"sensor_2\"] += 1\n    if df_test_stat[\"sensor_3\"].max() > 0:\n        stats[\"sensor_3\"] += 1\n    if df_test_stat[\"sensor_4\"].max() > 0:\n        stats[\"sensor_4\"] += 1\n    if df_test_stat[\"sensor_5\"].max() > 0:\n        stats[\"sensor_5\"] += 1\n    if df_test_stat[\"sensor_6\"].max() > 0:\n        stats[\"sensor_6\"] += 1\n    if df_test_stat[\"sensor_7\"].max() > 0:\n        stats[\"sensor_7\"] += 1\n    if df_test_stat[\"sensor_8\"].max() > 0:\n        stats[\"sensor_8\"] += 1\n    if df_test_stat[\"sensor_9\"].max() > 0:\n        stats[\"sensor_9\"] += 1\n    if df_test_stat[\"sensor_10\"].max() > 0:\n        stats[\"sensor_10\"] += 1\n\n        \nprint(stats)  \n\n","metadata":{"papermill":{"duration":581.978113,"end_time":"2021-12-17T18:59:01.964595","exception":false,"start_time":"2021-12-17T18:49:19.986482","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T13:50:22.132938Z","iopub.execute_input":"2021-12-21T13:50:22.133531Z","iopub.status.idle":"2021-12-21T14:00:04.899732Z","shell.execute_reply.started":"2021-12-21T13:50:22.133494Z","shell.execute_reply":"2021-12-21T14:00:04.897763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.bar(stats.keys(), stats.values())","metadata":{"papermill":{"duration":0.288371,"end_time":"2021-12-17T18:59:02.298221","exception":false,"start_time":"2021-12-17T18:59:02.009850","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:00:04.903178Z","iopub.execute_input":"2021-12-21T14:00:04.903822Z","iopub.status.idle":"2021-12-21T14:00:05.158628Z","shell.execute_reply.started":"2021-12-21T14:00:04.903773Z","shell.execute_reply":"2021-12-21T14:00:05.157693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train.sort_values('time_to_eruption', axis=0, ascending=True).iloc[[0,-1],:])\n\nsegment_id_min = 601524801\nsegment_id_max = 1923243961\n\ndf_segment_id_min = pd.read_csv(PATH+'train/'+str(segment_id_min)+'.csv')\n\ndf_segment_id_max = pd.read_csv(PATH+'train/'+str(segment_id_max)+'.csv')","metadata":{"papermill":{"duration":0.281983,"end_time":"2021-12-17T18:59:02.625150","exception":false,"start_time":"2021-12-17T18:59:02.343167","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:00:05.159851Z","iopub.execute_input":"2021-12-21T14:00:05.160073Z","iopub.status.idle":"2021-12-21T14:00:05.395514Z","shell.execute_reply.started":"2021-12-21T14:00:05.160046Z","shell.execute_reply":"2021-12-21T14:00:05.394602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_segment_id_min.plot(figsize=(20,20),\n                  subplots=True,\n                  layout=(10,1),\n                  rot=0,\n                  lw=1,\n                  title='segment id min')\nplt.show()","metadata":{"papermill":{"duration":2.628195,"end_time":"2021-12-17T18:59:05.298651","exception":false,"start_time":"2021-12-17T18:59:02.670456","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:00:05.398557Z","iopub.execute_input":"2021-12-21T14:00:05.398925Z","iopub.status.idle":"2021-12-21T14:00:08.011653Z","shell.execute_reply.started":"2021-12-21T14:00:05.398878Z","shell.execute_reply":"2021-12-21T14:00:08.010553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_segment_id_max.plot(figsize=(20,20),\n                  subplots=True,\n                  layout=(10,1),\n                  rot=0,\n                  lw=1,\n                  title='segmanet id max')\nplt.show()","metadata":{"papermill":{"duration":3.018846,"end_time":"2021-12-17T18:59:08.370156","exception":false,"start_time":"2021-12-17T18:59:05.351310","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:00:08.013153Z","iopub.execute_input":"2021-12-21T14:00:08.015599Z","iopub.status.idle":"2021-12-21T14:00:11.029669Z","shell.execute_reply.started":"2021-12-21T14:00:08.015552Z","shell.execute_reply":"2021-12-21T14:00:11.028869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_features(signal, ts, sensor_id):\n    X = pd.DataFrame()\n    f = np.fft.fft(signal)\n    f_real = np.real(f)\n    X.loc[ts, f'{sensor_id}_sum'] = signal.sum()\n    X.loc[ts, f'{sensor_id}_mean'] = signal.mean()\n    X.loc[ts, f'{sensor_id}_std'] = signal.std()\n    X.loc[ts, f'{sensor_id}_var'] = signal.var()\n    X.loc[ts, f'{sensor_id}_max'] = signal.max()\n    X.loc[ts, f'{sensor_id}_min'] = signal.min()\n    X.loc[ts, f'{sensor_id}_skew'] = signal.skew()\n    X.loc[ts, f'{sensor_id}_mad'] = signal.mad()\n    X.loc[ts, f'{sensor_id}_kurtosis'] = signal.kurtosis()\n    X.loc[ts, f'{sensor_id}_quantile99'] = np.quantile(signal, 0.99)\n    X.loc[ts, f'{sensor_id}_quantile95'] = np.quantile(signal, 0.95)\n    X.loc[ts, f'{sensor_id}_quantile85'] = np.quantile(signal, 0.85)\n    X.loc[ts, f'{sensor_id}_quantile75'] = np.quantile(signal, 0.75)\n    X.loc[ts, f'{sensor_id}_quantile55'] = np.quantile(signal, 0.55)\n    X.loc[ts, f'{sensor_id}_quantile45'] = np.quantile(signal, 0.45)\n    X.loc[ts, f'{sensor_id}_quantile25'] = np.quantile(signal, 0.25)\n    X.loc[ts, f'{sensor_id}_quantile15'] = np.quantile(signal, 0.15)\n    X.loc[ts, f'{sensor_id}_quantile05'] = np.quantile(signal, 0.05)\n    X.loc[ts, f'{sensor_id}_quantile01'] = np.quantile(signal, 0.01)\n    X.loc[ts, f'{sensor_id}_fft_real_mean'] = f_real.mean()\n    X.loc[ts, f'{sensor_id}_fft_real_std'] = f_real.std()\n    X.loc[ts, f'{sensor_id}_fft_real_max'] = f_real.max()\n    X.loc[ts, f'{sensor_id}_fft_real_min'] = f_real.min()\n    \n    return X","metadata":{"papermill":{"duration":0.079684,"end_time":"2021-12-17T18:59:08.513362","exception":false,"start_time":"2021-12-17T18:59:08.433678","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:00:11.030953Z","iopub.execute_input":"2021-12-21T14:00:11.031401Z","iopub.status.idle":"2021-12-21T14:00:11.046005Z","shell.execute_reply.started":"2021-12-21T14:00:11.031347Z","shell.execute_reply":"2021-12-21T14:00:11.045009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_set = list()\nseg = 0\n\nfor seg, segment_id in enumerate(train.segment_id):\n    signals = pd.read_csv(PATH+'train/'+str(segment_id)+'.csv')\n    train_row = []\n    \n    if seg%200 == 0:\n        print('Processing segment_id={}'.format(seg))\n        \n    for sensor in range(0, 10):\n        sensor_id = f'sensor_{sensor+1}'\n        train_row.append(build_features(signals[sensor_id].fillna(0), segment_id, sensor_id))\n        \n    train_row = pd.concat(train_row, axis=1)\n    train_set.append(train_row)\n    seg+=1\n    \ntrain_set = pd.concat(train_set)","metadata":{"papermill":{"duration":2467.7067,"end_time":"2021-12-17T19:40:16.282398","exception":false,"start_time":"2021-12-17T18:59:08.575698","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:00:11.047317Z","iopub.execute_input":"2021-12-21T14:00:11.047582Z","iopub.status.idle":"2021-12-21T14:40:16.462829Z","shell.execute_reply.started":"2021-12-21T14:00:11.047551Z","shell.execute_reply":"2021-12-21T14:40:16.462208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = train_set.reset_index()\ntrain_set = train_set.rename(columns={'index':  'segment_id'})\n\ntrain_set = pd.merge(train_set, train, on='segment_id')","metadata":{"papermill":{"duration":0.124612,"end_time":"2021-12-17T19:40:16.476791","exception":false,"start_time":"2021-12-17T19:40:16.352179","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:40:16.464839Z","iopub.execute_input":"2021-12-21T14:40:16.465149Z","iopub.status.idle":"2021-12-21T14:40:16.508970Z","shell.execute_reply.started":"2021-12-21T14:40:16.465107Z","shell.execute_reply":"2021-12-21T14:40:16.507978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_set.head(3))","metadata":{"papermill":{"duration":0.090459,"end_time":"2021-12-17T19:40:16.635124","exception":false,"start_time":"2021-12-17T19:40:16.544665","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:40:16.510230Z","iopub.execute_input":"2021-12-21T14:40:16.510576Z","iopub.status.idle":"2021-12-21T14:40:16.527151Z","shell.execute_reply.started":"2021-12-21T14:40:16.510540Z","shell.execute_reply":"2021-12-21T14:40:16.526185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = []\nfor dirname, _, filenames in os.walk(PATH+'test/'):\n    for filename in filenames:\n        test_files.append(filename[:-4])\n        \ntest = pd.DataFrame(test_files, columns=['segment_id'])","metadata":{"papermill":{"duration":1.278002,"end_time":"2021-12-17T19:40:17.981761","exception":false,"start_time":"2021-12-17T19:40:16.703759","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:40:16.528578Z","iopub.execute_input":"2021-12-21T14:40:16.528963Z","iopub.status.idle":"2021-12-21T14:40:17.796474Z","shell.execute_reply.started":"2021-12-21T14:40:16.528930Z","shell.execute_reply":"2021-12-21T14:40:17.795576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = list()\nseg = 0\n\nfor seg, segment_id in enumerate(test.segment_id):\n    signals = pd.read_csv(PATH+'test/'+str(segment_id)+'.csv')\n    test_row = []\n    \n    if seg%200 == 0:\n        print('Processing segment_id={}'.format(seg))\n        \n    for sensor in range(0, 10):\n        sensor_id = f'sensor_{sensor+1}'\n        test_row.append(build_features(signals[sensor_id].fillna(0), segment_id, sensor_id))\n        \n    test_row = pd.concat(test_row, axis=1)\n    test_set.append(test_row)\n    seg+=1\n    \ntest_set = pd.concat(test_set)","metadata":{"papermill":{"duration":2436.526438,"end_time":"2021-12-17T20:20:54.577211","exception":false,"start_time":"2021-12-17T19:40:18.050773","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T14:40:17.797751Z","iopub.execute_input":"2021-12-21T14:40:17.797998Z","iopub.status.idle":"2021-12-21T15:21:18.370899Z","shell.execute_reply.started":"2021-12-21T14:40:17.797967Z","shell.execute_reply":"2021-12-21T15:21:18.369961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = test_set.reset_index()\ntest_set = test_set.rename(columns={'index':  'segment_id'})\n\ntest_set = pd.merge(test_set, test, on='segment_id')","metadata":{"papermill":{"duration":0.128378,"end_time":"2021-12-17T20:20:54.783326","exception":false,"start_time":"2021-12-17T20:20:54.654948","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:21:18.372697Z","iopub.execute_input":"2021-12-21T15:21:18.373582Z","iopub.status.idle":"2021-12-21T15:21:18.416604Z","shell.execute_reply.started":"2021-12-21T15:21:18.373535Z","shell.execute_reply":"2021-12-21T15:21:18.415461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_set.head(3))","metadata":{"papermill":{"duration":0.098735,"end_time":"2021-12-17T20:20:54.957637","exception":false,"start_time":"2021-12-17T20:20:54.858902","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:21:18.418429Z","iopub.execute_input":"2021-12-21T15:21:18.418868Z","iopub.status.idle":"2021-12-21T15:21:18.438329Z","shell.execute_reply.started":"2021-12-21T15:21:18.418818Z","shell.execute_reply":"2021-12-21T15:21:18.437390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_set.drop(['segment_id', 'time_to_eruption'], axis=1)\ny = train_set['time_to_eruption']\n\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, \n                                                      test_size=0.2,\n                                                      random_state=42)","metadata":{"papermill":{"duration":0.113477,"end_time":"2021-12-17T20:20:55.148671","exception":false,"start_time":"2021-12-17T20:20:55.035194","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:21:18.440226Z","iopub.execute_input":"2021-12-21T15:21:18.440499Z","iopub.status.idle":"2021-12-21T15:21:18.472755Z","shell.execute_reply.started":"2021-12-21T15:21:18.440466Z","shell.execute_reply":"2021-12-21T15:21:18.471652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.head(3))\nprint('np.shape(X_train) = ', np.shape(X_train))","metadata":{"papermill":{"duration":0.095975,"end_time":"2021-12-17T20:20:55.322869","exception":false,"start_time":"2021-12-17T20:20:55.226894","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:21:18.477498Z","iopub.execute_input":"2021-12-21T15:21:18.478240Z","iopub.status.idle":"2021-12-21T15:21:18.496503Z","shell.execute_reply.started":"2021-12-21T15:21:18.478147Z","shell.execute_reply":"2021-12-21T15:21:18.495456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train.head(3))\nprint('np.shape(y_train) = ', np.shape(y_train))","metadata":{"papermill":{"duration":0.087649,"end_time":"2021-12-17T20:20:55.486715","exception":false,"start_time":"2021-12-17T20:20:55.399066","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:21:18.498190Z","iopub.execute_input":"2021-12-21T15:21:18.499022Z","iopub.status.idle":"2021-12-21T15:21:18.514135Z","shell.execute_reply.started":"2021-12-21T15:21:18.498956Z","shell.execute_reply":"2021-12-21T15:21:18.513469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\n\nmodel = RandomForestRegressor(max_depth=20, random_state=0)\nmodel.fit(X_train, y_train)","metadata":{"papermill":{"duration":54.290346,"end_time":"2021-12-17T20:21:49.854051","exception":false,"start_time":"2021-12-17T20:20:55.563705","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:21:18.515156Z","iopub.execute_input":"2021-12-21T15:21:18.515418Z","iopub.status.idle":"2021-12-21T15:22:12.925970Z","shell.execute_reply.started":"2021-12-21T15:21:18.515379Z","shell.execute_reply":"2021-12-21T15:22:12.925104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_valid)","metadata":{"papermill":{"duration":0.126058,"end_time":"2021-12-17T20:21:50.056592","exception":false,"start_time":"2021-12-17T20:21:49.930534","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:22:12.927518Z","iopub.execute_input":"2021-12-21T15:22:12.927750Z","iopub.status.idle":"2021-12-21T15:22:12.974153Z","shell.execute_reply.started":"2021-12-21T15:22:12.927722Z","shell.execute_reply":"2021-12-21T15:22:12.973408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import r2_score\n\nconf_mat = r2_score(y_valid, y_pred)\nprint(conf_mat)","metadata":{"papermill":{"duration":0.08682,"end_time":"2021-12-17T20:21:50.221958","exception":false,"start_time":"2021-12-17T20:21:50.135138","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:22:12.975510Z","iopub.execute_input":"2021-12-21T15:22:12.975720Z","iopub.status.idle":"2021-12-21T15:22:12.981631Z","shell.execute_reply.started":"2021-12-21T15:22:12.975694Z","shell.execute_reply":"2021-12-21T15:22:12.980518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\n\nmse = mean_squared_error(y_valid, y_pred)\nfig = plt.figure()\nmulreg = fig.add_subplot(1, 1, 1)\nmulreg.scatter(y_valid, y_pred, color='r')\nmulreg.set_title('Nonlinear Regression') ","metadata":{"papermill":{"duration":0.368435,"end_time":"2021-12-17T20:21:50.677492","exception":false,"start_time":"2021-12-17T20:21:50.309057","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:22:12.982966Z","iopub.execute_input":"2021-12-21T15:22:12.983384Z","iopub.status.idle":"2021-12-21T15:22:13.230013Z","shell.execute_reply.started":"2021-12-21T15:22:12.983341Z","shell.execute_reply":"2021-12-21T15:22:13.229236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test_set.drop(columns=['segment_id']))","metadata":{"papermill":{"duration":0.235972,"end_time":"2021-12-17T20:21:51.002076","exception":false,"start_time":"2021-12-17T20:21:50.766104","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:22:13.231066Z","iopub.execute_input":"2021-12-21T15:22:13.231273Z","iopub.status.idle":"2021-12-21T15:22:13.362180Z","shell.execute_reply.started":"2021-12-21T15:22:13.231248Z","shell.execute_reply":"2021-12-21T15:22:13.361194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame()  \nsubmission['segment_id'] = test_set['segment_id']\nsubmission['time_to_eruption'] = prediction\nsubmission.to_csv('submission.csv', header=True, index=False)","metadata":{"papermill":{"duration":0.119152,"end_time":"2021-12-17T20:21:51.200633","exception":false,"start_time":"2021-12-17T20:21:51.081481","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-12-21T15:22:13.363581Z","iopub.execute_input":"2021-12-21T15:22:13.364000Z","iopub.status.idle":"2021-12-21T15:22:13.396133Z","shell.execute_reply.started":"2021-12-21T15:22:13.363952Z","shell.execute_reply":"2021-12-21T15:22:13.394857Z"},"trusted":true},"execution_count":null,"outputs":[]}]}