{"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":"markdown","source":"## 1. EDA","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-06T01:45:14.855230Z","iopub.execute_input":"2022-08-06T01:45:14.855614Z","iopub.status.idle":"2022-08-06T01:45:14.883416Z","shell.execute_reply.started":"2022-08-06T01:45:14.855538Z","shell.execute_reply":"2022-08-06T01:45:14.882709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/porto-seguro-safe-driver-prediction/train.csv', index_col = 'id')\ntest = pd.read_csv('../input/porto-seguro-safe-driver-prediction/test.csv', index_col = 'id')\nsubmission = pd.read_csv('../input/porto-seguro-safe-driver-prediction/sample_submission.csv', index_col = 'id')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:14.884859Z","iopub.execute_input":"2022-08-06T01:45:14.885767Z","iopub.status.idle":"2022-08-06T01:45:23.506122Z","shell.execute_reply.started":"2022-08-06T01:45:14.885733Z","shell.execute_reply":"2022-08-06T01:45:23.505036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:23.507399Z","iopub.execute_input":"2022-08-06T01:45:23.508337Z","iopub.status.idle":"2022-08-06T01:45:23.518432Z","shell.execute_reply.started":"2022-08-06T01:45:23.508298Z","shell.execute_reply":"2022-08-06T01:45:23.517351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:23.520812Z","iopub.execute_input":"2022-08-06T01:45:23.521968Z","iopub.status.idle":"2022-08-06T01:45:23.552926Z","shell.execute_reply.started":"2022-08-06T01:45:23.521924Z","shell.execute_reply":"2022-08-06T01:45:23.551927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:23.554015Z","iopub.execute_input":"2022-08-06T01:45:23.554405Z","iopub.status.idle":"2022-08-06T01:45:23.570865Z","shell.execute_reply.started":"2022-08-06T01:45:23.554382Z","shell.execute_reply":"2022-08-06T01:45:23.569380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:23.572558Z","iopub.execute_input":"2022-08-06T01:45:23.573427Z","iopub.status.idle":"2022-08-06T01:45:23.590625Z","shell.execute_reply.started":"2022-08-06T01:45:23.573395Z","shell.execute_reply":"2022-08-06T01:45:23.589780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import missingno as msno\ntrain_copy = train.copy().replace(-1, np.NaN)\nmsno.bar(df = train_copy.iloc[:, 1:29], figsize = (13,6))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:23.592046Z","iopub.execute_input":"2022-08-06T01:45:23.592384Z","iopub.status.idle":"2022-08-06T01:45:26.130084Z","shell.execute_reply.started":"2022-08-06T01:45:23.592354Z","shell.execute_reply":"2022-08-06T01:45:26.128001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"msno.bar(df = train_copy.iloc[:, 29:], figsize = (13,6))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:26.132350Z","iopub.execute_input":"2022-08-06T01:45:26.132808Z","iopub.status.idle":"2022-08-06T01:45:27.968140Z","shell.execute_reply.started":"2022-08-06T01:45:26.132774Z","shell.execute_reply":"2022-08-06T01:45:27.966892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resumetable(df):\n    print (f'데이터셋 형상: {df.shape}')\n    summary = pd.DataFrame(df.dtypes, columns = ['데이터 타입'])\n    summary['결측값 개수'] = (df == -1).sum().values\n    summary['고윳값 개수'] = df.nunique().values\n    summary['데이터 종류'] = None\n    for col in df.columns:\n        if 'bin' in col or col == 'target':\n            summary.loc[col, '데이터 종류'] = '이진형'\n        elif 'cat' in col:\n            summary.loc[col, '데이터 종류'] = '명목형'\n        elif df[col].dtype == float:\n            summary.loc[col, '데이터 종류'] = '연속형'\n        elif df[col].dtype == int:\n            summary.loc[col, '데이터 종류'] = '순서형'\n            \n    return summary","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:27.971389Z","iopub.execute_input":"2022-08-06T01:45:27.971758Z","iopub.status.idle":"2022-08-06T01:45:27.980141Z","shell.execute_reply.started":"2022-08-06T01:45:27.971727Z","shell.execute_reply":"2022-08-06T01:45:27.979154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary = resumetable(train)\nsummary","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:27.981391Z","iopub.execute_input":"2022-08-06T01:45:27.981786Z","iopub.status.idle":"2022-08-06T01:45:28.221129Z","shell.execute_reply.started":"2022-08-06T01:45:27.981749Z","shell.execute_reply":"2022-08-06T01:45:28.219978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary[summary['데이터 종류'] == '명목형'].index","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:28.221986Z","iopub.execute_input":"2022-08-06T01:45:28.222232Z","iopub.status.idle":"2022-08-06T01:45:28.230975Z","shell.execute_reply.started":"2022-08-06T01:45:28.222209Z","shell.execute_reply":"2022-08-06T01:45:28.229790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary[summary['데이터 타입'] == 'float64'].index","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:28.232478Z","iopub.execute_input":"2022-08-06T01:45:28.232968Z","iopub.status.idle":"2022-08-06T01:45:28.243142Z","shell.execute_reply.started":"2022-08-06T01:45:28.232935Z","shell.execute_reply":"2022-08-06T01:45:28.242058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:28.244682Z","iopub.execute_input":"2022-08-06T01:45:28.245236Z","iopub.status.idle":"2022-08-06T01:45:28.253293Z","shell.execute_reply.started":"2022-08-06T01:45:28.245183Z","shell.execute_reply":"2022-08-06T01:45:28.252412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def write_percent(ax, total_size):\n    for patch in ax.patches:\n        height = patch.get_height()\n        width = patch.get_width()\n        left_coord = patch.get_x()\n        percent = height / total_size * 100\n        \n        ax.text(left_coord + width / 2.0,\n                height + total_size * 0.001,\n                '{:1.1f}%'.format(percent),\n                ha = 'center')\n        \nmpl.rc('font', size = 15)\nplt.figure(figsize = (7,6))\n\nax = sns.countplot(x = 'target', data = train)\nwrite_percent(ax, len(train))\nax.set_title('Target Distribution')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:28.254503Z","iopub.execute_input":"2022-08-06T01:45:28.254881Z","iopub.status.idle":"2022-08-06T01:45:28.452412Z","shell.execute_reply.started":"2022-08-06T01:45:28.254848Z","shell.execute_reply":"2022-08-06T01:45:28.451691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.gridspec as gridspec\ndef plot_target_ratio_by_features(df, features, num_rows, num_cols, size = (12,18)):\n    mpl.rc('font', size = 9)\n    plt.figure(figsize = size)\n    grid = gridspec.GridSpec(num_rows, num_cols)\n    plt.subplots_adjust(wspace = 0.3, hspace = 0.3)\n    \n    for idx, feature in enumerate(features):\n        ax = plt.subplot(grid[idx])\n        sns.barplot(x = feature, y = 'target', data = df, palette = 'Set2', ax= ax)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:28.453591Z","iopub.execute_input":"2022-08-06T01:45:28.454016Z","iopub.status.idle":"2022-08-06T01:45:28.460158Z","shell.execute_reply.started":"2022-08-06T01:45:28.453989Z","shell.execute_reply":"2022-08-06T01:45:28.459421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bin_features = summary[summary['데이터 종류'] == '이진형'].index\nplot_target_ratio_by_features(train, bin_features, 6,3)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:45:28.461325Z","iopub.execute_input":"2022-08-06T01:45:28.461764Z","iopub.status.idle":"2022-08-06T01:48:23.166935Z","shell.execute_reply.started":"2022-08-06T01:45:28.461735Z","shell.execute_reply":"2022-08-06T01:48:23.166145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nom_features = summary[summary['데이터 종류'] == '명목형'].index\nplot_target_ratio_by_features(train, nom_features, 7,2)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:48:23.168499Z","iopub.execute_input":"2022-08-06T01:48:23.168825Z","iopub.status.idle":"2022-08-06T01:50:31.245153Z","shell.execute_reply.started":"2022-08-06T01:48:23.168798Z","shell.execute_reply":"2022-08-06T01:50:31.243939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ord_features = summary[summary['데이터 종류'] == '순서형'].index\nplot_target_ratio_by_features(train, ord_features, 8,2, (12,20))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:50:31.246511Z","iopub.execute_input":"2022-08-06T01:50:31.247524Z","iopub.status.idle":"2022-08-06T01:52:32.536191Z","shell.execute_reply.started":"2022-08-06T01:50:31.247479Z","shell.execute_reply":"2022-08-06T01:52:32.534986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cont_features = summary[summary['데이터 종류'] == '연속형'].index\nplt.figure(figsize = (12,16))\ngrid = gridspec.GridSpec(5,2)\nplt.subplots_adjust(wspace = 0.2, hspace = 0.4)\n\nfor idx, cont_feature in enumerate(cont_features):\n    train[cont_feature] = pd.cut(train[cont_feature], 5)\n    \n    ax = plt.subplot(grid[idx])\n    sns.barplot(x = cont_feature, y = 'target', data = train, palette = 'Set2', ax = ax)\n    ax.tick_params(axis = 'x', labelrotation = 10)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:52:32.537757Z","iopub.execute_input":"2022-08-06T01:52:32.539342Z","iopub.status.idle":"2022-08-06T01:53:53.685182Z","shell.execute_reply.started":"2022-08-06T01:52:32.539280Z","shell.execute_reply":"2022-08-06T01:53:53.684210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_copy = train_copy.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:53:53.686720Z","iopub.execute_input":"2022-08-06T01:53:53.687290Z","iopub.status.idle":"2022-08-06T01:53:53.881323Z","shell.execute_reply.started":"2022-08-06T01:53:53.687253Z","shell.execute_reply":"2022-08-06T01:53:53.880067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,8))\ncont_corr = train_copy[cont_features].corr()\nsns.heatmap(cont_corr, annot = True, cmap = 'OrRd')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T01:53:53.882745Z","iopub.execute_input":"2022-08-06T01:53:53.883072Z","iopub.status.idle":"2022-08-06T01:53:54.458491Z","shell.execute_reply.started":"2022-08-06T01:53:53.883041Z","shell.execute_reply":"2022-08-06T01:53:54.457120Z"},"trusted":true},"execution_count":null,"outputs":[]}]}