{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Tabular models"},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.tabular import *\nfrom fastai.callbacks import EarlyStoppingCallback, ReduceLROnPlateauCallback\nfrom tqdm import tqdm_notebook as tqdm\nfrom sklearn.model_selection import StratifiedKFold","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/cat-in-the-dat-ii/') \ndf = pd.read_csv(path/'train.csv')\ndf.set_index('id',drop=True,inplace=True)\ntest_df = pd.read_csv(path/'test.csv')\ntest_df.set_index('id',drop=True,inplace=True)\nsample = pd.read_csv(path/'sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head(3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Cardinality of Categorical Variables"},{"metadata":{"trusted":true},"cell_type":"code","source":"df.bin_0.nunique(), df.bin_1.nunique(), df.bin_2.nunique(), df.bin_3.nunique(), df.bin_4.nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.nom_0.nunique(), df.nom_1.nunique(), df.nom_2.nunique(), df.nom_3.nunique(), df.nom_4.nunique(), df.nom_5.nunique(), df.nom_6.nunique(), df.nom_7.nunique(), df.nom_8.nunique(), df.nom_9.nunique(), ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.ord_0.nunique(), df.ord_1.nunique(), df.ord_2.nunique(), df.ord_3.nunique(), df.ord_4.nunique(), df.ord_5.nunique() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dep_var = 'target'\ncat_names = ['bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4', 'nom_0', 'nom_1',\n             'nom_2', 'nom_3', 'nom_4', 'nom_5', 'nom_6', 'nom_7', 'nom_8',\n             'nom_9','ord_0', 'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5',\n             'day', 'month']\nprocs = [FillMissing, Categorify, Normalize]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"embed = {}\nfor col in cat_names:\n    embed[col] = min(50, df[col].nunique()) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"embed","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# test = TabularList.from_df(test_df, path=path, cat_names=cat_names, cont_names=cont_names)#, procs=procs)\n\ntest = TabularList.from_df(test_df, path=path, cat_names=cat_names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_splits = 10\nskf = StratifiedKFold(num_splits, shuffle=True)#, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This Style of Callbacks and Cross-Validation is taken from Abhishek Thakur's public Kernel https://www.kaggle.com/abhishek/same-old-entity-embeddings"},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_list = []\nfor train_idx, valid_idx in skf.split(df.drop(labels='target', axis=1),df['target']):\n#     print(train_idx[:10], valid_idx[:10])\n#     print(len(train_idx), len(valid_idx))\n    data_fold = (TabularList.from_df(df, path=path, cat_names=cat_names, procs=procs)\n                 .split_by_idxs(train_idx, valid_idx)\n                 .label_from_df(cols=dep_var)\n                 .add_test(test)\n                 .databunch(bs=1024))\n#     print(len(data_fold.train_ds), len(data_fold.valid_ds), len(data_fold.test_ds))\n    \n    learn = tabular_learner(data_fold, layers=[300, 300], emb_drop=0.04, ps=[0.001, 0.01],metrics=AUROC(), emb_szs = embed,\n                            callback_fns=[partial(ReduceLROnPlateauCallback, monitor='auroc', min_delta=0.01, patience=1, min_lr=1e-06, factor=0.10, mode='max'),\n                                          partial(EarlyStoppingCallback, monitor='auroc', min_delta=0.001, patience=2, mode='max')])\n    learn.loss_func = nn.CrossEntropyLoss()\n#     print(learn.model)\n    learn.fit(10, 3e-3, wd = 0.2)\n    \n    pred = learn.get_preds(DatasetType.Test)[0][:, 1]\n    pred_list.append(pred.numpy())\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Inference"},{"metadata":{"trusted":true},"cell_type":"code","source":"len(pred_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_list","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np_preds = np.array(pred_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = np.sum(np_preds, axis=0)/num_splits","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"max(preds), min(preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_submision(preds):\n    sample['target'] = preds\n    return sample","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = get_submision(preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('./sub_10f_commit.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":" #### Have any Questions or Feedback ? Please comment below."}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.1"}},"nbformat":4,"nbformat_minor":1}