{"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\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nplt.style.use(\"seaborn-v0_8-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":"2023-05-26T02:01:37.633095Z","iopub.execute_input":"2023-05-26T02:01:37.633509Z","iopub.status.idle":"2023-05-26T02:01:42.592815Z","shell.execute_reply.started":"2023-05-26T02:01:37.633477Z","shell.execute_reply":"2023-05-26T02:01:42.591039Z"},"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":"2023-05-26T02:01:42.595254Z","iopub.execute_input":"2023-05-26T02:01:42.596145Z","iopub.status.idle":"2023-05-26T02:01:42.617725Z","shell.execute_reply.started":"2023-05-26T02:01:42.596105Z","shell.execute_reply":"2023-05-26T02:01:42.616744Z"},"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":"2023-05-26T02:01:42.619482Z","iopub.execute_input":"2023-05-26T02:01:42.620195Z","iopub.status.idle":"2023-05-26T02:01:42.725216Z","shell.execute_reply.started":"2023-05-26T02:01:42.620162Z","shell.execute_reply":"2023-05-26T02:01:42.723707Z"},"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":"2023-05-26T02:01:42.728089Z","iopub.execute_input":"2023-05-26T02:01:42.728476Z","iopub.status.idle":"2023-05-26T02:01:42.746794Z","shell.execute_reply.started":"2023-05-26T02:01:42.728443Z","shell.execute_reply":"2023-05-26T02:01:42.745233Z"},"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":"2023-05-26T02:01:42.748689Z","iopub.execute_input":"2023-05-26T02:01:42.749221Z","iopub.status.idle":"2023-05-26T02:01:42.903463Z","shell.execute_reply.started":"2023-05-26T02:01:42.749176Z","shell.execute_reply":"2023-05-26T02:01:42.902158Z"},"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":"2023-05-26T02:01:42.905722Z","iopub.execute_input":"2023-05-26T02:01:42.907296Z","iopub.status.idle":"2023-05-26T03:14:51.802071Z","shell.execute_reply.started":"2023-05-26T02:01:42.907219Z","shell.execute_reply":"2023-05-26T03:14:51.800223Z"},"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":"2023-05-26T03:14:51.805364Z","iopub.execute_input":"2023-05-26T03:14:51.805918Z","iopub.status.idle":"2023-05-26T03:14:52.620802Z","shell.execute_reply.started":"2023-05-26T03:14:51.805872Z","shell.execute_reply":"2023-05-26T03:14:52.619403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Кількість файлів у тестовому наборі","metadata":{}},{"cell_type":"code","source":"NaNDF.plot(figsize =(20, 20), x=\"sensors\", y=\"count\", kind=\"bar\",  rot=5, fontsize=14 )","metadata":{"execution":{"iopub.status.busy":"2023-05-26T03:14:52.622738Z","iopub.execute_input":"2023-05-26T03:14:52.6235Z","iopub.status.idle":"2023-05-26T03:14:53.24876Z","shell.execute_reply.started":"2023-05-26T03:14:52.623452Z","shell.execute_reply":"2023-05-26T03:14:53.247713Z"},"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":"2023-05-26T03:14:53.250522Z","iopub.execute_input":"2023-05-26T03:14:53.250867Z","iopub.status.idle":"2023-05-26T03:14:53.273739Z","shell.execute_reply.started":"2023-05-26T03:14:53.250836Z","shell.execute_reply":"2023-05-26T03:14:53.271413Z"},"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":"2023-05-26T03:14:53.28212Z","iopub.execute_input":"2023-05-26T03:14:53.283549Z","iopub.status.idle":"2023-05-26T03:14:53.289704Z","shell.execute_reply.started":"2023-05-26T03:14:53.283505Z","shell.execute_reply":"2023-05-26T03:14:53.288788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(files_test[0:10])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-26T03:14:53.291435Z","iopub.execute_input":"2023-05-26T03:14:53.292313Z","iopub.status.idle":"2023-05-26T03:14:53.305457Z","shell.execute_reply.started":"2023-05-26T03:14:53.292281Z","shell.execute_reply":"2023-05-26T03:14:53.30425Z"},"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":"2023-05-26T03:14:53.306968Z","iopub.execute_input":"2023-05-26T03:14:53.30817Z","iopub.status.idle":"2023-05-26T03:14:53.321041Z","shell.execute_reply.started":"2023-05-26T03:14:53.308126Z","shell.execute_reply":"2023-05-26T03:14:53.319314Z"},"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":"2023-05-26T03:14:53.322769Z","iopub.execute_input":"2023-05-26T03:14:53.323261Z","iopub.status.idle":"2023-05-26T03:14:53.343357Z","shell.execute_reply.started":"2023-05-26T03:14:53.323226Z","shell.execute_reply":"2023-05-26T03:14:53.342292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Діаграмма розподілення часу","metadata":{}},{"cell_type":"code","source":"sns.histplot(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":"2023-05-26T03:25:41.556411Z","iopub.status.idle":"2023-05-26T03:25:41.56116Z","shell.execute_reply.started":"2023-05-26T03:25:41.559932Z","shell.execute_reply":"2023-05-26T03:25:41.560086Z"},"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":"2023-05-26T03:14:53.961657Z","iopub.execute_input":"2023-05-26T03:14:53.962199Z","iopub.status.idle":"2023-05-26T03:14:53.978036Z","shell.execute_reply.started":"2023-05-26T03:14:53.962166Z","shell.execute_reply":"2023-05-26T03:14:53.976561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Візуалізуємо покази датчиків. Наприклад, вже розглянутого раніше файлу train/800654756.csv","metadata":{}},{"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":"2023-05-26T03:14:53.979631Z","iopub.execute_input":"2023-05-26T03:14:53.980005Z","iopub.status.idle":"2023-05-26T03:14:58.792317Z","shell.execute_reply.started":"2023-05-26T03:14:53.97997Z","shell.execute_reply":"2023-05-26T03:14:58.791409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Візуалізуємо покази, які відповідають мінімальному та максимальному часу.","metadata":{}},{"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":"2023-05-26T03:14:58.793383Z","iopub.execute_input":"2023-05-26T03:14:58.793689Z","iopub.status.idle":"2023-05-26T03:14:59.073008Z","shell.execute_reply.started":"2023-05-26T03:14:58.793662Z","shell.execute_reply":"2023-05-26T03:14:59.072132Z"},"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":"2023-05-26T03:14:59.077802Z","iopub.execute_input":"2023-05-26T03:14:59.078497Z","iopub.status.idle":"2023-05-26T03:15:03.963928Z","shell.execute_reply.started":"2023-05-26T03:14:59.078452Z","shell.execute_reply":"2023-05-26T03:15:03.962576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-05-26T03:15:03.965865Z","iopub.execute_input":"2023-05-26T03:15:03.966235Z","iopub.status.idle":"2023-05-26T03:15:08.607496Z","shell.execute_reply.started":"2023-05-26T03:15:03.966206Z","shell.execute_reply":"2023-05-26T03:15:08.606222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Як бачимо покази, що відповідають максимальному часу відрізняються за характеристиками амплітуди та частоти від показів, які відповідають мінімальному часу. Конкретна інтерпретація цих показів неможливо без знань типів датчиків та не входить до нашої задачі.","metadata":{}},{"cell_type":"markdown","source":"Підготовка даних для навчання (Data Preparation)","metadata":{}},{"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.mean()\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":"2023-05-26T03:37:54.591516Z","iopub.execute_input":"2023-05-26T03:37:54.592063Z","iopub.status.idle":"2023-05-26T03:37:54.609411Z","shell.execute_reply.started":"2023-05-26T03:37:54.592024Z","shell.execute_reply":"2023-05-26T03:37:54.607973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Формування DataFrame з даними тренування","metadata":{}},{"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":"2023-05-26T03:37:57.927237Z","iopub.execute_input":"2023-05-26T03:37:57.927722Z","iopub.status.idle":"2023-05-26T04:17:33.192063Z","shell.execute_reply.started":"2023-05-26T03:37:57.927685Z","shell.execute_reply":"2023-05-26T04:17:33.190365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_set.head(3))","metadata":{"execution":{"iopub.status.busy":"2023-05-26T04:37:13.953481Z","iopub.execute_input":"2023-05-26T04:37:13.95396Z","iopub.status.idle":"2023-05-26T04:37:13.975515Z","shell.execute_reply.started":"2023-05-26T04:37:13.953909Z","shell.execute_reply":"2023-05-26T04:37:13.973934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Дані для тесту","metadata":{}},{"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":"2023-05-26T04:38:38.018063Z","iopub.execute_input":"2023-05-26T04:38:38.018547Z","iopub.status.idle":"2023-05-26T04:38:39.246918Z","shell.execute_reply.started":"2023-05-26T04:38:38.018511Z","shell.execute_reply":"2023-05-26T04:38:39.246025Z"},"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":"2023-05-26T04:38:44.173422Z","iopub.execute_input":"2023-05-26T04:38:44.173795Z","iopub.status.idle":"2023-05-26T05:20:56.08829Z","shell.execute_reply.started":"2023-05-26T04:38:44.173767Z","shell.execute_reply":"2023-05-26T05:20:56.08666Z"},"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":"2023-05-26T05:24:20.680382Z","iopub.execute_input":"2023-05-26T05:24:20.681033Z","iopub.status.idle":"2023-05-26T05:24:20.733584Z","shell.execute_reply.started":"2023-05-26T05:24:20.680981Z","shell.execute_reply":"2023-05-26T05:24:20.732039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_set.head(3))","metadata":{"execution":{"iopub.status.busy":"2023-05-26T05:24:24.429197Z","iopub.execute_input":"2023-05-26T05:24:24.429635Z","iopub.status.idle":"2023-05-26T05:24:24.44848Z","shell.execute_reply.started":"2023-05-26T05:24:24.429602Z","shell.execute_reply":"2023-05-26T05:24:24.447275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Розділення даних тренування на набори для безпосередньо тренування та валідації","metadata":{}},{"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":"2023-05-26T05:24:27.64952Z","iopub.execute_input":"2023-05-26T05:24:27.650031Z","iopub.status.idle":"2023-05-26T05:24:27.691965Z","shell.execute_reply.started":"2023-05-26T05:24:27.649991Z","shell.execute_reply":"2023-05-26T05:24:27.690899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Вхідні дані тренування (статистичні характеристики показів датчиків)","metadata":{}},{"cell_type":"code","source":"print(X_train.head(3))\nprint('np.shape(X_train) = ', np.shape(X_train))","metadata":{"execution":{"iopub.status.busy":"2023-05-26T05:24:33.381736Z","iopub.execute_input":"2023-05-26T05:24:33.382177Z","iopub.status.idle":"2023-05-26T05:24:33.406671Z","shell.execute_reply.started":"2023-05-26T05:24:33.382142Z","shell.execute_reply":"2023-05-26T05:24:33.404659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Вихідні дані тренування (дані часу)","metadata":{}},{"cell_type":"code","source":"print(y_train.head(3))\nprint('np.shape(y_train) = ', np.shape(y_train))","metadata":{"execution":{"iopub.status.busy":"2023-05-26T05:24:36.301461Z","iopub.execute_input":"2023-05-26T05:24:36.302099Z","iopub.status.idle":"2023-05-26T05:24:36.31127Z","shell.execute_reply.started":"2023-05-26T05:24:36.302051Z","shell.execute_reply":"2023-05-26T05:24:36.309719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Побудуємо просту модель регресії методом Випадкових лісів.","metadata":{}},{"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":"2023-05-26T05:24:38.401686Z","iopub.execute_input":"2023-05-26T05:24:38.402168Z","iopub.status.idle":"2023-05-26T05:25:38.207757Z","shell.execute_reply.started":"2023-05-26T05:24:38.402133Z","shell.execute_reply":"2023-05-26T05:25:38.206148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Оцінка точності моделі (Evaluation)","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(X_valid)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T05:36:24.722583Z","iopub.status.idle":"2023-05-26T05:36:24.723074Z","shell.execute_reply.started":"2023-05-26T05:36:24.722811Z","shell.execute_reply":"2023-05-26T05:36:24.72283Z"},"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":"2023-05-26T05:36:24.714987Z","iopub.status.idle":"2023-05-26T05:36:24.715676Z","shell.execute_reply.started":"2023-05-26T05:36:24.71536Z","shell.execute_reply":"2023-05-26T05:36:24.715388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Впровадження моделі (Deployment) Прогноз на тестових даних","metadata":{}},{"cell_type":"code","source":"prediction = model.predict(test_set.drop(columns=['segment_id']))","metadata":{"execution":{"iopub.status.busy":"2023-05-26T05:36:24.718304Z","iopub.status.idle":"2023-05-26T05:36:24.71899Z","shell.execute_reply.started":"2023-05-26T05:36:24.71865Z","shell.execute_reply":"2023-05-26T05:36:24.718677Z"},"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":"2023-05-26T05:36:24.724531Z","iopub.status.idle":"2023-05-26T05:36:24.72534Z","shell.execute_reply.started":"2023-05-26T05:36:24.725094Z","shell.execute_reply":"2023-05-26T05:36:24.725118Z"},"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":"2023-05-26T05:36:24.711899Z","iopub.status.idle":"2023-05-26T05:36:24.712448Z","shell.execute_reply.started":"2023-05-26T05:36:24.712201Z","shell.execute_reply":"2023-05-26T05:36:24.712222Z"},"trusted":true},"execution_count":null,"outputs":[]}]}