{"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":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:26:35.750978Z","iopub.execute_input":"2022-12-30T04:26:35.751346Z","iopub.status.idle":"2022-12-30T04:26:35.757514Z","shell.execute_reply.started":"2022-12-30T04:26:35.751317Z","shell.execute_reply":"2022-12-30T04:26:35.756165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/ai511-homeloan-2022/train_data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:26:41.226119Z","iopub.execute_input":"2022-12-30T04:26:41.226609Z","iopub.status.idle":"2022-12-30T04:26:43.868936Z","shell.execute_reply.started":"2022-12-30T04:26:41.226567Z","shell.execute_reply":"2022-12-30T04:26:43.867746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:26:45.553036Z","iopub.execute_input":"2022-12-30T04:26:45.553543Z","iopub.status.idle":"2022-12-30T04:26:45.592618Z","shell.execute_reply.started":"2022-12-30T04:26:45.553505Z","shell.execute_reply":"2022-12-30T04:26:45.590625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 清理與準備訓練資料\n# 區分x和y\ndroplist = ['SK_ID_CURR','FLAG_DOCUMENT_2','FLAG_DOCUMENT_3','FLAG_DOCUMENT_4','FLAG_DOCUMENT_5',\n            'FLAG_DOCUMENT_6','FLAG_DOCUMENT_7','FLAG_DOCUMENT_8','FLAG_DOCUMENT_9',\n            'FLAG_DOCUMENT_10','FLAG_DOCUMENT_11','FLAG_DOCUMENT_12','FLAG_DOCUMENT_13',\n            'FLAG_DOCUMENT_14','FLAG_DOCUMENT_15','FLAG_DOCUMENT_16','FLAG_DOCUMENT_17',\n            'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19','FLAG_DOCUMENT_20','FLAG_DOCUMENT_21','TARGET']\nx = data.drop(droplist,axis=1) # feature\ny = data['TARGET'].copy() # y","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:28:22.250777Z","iopub.execute_input":"2022-12-30T04:28:22.251253Z","iopub.status.idle":"2022-12-30T04:28:22.333268Z","shell.execute_reply.started":"2022-12-30T04:28:22.251219Z","shell.execute_reply":"2022-12-30T04:28:22.331661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 數值變數處理\ncat_col = ['NAME_CONTRACT_TYPE','CODE_GENDER','FLAG_OWN_CAR','FLAG_OWN_REALTY',\n             'NAME_TYPE_SUITE','NAME_INCOME_TYPE','NAME_EDUCATION_TYPE','NAME_FAMILY_STATUS',\n             'NAME_HOUSING_TYPE','OCCUPATION_TYPE','WEEKDAY_APPR_PROCESS_START','ORGANIZATION_TYPE',\n             'FONDKAPREMONT_MODE','HOUSETYPE_MODE','WALLSMATERIAL_MODE','EMERGENCYSTATE_MODE']\nx_num = x.drop(cat_col,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:28:24.557459Z","iopub.execute_input":"2022-12-30T04:28:24.557958Z","iopub.status.idle":"2022-12-30T04:28:24.629291Z","shell.execute_reply.started":"2022-12-30T04:28:24.557914Z","shell.execute_reply":"2022-12-30T04:28:24.627802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 準備訓練資料\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.preprocessing import OneHotEncoder\n\npip_num = Pipeline([\n    ('imputer',SimpleImputer(strategy='median')),\n    ('std_scaler',StandardScaler())\n])\n\npip_full = ColumnTransformer([\n    ('num',pip_num,list(x_num)[0:]),\n    ('cat',OneHotEncoder(),cat_col)\n])","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:28:26.385278Z","iopub.execute_input":"2022-12-30T04:28:26.385809Z","iopub.status.idle":"2022-12-30T04:28:26.394599Z","shell.execute_reply.started":"2022-12-30T04:28:26.385771Z","shell.execute_reply":"2022-12-30T04:28:26.393363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_prep = pip_full.fit_transform(x)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T03:36:24.004800Z","iopub.execute_input":"2022-12-30T03:36:24.005279Z","iopub.status.idle":"2022-12-30T03:36:28.655504Z","shell.execute_reply.started":"2022-12-30T03:36:24.005246Z","shell.execute_reply":"2022-12-30T03:36:28.653919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 建立訓練集/測試集\nfrom sklearn.model_selection import train_test_split\n\nx_train_prep, x_test_prep = train_test_split(x_prep, test_size = 0.2, random_state = 1)\ny_train, y_test = train_test_split(y, test_size = 0.2, random_state = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:33:51.599402Z","iopub.execute_input":"2022-12-30T04:33:51.599853Z","iopub.status.idle":"2022-12-30T04:33:51.834038Z","shell.execute_reply.started":"2022-12-30T04:33:51.599815Z","shell.execute_reply":"2022-12-30T04:33:51.832761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_prep.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:33:55.669137Z","iopub.execute_input":"2022-12-30T04:33:55.670336Z","iopub.status.idle":"2022-12-30T04:33:55.677581Z","shell.execute_reply.started":"2022-12-30T04:33:55.670280Z","shell.execute_reply":"2022-12-30T04:33:55.676459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_prep.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:33:58.562285Z","iopub.execute_input":"2022-12-30T04:33:58.562818Z","iopub.status.idle":"2022-12-30T04:33:58.570983Z","shell.execute_reply.started":"2022-12-30T04:33:58.562777Z","shell.execute_reply":"2022-12-30T04:33:58.569749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RandomForestClassifier\nfrom sklearn.ensemble import RandomForestClassifier\n\nrnd_clf = RandomForestClassifier(n_estimators=500, max_leaf_nodes=16, class_weight=\"balanced\", oob_score=True, random_state=1)\nrnd_clf.fit(x_train_prep,y_train)\nrnd_clf.score(x_train_prep,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:35:00.801439Z","iopub.execute_input":"2022-12-30T04:35:00.801994Z","iopub.status.idle":"2022-12-30T04:37:37.832949Z","shell.execute_reply.started":"2022-12-30T04:35:00.801951Z","shell.execute_reply":"2022-12-30T04:37:37.831230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rnd_clf.oob_score_","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:38:33.161319Z","iopub.execute_input":"2022-12-30T04:38:33.163047Z","iopub.status.idle":"2022-12-30T04:38:33.170930Z","shell.execute_reply.started":"2022-12-30T04:38:33.162961Z","shell.execute_reply":"2022-12-30T04:38:33.169702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rnd_clf.score(x_test_prep, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:38:36.532172Z","iopub.execute_input":"2022-12-30T04:38:36.532686Z","iopub.status.idle":"2022-12-30T04:38:38.723029Z","shell.execute_reply.started":"2022-12-30T04:38:36.532633Z","shell.execute_reply":"2022-12-30T04:38:38.721377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#7.驗證與解釋模型\n#7.1驗證模型\ndata_test = pd.read_csv('/kaggle/input/ai511-homeloan-2022/test_data.csv')\ndroplist2 = ['SK_ID_CURR','FLAG_DOCUMENT_2','FLAG_DOCUMENT_3','FLAG_DOCUMENT_4','FLAG_DOCUMENT_5',\n            'FLAG_DOCUMENT_6','FLAG_DOCUMENT_7','FLAG_DOCUMENT_8','FLAG_DOCUMENT_9',\n            'FLAG_DOCUMENT_10','FLAG_DOCUMENT_11','FLAG_DOCUMENT_12','FLAG_DOCUMENT_13',\n            'FLAG_DOCUMENT_14','FLAG_DOCUMENT_15','FLAG_DOCUMENT_16','FLAG_DOCUMENT_17',\n            'FLAG_DOCUMENT_18','FLAG_DOCUMENT_19','FLAG_DOCUMENT_20','FLAG_DOCUMENT_21']\nx_test = data_test.drop(droplist2,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:39:44.609245Z","iopub.execute_input":"2022-12-30T04:39:44.610989Z","iopub.status.idle":"2022-12-30T04:39:46.144072Z","shell.execute_reply.started":"2022-12-30T04:39:44.610917Z","shell.execute_reply":"2022-12-30T04:39:46.142380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_prep = pip_full.fit_transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:39:49.740507Z","iopub.execute_input":"2022-12-30T04:39:49.740935Z","iopub.status.idle":"2022-12-30T04:39:52.754104Z","shell.execute_reply.started":"2022-12-30T04:39:49.740903Z","shell.execute_reply":"2022-12-30T04:39:52.752998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = rnd_clf.predict (x_test_prep)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:40:47.374328Z","iopub.execute_input":"2022-12-30T04:40:47.374815Z","iopub.status.idle":"2022-12-30T04:40:54.448788Z","shell.execute_reply.started":"2022-12-30T04:40:47.374779Z","shell.execute_reply":"2022-12-30T04:40:54.447302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nsubmission = pd.DataFrame()\nId = pd.read_csv('/kaggle/input/ai511-homeloan-2022/sample_solutions.csv')\nsubmission['SK_ID_CURR'] = Id.SK_ID_CURR\nsubmission['TARGET'] = prediction\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:40:56.966528Z","iopub.execute_input":"2022-12-30T04:40:56.967009Z","iopub.status.idle":"2022-12-30T04:40:57.010493Z","shell.execute_reply.started":"2022-12-30T04:40:56.966972Z","shell.execute_reply":"2022-12-30T04:40:57.008988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-12-30T04:41:00.029926Z","iopub.execute_input":"2022-12-30T04:41:00.030397Z","iopub.status.idle":"2022-12-30T04:41:00.176589Z","shell.execute_reply.started":"2022-12-30T04:41:00.030362Z","shell.execute_reply":"2022-12-30T04:41:00.175048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}