{"cells":[{"metadata":{"_uuid":"66f86116d95d6ab19d5221468af20dad9c2b3f91"},"cell_type":"markdown","source":"# Tabular model"},{"metadata":{},"cell_type":"markdown","source":"![](https://media.giphy.com/media/l2JJu8U8SoHhQEnoQ/giphy.gif)"},{"metadata":{"trusted":true,"_uuid":"be215bbbf440c26b35a770cc34d0a8422965673d"},"cell_type":"code","source":"from fastai.tabular import *","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8b7424544878d09a9e63e024943ef65412f97ba4"},"cell_type":"markdown","source":"Tabular data should be in a Pandas `DataFrame`."},{"metadata":{},"cell_type":"markdown","source":"# Importing data"},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nsample_submission = pd.read_csv(\"../input/cat-in-the-dat-ii/sample_submission.csv\")\nX_test = pd.read_csv(\"../input/cat-in-the-dat-ii/test.csv\")\ntrain = pd.read_csv(\"../input/cat-in-the-dat-ii/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Select features"},{"metadata":{"trusted":true},"cell_type":"code","source":"procs = [FillMissing, Categorify, Normalize]\nFillMissing.FillStrategy='MEAN'\n\ndep_var = 'target'\ncat_names = ['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4', 'nom_0', 'nom_1', 'nom_2', \n             'nom_3', 'nom_4', 'nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9',\n            'ord_0', 'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5', 'day', 'month']\ncont_names = []\nX=train[cont_names]\nX=train[cat_names]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prepare train/test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH = Path('/kaggle/input/cat-in-the-dat-ii/')\ntest = TabularList.from_df(X_test, path=PATH, cat_names=cat_names, cont_names=cont_names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = (TabularList.from_df(train, path=PATH,\n                            cat_names=cat_names, \n                            cont_names=cont_names,\n                            procs=procs)\n                           .split_by_idx(valid_idx = range(len(train)-50000, len(train)))\n                           .label_from_df(cols=dep_var)\n                           .add_test(test)\n                           .databunch())\nprint(data.train_ds.cont_names)\nprint(data.train_ds.cat_names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train model"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = tabular_learner(data, layers=[200,100], metrics=accuracy,model_dir=\"/tmp/model/\", ps=0.15)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit(1, lr=1e-3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr=1e-03","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(1, slice(lr))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr=1e-03","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(2, slice(lr),wd=0.3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It shows that the model get stuck so I used 'wd' weight decay but is didnt seem to work"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('model_1')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Submission"},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = learn.get_preds(ds_type=DatasetType.Test)[0][:,1].numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'id':sample_submission['id'],'target':preds})\nsubmission.to_csv('my_submission.csv', header=True, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.describe()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":4}