{"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\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 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-11-29T14:46:17.787423Z","iopub.execute_input":"2022-11-29T14:46:17.788606Z","iopub.status.idle":"2022-11-29T14:46:26.451716Z","shell.execute_reply.started":"2022-11-29T14:46:17.788498Z","shell.execute_reply":"2022-11-29T14:46:26.450324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\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 tqdm import tqdm\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))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T14:46:26.453701Z","iopub.execute_input":"2022-11-29T14:46:26.455234Z","iopub.status.idle":"2022-11-29T14:46:30.656511Z","shell.execute_reply.started":"2022-11-29T14:46:26.455169Z","shell.execute_reply":"2022-11-29T14:46:30.655618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Segment_id - номер сегменту з якого знімаються покази датчиків. Time_to_eruption - час до виверження","metadata":{}},{"cell_type":"code","source":"print(file_list[0])\n\n\nprint(pd.read_csv(file_list[0]))\nprint(pd.read_csv(file_list[0]).isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-11-29T14:46:30.658371Z","iopub.execute_input":"2022-11-29T14:46:30.658888Z","iopub.status.idle":"2022-11-29T14:46:30.698646Z","shell.execute_reply.started":"2022-11-29T14:46:30.658841Z","shell.execute_reply":"2022-11-29T14:46:30.697400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Папка train містить файли з показниками показів датчиків","metadata":{}},{"cell_type":"markdown","source":"Десять хвилин журналів від десяти різних датчиків, розташованих навколо вулкана","metadata":{}},{"cell_type":"code","source":"print(file_list_train[0])\nprint(pd.read_csv(file_list_train[0]))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T14:46:30.701761Z","iopub.execute_input":"2022-11-29T14:46:30.702883Z","iopub.status.idle":"2022-11-29T14:46:30.835902Z","shell.execute_reply.started":"2022-11-29T14:46:30.702843Z","shell.execute_reply":"2022-11-29T14:46:30.835015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(file_list[1])\nprint(pd.read_csv(file_list[1]))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T14:46:30.837071Z","iopub.execute_input":"2022-11-29T14:46:30.837859Z","iopub.status.idle":"2022-11-29T14:46:30.856574Z","shell.execute_reply.started":"2022-11-29T14:46:30.837822Z","shell.execute_reply":"2022-11-29T14:46:30.855422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Папка test містить набори значень датчиків але вже без відповідного значення часу до виверження","metadata":{}},{"cell_type":"code","source":"print(file_list_test[0])\nprint(pd.read_csv(file_list_test[0]))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T14:46:30.858175Z","iopub.execute_input":"2022-11-29T14:46:30.858536Z","iopub.status.idle":"2022-11-29T14:46:30.994970Z","shell.execute_reply.started":"2022-11-29T14:46:30.858502Z","shell.execute_reply":"2022-11-29T14:46:30.993747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(file_list_test)) \nkeys = list(pd.read_csv(file_list_test[0]).keys()) \nprint(keys)\nnanC = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\nfor index in range(len(file_list_test)):\n    if(index % 200 == 0): \n        print(index)\n    df = pd.read_csv(file_list_test[index]) \n    for key in df.keys():\n        if df[key].isna().sum() == len(pd.read_csv(file_list_train[0])):            \n            nanC[keys.index(key)] += 1\n\nprint(nanC)\ndata={'sensors': keys, 'count': nanC}\nNaNDF = pd.DataFrame(data)\nprint(NaNDF)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T14:46:30.996468Z","iopub.execute_input":"2022-11-29T14:46:30.997537Z","iopub.status.idle":"2022-11-29T15:39:10.952074Z","shell.execute_reply.started":"2022-11-29T14:46:30.997484Z","shell.execute_reply":"2022-11-29T15:39:10.950253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NaNDF.plot(figsize =(20, 20), x=\"sensors\", y=\"count\", kind=\"bar\",  rot=5, fontsize=14 )","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:10.954616Z","iopub.execute_input":"2022-11-29T15:39:10.955166Z","iopub.status.idle":"2022-11-29T15:39:11.482130Z","shell.execute_reply.started":"2022-11-29T15:39:10.955126Z","shell.execute_reply":"2022-11-29T15:39:11.479706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Кількість файлів у тестовому наборі","metadata":{}},{"cell_type":"code","source":"print(len(file_list_test))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:11.483871Z","iopub.execute_input":"2022-11-29T15:39:11.484317Z","iopub.status.idle":"2022-11-29T15:39:11.491993Z","shell.execute_reply.started":"2022-11-29T15:39:11.484277Z","shell.execute_reply":"2022-11-29T15:39:11.490594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Кількість файлів у наборі для тренування\n\n","metadata":{}},{"cell_type":"code","source":"print(len(file_list_train))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:11.498778Z","iopub.execute_input":"2022-11-29T15:39:11.500388Z","iopub.status.idle":"2022-11-29T15:39:11.506844Z","shell.execute_reply.started":"2022-11-29T15:39:11.500338Z","shell.execute_reply":"2022-11-29T15:39:11.505707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Перевірка чи наявні файли з однією назвою в наборах для навчання та тренування","metadata":{}},{"cell_type":"code","source":"files_train = [file.split('/')[-1].split('.')[-2] for file in file_list_train]\nfiles_test = [file.split('/')[-1].split('.')[-2] for file in file_list_test]","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:11.508704Z","iopub.execute_input":"2022-11-29T15:39:11.509408Z","iopub.status.idle":"2022-11-29T15:39:11.528519Z","shell.execute_reply.started":"2022-11-29T15:39:11.509354Z","shell.execute_reply":"2022-11-29T15:39:11.527094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Перші 10 файлів тренування","metadata":{}},{"cell_type":"code","source":"print(files_train[0:10])","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:11.530462Z","iopub.execute_input":"2022-11-29T15:39:11.530947Z","iopub.status.idle":"2022-11-29T15:39:11.539905Z","shell.execute_reply.started":"2022-11-29T15:39:11.530891Z","shell.execute_reply":"2022-11-29T15:39:11.538738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(files_test[0:10])","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:11.541448Z","iopub.execute_input":"2022-11-29T15:39:11.542376Z","iopub.status.idle":"2022-11-29T15:39:11.551006Z","shell.execute_reply.started":"2022-11-29T15:39:11.542325Z","shell.execute_reply":"2022-11-29T15:39:11.549451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Кількість перетинів індексів файлів","metadata":{}},{"cell_type":"code","source":"test_set = set(files_test)\ntrain_set = set(files_train)\ninter = test_set.intersection(train_set)\n\nprint(inter)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:11.552870Z","iopub.execute_input":"2022-11-29T15:39:11.553340Z","iopub.status.idle":"2022-11-29T15:39:11.564320Z","shell.execute_reply.started":"2022-11-29T15:39:11.553251Z","shell.execute_reply":"2022-11-29T15:39:11.562861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Для подальшої роботи зчитуємо дані тренування у Data Frame","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(PATH+'train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:11.565933Z","iopub.execute_input":"2022-11-29T15:39:11.566751Z","iopub.status.idle":"2022-11-29T15:39:11.585016Z","shell.execute_reply.started":"2022-11-29T15:39:11.566695Z","shell.execute_reply":"2022-11-29T15:39:11.583966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Діаграма розподілення часу.","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2022-11-29T15:39:11.586549Z","iopub.execute_input":"2022-11-29T15:39:11.587460Z","iopub.status.idle":"2022-11-29T15:39:12.060185Z","shell.execute_reply.started":"2022-11-29T15:39:11.587413Z","shell.execute_reply":"2022-11-29T15:39:12.059056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Описова статистика ознаки часу","metadata":{}},{"cell_type":"code","source":"train['time_to_eruption'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:12.061795Z","iopub.execute_input":"2022-11-29T15:39:12.062127Z","iopub.status.idle":"2022-11-29T15:39:12.076651Z","shell.execute_reply.started":"2022-11-29T15:39:12.062097Z","shell.execute_reply":"2022-11-29T15:39:12.075416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_segment_id = pd.read_csv(PATH+'train/800654756.csv')\n\ndf_segment_id.plot(figsize=(20,20),\n                  subplots=True,\n                  layout=(10,1),\n                  rot=0,\n                  lw=1,\n                  title='sergment_id #800654756')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:12.078551Z","iopub.execute_input":"2022-11-29T15:39:12.079025Z","iopub.status.idle":"2022-11-29T15:39:14.760540Z","shell.execute_reply.started":"2022-11-29T15:39:12.078990Z","shell.execute_reply":"2022-11-29T15:39:14.759714Z"},"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')\ndf_segment_id_max = pd.read_csv(PATH+'train/'+str(segment_id_max)+'.csv')","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:14.761746Z","iopub.execute_input":"2022-11-29T15:39:14.762490Z","iopub.status.idle":"2022-11-29T15:39:15.029619Z","shell.execute_reply.started":"2022-11-29T15:39:14.762456Z","shell.execute_reply":"2022-11-29T15:39:15.028747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_segment_id_min.plot(figsize=(20,20), subplots=True, layout=(10,1), rot=0, lw=1, title='segment_id #601524801 (min)')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:15.031007Z","iopub.execute_input":"2022-11-29T15:39:15.031548Z","iopub.status.idle":"2022-11-29T15:39:17.531750Z","shell.execute_reply.started":"2022-11-29T15:39:15.031515Z","shell.execute_reply":"2022-11-29T15:39:17.530577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Візуалізуємо покази, які відповідають мінімальному та максимальному часу.","metadata":{}},{"cell_type":"code","source":"df_segment_id_max.plot(figsize=(20,20), subplots=True, layout=(10,1), rot=0, lw=1, title='segment_id #1923243961 (max)')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T15:39:17.533445Z","iopub.execute_input":"2022-11-29T15:39:17.534171Z","iopub.status.idle":"2022-11-29T15:39:20.334727Z","shell.execute_reply.started":"2022-11-29T15:39:17.534123Z","shell.execute_reply":"2022-11-29T15:39:20.333495Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T15:39:20.336366Z","iopub.execute_input":"2022-11-29T15:39:20.336880Z","iopub.status.idle":"2022-11-29T15:39:20.354969Z","shell.execute_reply.started":"2022-11-29T15:39:20.336833Z","shell.execute_reply":"2022-11-29T15:39:20.353420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_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":{"execution":{"iopub.status.busy":"2022-11-29T15:39:20.356653Z","iopub.execute_input":"2022-11-29T15:39:20.357151Z","iopub.status.idle":"2022-11-29T16:21:23.252729Z","shell.execute_reply.started":"2022-11-29T15:39:20.357105Z","shell.execute_reply":"2022-11-29T16:21:23.251423Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T16:21:23.255190Z","iopub.execute_input":"2022-11-29T16:21:23.255560Z","iopub.status.idle":"2022-11-29T16:21:23.298900Z","shell.execute_reply.started":"2022-11-29T16:21:23.255527Z","shell.execute_reply":"2022-11-29T16:21:23.297681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_set.head(3))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T16:21:23.300635Z","iopub.execute_input":"2022-11-29T16:21:23.301026Z","iopub.status.idle":"2022-11-29T16:21:23.318494Z","shell.execute_reply.started":"2022-11-29T16:21:23.300990Z","shell.execute_reply":"2022-11-29T16:21:23.317240Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T16:21:23.320305Z","iopub.execute_input":"2022-11-29T16:21:23.321009Z","iopub.status.idle":"2022-11-29T16:21:27.146556Z","shell.execute_reply.started":"2022-11-29T16:21:23.320950Z","shell.execute_reply":"2022-11-29T16:21:27.145392Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T16:21:27.148282Z","iopub.execute_input":"2022-11-29T16:21:27.150155Z","iopub.status.idle":"2022-11-29T17:04:03.134426Z","shell.execute_reply.started":"2022-11-29T16:21:27.150104Z","shell.execute_reply":"2022-11-29T17:04:03.132905Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T17:04:03.141736Z","iopub.execute_input":"2022-11-29T17:04:03.142349Z","iopub.status.idle":"2022-11-29T17:04:03.184906Z","shell.execute_reply.started":"2022-11-29T17:04:03.142304Z","shell.execute_reply":"2022-11-29T17:04:03.183844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_set.head(3))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T17:04:03.186288Z","iopub.execute_input":"2022-11-29T17:04:03.186786Z","iopub.status.idle":"2022-11-29T17:04:03.206621Z","shell.execute_reply.started":"2022-11-29T17:04:03.186748Z","shell.execute_reply":"2022-11-29T17:04:03.205354Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T17:04:03.208369Z","iopub.execute_input":"2022-11-29T17:04:03.209571Z","iopub.status.idle":"2022-11-29T17:04:03.242048Z","shell.execute_reply.started":"2022-11-29T17:04:03.209522Z","shell.execute_reply":"2022-11-29T17:04:03.240903Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T17:04:03.243794Z","iopub.execute_input":"2022-11-29T17:04:03.244258Z","iopub.status.idle":"2022-11-29T17:04:03.262822Z","shell.execute_reply.started":"2022-11-29T17:04:03.244213Z","shell.execute_reply":"2022-11-29T17:04:03.261342Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T17:04:03.264271Z","iopub.execute_input":"2022-11-29T17:04:03.264612Z","iopub.status.idle":"2022-11-29T17:04:03.276538Z","shell.execute_reply.started":"2022-11-29T17:04:03.264580Z","shell.execute_reply":"2022-11-29T17:04:03.275343Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T17:04:03.278261Z","iopub.execute_input":"2022-11-29T17:04:03.279504Z","iopub.status.idle":"2022-11-29T17:04:53.005454Z","shell.execute_reply.started":"2022-11-29T17:04:03.279466Z","shell.execute_reply":"2022-11-29T17:04:53.004402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_valid)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T17:04:53.007017Z","iopub.execute_input":"2022-11-29T17:04:53.007373Z","iopub.status.idle":"2022-11-29T17:04:53.060589Z","shell.execute_reply.started":"2022-11-29T17:04:53.007332Z","shell.execute_reply":"2022-11-29T17:04:53.059678Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T17:04:53.061980Z","iopub.execute_input":"2022-11-29T17:04:53.062284Z","iopub.status.idle":"2022-11-29T17:04:53.303308Z","shell.execute_reply.started":"2022-11-29T17:04:53.062256Z","shell.execute_reply":"2022-11-29T17:04:53.302133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test_set.drop(columns=['segment_id']))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T17:04:53.304517Z","iopub.execute_input":"2022-11-29T17:04:53.304869Z","iopub.status.idle":"2022-11-29T17:04:53.452087Z","shell.execute_reply.started":"2022-11-29T17:04:53.304837Z","shell.execute_reply":"2022-11-29T17:04:53.450970Z"},"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":{"execution":{"iopub.status.busy":"2022-11-29T17:04:53.453379Z","iopub.execute_input":"2022-11-29T17:04:53.453761Z","iopub.status.idle":"2022-11-29T17:04:53.478293Z","shell.execute_reply.started":"2022-11-29T17:04:53.453728Z","shell.execute_reply":"2022-11-29T17:04:53.477168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keys = list(pd.read_csv(file_list_test[0]).keys()) \nprint(keys)\nnanC = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\nfor index in range(len(file_list_test)):\n    df = pd.read_csv(file_list_test[index]) \n    for key in df.keys():\n        if df[key].isna().sum() == len(pd.read_csv(file_list_train[0])):      \n            nanC[keys.index(key)] += 1\n\nprint(nanC)\ndata={'sensors': keys, 'count': nanC}\nNaNDF = pd.DataFrame(data)\nprint(NaNDF)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T17:04:53.479393Z","iopub.execute_input":"2022-11-29T17:04:53.479700Z","iopub.status.idle":"2022-11-29T18:02:02.776091Z","shell.execute_reply.started":"2022-11-29T17:04:53.479670Z","shell.execute_reply":"2022-11-29T18:02:02.772793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NaNDF.plot(figsize =(20, 20), x=\"sensors\", y=\"count\", kind=\"bar\",  rot=5, fontsize=14 )","metadata":{"execution":{"iopub.status.busy":"2022-11-29T18:02:02.782235Z","iopub.execute_input":"2022-11-29T18:02:02.782943Z","iopub.status.idle":"2022-11-29T18:02:03.189497Z","shell.execute_reply.started":"2022-11-29T18:02:02.782853Z","shell.execute_reply":"2022-11-29T18:02:03.188218Z"},"trusted":true},"execution_count":null,"outputs":[]}]}