{"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":"from fastai.tabular.all import * \npd.options.display.max_columns = 50","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-08T21:48:39.393087Z","iopub.execute_input":"2023-01-08T21:48:39.393568Z","iopub.status.idle":"2023-01-08T21:48:39.398926Z","shell.execute_reply.started":"2023-01-08T21:48:39.393518Z","shell.execute_reply":"2023-01-08T21:48:39.397843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:49:09.623435Z","iopub.execute_input":"2023-01-08T21:49:09.623850Z","iopub.status.idle":"2023-01-08T21:49:18.287704Z","shell.execute_reply.started":"2023-01-08T21:49:09.623817Z","shell.execute_reply":"2023-01-08T21:49:18.286333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\")\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:50:05.052358Z","iopub.execute_input":"2023-01-08T21:50:05.053456Z","iopub.status.idle":"2023-01-08T21:50:11.531369Z","shell.execute_reply.started":"2023-01-08T21:50:05.053415Z","shell.execute_reply":"2023-01-08T21:50:11.529965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Exploration","metadata":{}},{"cell_type":"code","source":"train_df.shape, test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:50:51.178579Z","iopub.execute_input":"2023-01-08T21:50:51.179007Z","iopub.status.idle":"2023-01-08T21:50:51.187183Z","shell.execute_reply.started":"2023-01-08T21:50:51.178971Z","shell.execute_reply":"2023-01-08T21:50:51.185930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:51:07.757536Z","iopub.execute_input":"2023-01-08T21:51:07.757966Z","iopub.status.idle":"2023-01-08T21:51:07.856613Z","shell.execute_reply.started":"2023-01-08T21:51:07.757932Z","shell.execute_reply":"2023-01-08T21:51:07.855319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe().T","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:51:34.127839Z","iopub.execute_input":"2023-01-08T21:51:34.129181Z","iopub.status.idle":"2023-01-08T21:51:35.293927Z","shell.execute_reply.started":"2023-01-08T21:51:34.129131Z","shell.execute_reply":"2023-01-08T21:51:35.292636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = train_df.corr()\ncorr.style.background_gradient(cmap='coolwarm')","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:51:53.214785Z","iopub.execute_input":"2023-01-08T21:51:53.215228Z","iopub.status.idle":"2023-01-08T21:51:55.983244Z","shell.execute_reply.started":"2023-01-08T21:51:53.215197Z","shell.execute_reply":"2023-01-08T21:51:55.982391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:52:19.997975Z","iopub.execute_input":"2023-01-08T21:52:19.998416Z","iopub.status.idle":"2023-01-08T21:52:20.107722Z","shell.execute_reply.started":"2023-01-08T21:52:19.998380Z","shell.execute_reply":"2023-01-08T21:52:20.106473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"string_var = 'f_27'","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:53:01.663623Z","iopub.execute_input":"2023-01-08T21:53:01.664761Z","iopub.status.idle":"2023-01-08T21:53:01.669352Z","shell.execute_reply.started":"2023-01-08T21:53:01.664721Z","shell.execute_reply":"2023-01-08T21:53:01.668419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_27(df, do_convert = True):\n    if do_convert:\n        for i in range(10):\n            df[f'f_27_{i}'] = df[string_var].str.get(i)\n    df.drop([string_var], axis=1, inplace=True)\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:53:27.327719Z","iopub.execute_input":"2023-01-08T21:53:27.328804Z","iopub.status.idle":"2023-01-08T21:53:27.333587Z","shell.execute_reply.started":"2023-01-08T21:53:27.328763Z","shell.execute_reply":"2023-01-08T21:53:27.332747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = feature_27(train_df, do_convert=True)\ntest_df = feature_27(test_df, do_convert=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:53:44.552407Z","iopub.execute_input":"2023-01-08T21:53:44.552903Z","iopub.status.idle":"2023-01-08T21:53:53.634101Z","shell.execute_reply.started":"2023-01-08T21:53:44.552865Z","shell.execute_reply":"2023-01-08T21:53:53.632765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape, test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:54:04.579091Z","iopub.execute_input":"2023-01-08T21:54:04.579475Z","iopub.status.idle":"2023-01-08T21:54:04.587028Z","shell.execute_reply.started":"2023-01-08T21:54:04.579443Z","shell.execute_reply":"2023-01-08T21:54:04.585869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:54:21.613098Z","iopub.execute_input":"2023-01-08T21:54:21.613657Z","iopub.status.idle":"2023-01-08T21:54:21.656686Z","shell.execute_reply.started":"2023-01-08T21:54:21.613609Z","shell.execute_reply":"2023-01-08T21:54:21.655600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cont_names = [col for col in train_df if train_df[col].dtype ==\"float64\"]\ncat_names = [col for col in train_df if train_df[col].dtype !=\"float64\"]\ncont_names","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:54:43.611835Z","iopub.execute_input":"2023-01-08T21:54:43.612259Z","iopub.status.idle":"2023-01-08T21:54:43.623209Z","shell.execute_reply.started":"2023-01-08T21:54:43.612223Z","shell.execute_reply":"2023-01-08T21:54:43.622303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_names","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:55:03.117921Z","iopub.execute_input":"2023-01-08T21:55:03.118358Z","iopub.status.idle":"2023-01-08T21:55:03.125871Z","shell.execute_reply.started":"2023-01-08T21:55:03.118323Z","shell.execute_reply":"2023-01-08T21:55:03.124718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_names.remove(\"target\")\ncat_names.remove(\"id\")","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:55:36.813070Z","iopub.execute_input":"2023-01-08T21:55:36.813641Z","iopub.status.idle":"2023-01-08T21:55:36.838960Z","shell.execute_reply.started":"2023-01-08T21:55:36.813588Z","shell.execute_reply":"2023-01-08T21:55:36.837477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"splits = RandomSplitter(valid_pct=0.2)(range_of(train_df))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:55:50.863872Z","iopub.execute_input":"2023-01-08T21:55:50.864280Z","iopub.status.idle":"2023-01-08T21:55:51.019666Z","shell.execute_reply.started":"2023-01-08T21:55:50.864250Z","shell.execute_reply":"2023-01-08T21:55:51.018450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"splits","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:56:04.227627Z","iopub.execute_input":"2023-01-08T21:56:04.228737Z","iopub.status.idle":"2023-01-08T21:56:04.234527Z","shell.execute_reply.started":"2023-01-08T21:56:04.228702Z","shell.execute_reply":"2023-01-08T21:56:04.233613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"code","source":"to = TabularPandas(train_df,\n                   procs = [Categorify,Normalize],\n                   cat_names = cat_names,\n                   cont_names = cont_names,\n                   y_names='target',\n                   y_block = CategoryBlock,\n                   splits=splits)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:56:43.576884Z","iopub.execute_input":"2023-01-08T21:56:43.577289Z","iopub.status.idle":"2023-01-08T21:56:46.421993Z","shell.execute_reply.started":"2023-01-08T21:56:43.577254Z","shell.execute_reply":"2023-01-08T21:56:46.420845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to.xs.iloc[:2]","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:57:01.153737Z","iopub.execute_input":"2023-01-08T21:57:01.154204Z","iopub.status.idle":"2023-01-08T21:57:01.228457Z","shell.execute_reply.started":"2023-01-08T21:57:01.154160Z","shell.execute_reply":"2023-01-08T21:57:01.227149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to.xs.shape,train_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:57:18.454191Z","iopub.execute_input":"2023-01-08T21:57:18.454627Z","iopub.status.idle":"2023-01-08T21:57:18.509078Z","shell.execute_reply.started":"2023-01-08T21:57:18.454593Z","shell.execute_reply":"2023-01-08T21:57:18.507893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = to.dataloaders(bs=2048)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:57:40.908752Z","iopub.execute_input":"2023-01-08T21:57:40.909181Z","iopub.status.idle":"2023-01-08T21:57:40.965981Z","shell.execute_reply.started":"2023-01-08T21:57:40.909147Z","shell.execute_reply":"2023-01-08T21:57:40.964802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:57:54.073836Z","iopub.execute_input":"2023-01-08T21:57:54.074385Z","iopub.status.idle":"2023-01-08T21:57:55.071931Z","shell.execute_reply.started":"2023-01-08T21:57:54.074337Z","shell.execute_reply":"2023-01-08T21:57:55.070765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = tabular_learner(dls, \n                        metrics=accuracy, \n                        layers = [512,256,128,128,64], \n                        wd=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:58:24.669730Z","iopub.execute_input":"2023-01-08T21:58:24.670097Z","iopub.status.idle":"2023-01-08T21:58:24.690312Z","shell.execute_reply.started":"2023-01-08T21:58:24.670067Z","shell.execute_reply":"2023-01-08T21:58:24.689096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_min,lr_steep = learn.lr_find(suggest_funcs=(minimum, steep))","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:58:48.567691Z","iopub.execute_input":"2023-01-08T21:58:48.568125Z","iopub.status.idle":"2023-01-08T21:58:55.995027Z","shell.execute_reply.started":"2023-01-08T21:58:48.568088Z","shell.execute_reply":"2023-01-08T21:58:55.994044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Minimum/10: {lr_min:.2e}, steepest point: {lr_steep:.2e}\")","metadata":{"execution":{"iopub.status.busy":"2023-01-08T21:59:52.913636Z","iopub.execute_input":"2023-01-08T21:59:52.914084Z","iopub.status.idle":"2023-01-08T21:59:52.920746Z","shell.execute_reply.started":"2023-01-08T21:59:52.914051Z","shell.execute_reply":"2023-01-08T21:59:52.919530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(30, lr_steep)","metadata":{"execution":{"iopub.status.busy":"2023-01-08T22:00:12.097983Z","iopub.execute_input":"2023-01-08T22:00:12.098395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.summary()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row, clas, probs = learn.predict(train_df.iloc[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.drop(\"id\", axis=1, inplace= True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dl = learn.dls.test_dl(test_df)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dl","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = learn.get_preds(dl=dl)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = learn.get_preds(dl=dl)[0].argmax(1).numpy()\npreds[:5]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/sample_submission.csv\")\nsample","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame({'id':sample.id, 'target': preds})\nsub.to_csv('submission.csv', index=False)\nsub.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = to.train.xs, to.train.ys.values.ravel()\nX_valid, y_valid = to.valid.xs, to.valid.ys.values.ravel()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_valid","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape, X_valid.shape, y_valid.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfrom sklearn.ensemble import HistGradientBoostingClassifier\nclf = HistGradientBoostingClassifier().fit(X_train, y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.score(X_valid, y_valid)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfrom sklearn.ensemble import RandomForestClassifier\nclf1 = RandomForestClassifier().fit(X_train, y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf1.score(X_valid, y_valid)","metadata":{},"execution_count":null,"outputs":[]}]}