{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\nimport pylab\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.ensemble import RandomForestClassifier\nimport xgboost\n\npylab.rcParams['figure.figsize'] = (15.0, 8.0)\nsns.set()\nSEED = 42","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mapord(s, values):\n    vmap = {name: i for i, name in enumerate(values)}\n    return s.map(lambda x: vmap.get(x, x))\ndef mapletter(s):\n    return s.map(lambda x: ord(x.lower()) - ord('a') if type(x) is str else x)\ndef split(X, y):\n    return train_test_split(X, y, random_state=SEED, stratify=y)\ndef proportion_plot(df, x, cat, kind='bar', **kwargs):\n    df.groupby(x)[cat].value_counts(normalize=True)\\\n    .rename('count')\\\n    .reset_index()\\\n    .pivot(index=x, columns=cat, values='count')\\\n    .plot(kind=kind, stacked=True, **kwargs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dftrain = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv')\ndftest = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv')\nsample = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/sample_submission.csv')\ndftrain.set_index('id', inplace=True)\ndftest.set_index('id', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"proportion_plot(dftrain, 'day', 'target')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"proportion_plot(dftrain, 'month', 'target')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(2,2, figsize=(17, 10))\nsns.countplot(data=dftrain, x='bin_0', ax=ax[0][0])\nsns.countplot(data=dftrain, x='bin_1', ax=ax[0][1])\nsns.countplot(data=dftrain, x='bin_2', ax=ax[1][0])\nsns.countplot(data=dftrain, x='bin_3', ax=ax[1][1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(3,2, figsize=(17, 20))\nsns.countplot(data=dftrain, x='nom_0', ax=ax[0][0])\nsns.countplot(data=dftrain, x='nom_1', ax=ax[0][1])\nsns.countplot(data=dftrain, x='nom_2', ax=ax[1][0])\nsns.countplot(data=dftrain, x='nom_3', ax=ax[1][1])\nsns.countplot(data=dftrain, x='nom_4', ax=ax[2][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(3,2, figsize=(17, 20))\nsns.countplot(data=dftrain, x='ord_0', ax=ax[0][0])\nsns.countplot(data=dftrain, x='ord_1', ax=ax[0][1])\nsns.countplot(data=dftrain, x='ord_2', ax=ax[1][0])\nsns.countplot(data=dftrain, x='ord_3', ax=ax[1][1])\nsns.countplot(data=dftrain, x='ord_4', ax=ax[2][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dftrain.fillna()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def fillna_mode(s):\n    return s.fillna(s.mode().iloc[0])\n\ndef map2letters(s):\n    values = sorted(s.unique())\n    dct = { v: i for i, v in enumerate(values) }\n    return s.map(lambda x: dct[x])\n    \n\n# Encoding and imputation\ny = dftrain['target'].copy()\ndfall = pd.concat([dftrain.drop(columns='target'), dftest])\ndrop = []\n\n# Binary\ndfall['bin_0'] = fillna_mode(dfall['bin_0'])\ndfall['bin_1'] = fillna_mode(dfall['bin_1'])\ndfall['bin_2'] = fillna_mode(dfall['bin_2'])\ntf_map = {'T': 1.0, 'F': 0.0}\ndfall['bin_3'] = fillna_mode(dfall['bin_3']).map(lambda x: tf_map[x])\nyn_map = {'Y': 1.0, 'N': 0.0}\ndfall['bin_4'] = fillna_mode(dfall['bin_4']).map(lambda x: yn_map[x])\n\n# Nominal\nnum_hot_encodes = 4\nhot_encodes = ['nom_{}'.format(i) for i in range(0, num_hot_encodes+1)]\nfor name in hot_encodes: # TODO: reduce cardinality for the other ords\n    dfall[name] = fillna_mode(dfall[name])\ndfall = pd.get_dummies(dfall, columns=hot_encodes, drop_first=True)\ndrop += ['nom_{}'.format(i) for i in range(num_hot_encodes+1, 10)]\n\n# Ordinal\ndfall['ord_0'] = fillna_mode(dfall['ord_0'])\ndfall['ord_1'] = mapord(fillna_mode(dfall['ord_1']), ['Novice', 'Contributor', 'Expert', 'Master', 'Grandmaster'])\ndfall['ord_2'] = mapord(fillna_mode(dfall['ord_2']), ['Freezing', 'Cold', 'Warm', 'Hot', 'Boiling Hot', 'Lava Hot'])\ndfall['ord_3'] = mapletter(fillna_mode(dfall['ord_3']))\ndfall['ord_4'] = mapletter(fillna_mode(dfall['ord_4']))\ndfall['ord_5'] = map2letters(fillna_mode(dfall['ord_5']))\n\n# Day: days 1, 2, 6, 7 are similar; 3, 4, 5 too. Month: as is\ndfall['day_extreme'] = dfall['day'].isin([1.0, 2.0, 6.0, 7.0]).astype(float)\n\n# Month\ndrop.append('day')\n\n# Drop\ndfall.drop(columns=drop, inplace=True)\n\n# Setup for modelling\nX = dfall.loc[y.index, :].copy()\nX_target = dfall.loc[dftest.index, :]\nX_train, X_test, y_train, y_test = split(X, y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":false,"_kg_hide-output":false,"collapsed":true},"cell_type":"code","source":"fig, ax = plt.subplots(3,2, figsize=(17, 20))\nfor i in range(0, 5):\n    proportion_plot(X.assign(target=y), 'ord_{}'.format(i), 'target', ax=ax.flatten()[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X.assign(target=y).groupby('ord_5')['target'].value_counts(normalize=True)\\\n    .rename('count')\\\n    .reset_index()\\\n    .pivot(index='ord_5', columns='target', values='count')[1].plot.line()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"corr = X.assign(target=y).corr()\nidx = corr['target'].abs().sort_values(ascending=False).index\nsns.heatmap(corr.loc[idx, idx], cmap=plt.cm.BrBG)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = xgboost.XGBClassifier(random_state=SEED, objective='binary:logistic')\nmodel.fit(X_train, y_train)\nmodel.score(X_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"probs = model.predict_proba(X_test)\nroc_auc_score(y_test, probs[:, 1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X, y)\npred = model.predict_proba(X_target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfout = pd.DataFrame({'id': X_target.index, 'target': pred[:, 1]})\ndfout.to_csv('awesome.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}