{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-24T22:22:27.491733Z","iopub.execute_input":"2022-07-24T22:22:27.492555Z","iopub.status.idle":"2022-07-24T22:22:27.527013Z","shell.execute_reply.started":"2022-07-24T22:22:27.492420Z","shell.execute_reply":"2022-07-24T22:22:27.526068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndata_path = '/kaggle/input/cat-in-the-dat/'\n\ntrain = pd.read_csv(data_path + 'train.csv', index_col = 'id')\ntest = pd.read_csv(data_path + 'test.csv', index_col = 'id')\nsubmission = pd.read_csv(data_path + 'sample_submission.csv', index_col = 'id')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:23:52.330588Z","iopub.execute_input":"2022-07-24T22:23:52.331008Z","iopub.status.idle":"2022-07-24T22:23:54.684457Z","shell.execute_reply.started":"2022-07-24T22:23:52.330973Z","shell.execute_reply":"2022-07-24T22:23:54.683558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 7.3.1 피처 엔지니어링","metadata":{}},{"cell_type":"markdown","source":"데이터 합치기","metadata":{}},{"cell_type":"code","source":"all_data = pd.concat([train, test])\nall_data = all_data.drop('target', axis=1)\nall_data","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:25:43.747643Z","iopub.execute_input":"2022-07-24T22:25:43.748400Z","iopub.status.idle":"2022-07-24T22:25:44.768795Z","shell.execute_reply.started":"2022-07-24T22:25:43.748359Z","shell.execute_reply":"2022-07-24T22:25:44.767569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"원-핫 인코딩","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\n\nencoder = OneHotEncoder()\nall_data_encoded = encoder.fit_transform(all_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:28:21.972208Z","iopub.execute_input":"2022-07-24T22:28:21.972968Z","iopub.status.idle":"2022-07-24T22:28:25.774529Z","shell.execute_reply.started":"2022-07-24T22:28:21.972928Z","shell.execute_reply":"2022-07-24T22:28:25.773376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_data_encoded","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:28:29.706464Z","iopub.execute_input":"2022-07-24T22:28:29.707155Z","iopub.status.idle":"2022-07-24T22:28:29.714736Z","shell.execute_reply.started":"2022-07-24T22:28:29.707108Z","shell.execute_reply":"2022-07-24T22:28:29.713684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"데이터 나누기","metadata":{}},{"cell_type":"code","source":"num_train = len(train)\n\nX_train = all_data_encoded[:num_train]\nX_test = all_data_encoded[num_train:]\n\ny = train['target']","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:29:25.828711Z","iopub.execute_input":"2022-07-24T22:29:25.829121Z","iopub.status.idle":"2022-07-24T22:29:26.004915Z","shell.execute_reply.started":"2022-07-24T22:29:25.829087Z","shell.execute_reply":"2022-07-24T22:29:26.003817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_valid, y_train, y_valid = train_test_split(X_train, y, test_size=0.1, stratify=y, random_state=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:30:40.095560Z","iopub.execute_input":"2022-07-24T22:30:40.095936Z","iopub.status.idle":"2022-07-24T22:30:40.352722Z","shell.execute_reply.started":"2022-07-24T22:30:40.095906Z","shell.execute_reply":"2022-07-24T22:30:40.351439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 7.3.2 모델 훈련","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n\nlogistic_model = LogisticRegression(max_iter=1000, random_state=42)\nlogistic_model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:33:34.112355Z","iopub.execute_input":"2022-07-24T22:33:34.112756Z","iopub.status.idle":"2022-07-24T22:34:47.684860Z","shell.execute_reply.started":"2022-07-24T22:33:34.112720Z","shell.execute_reply":"2022-07-24T22:34:47.683643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 7.3.3 모델 성능 검증","metadata":{}},{"cell_type":"code","source":"logistic_model.predict_proba(X_valid)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:48.140104Z","iopub.execute_input":"2022-07-24T22:34:48.140445Z","iopub.status.idle":"2022-07-24T22:34:48.151534Z","shell.execute_reply.started":"2022-07-24T22:34:48.140415Z","shell.execute_reply":"2022-07-24T22:34:48.150394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logistic_model.predict(X_valid)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:34:51.002155Z","iopub.execute_input":"2022-07-24T22:34:51.002820Z","iopub.status.idle":"2022-07-24T22:34:51.012528Z","shell.execute_reply.started":"2022-07-24T22:34:51.002767Z","shell.execute_reply":"2022-07-24T22:34:51.011712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid_preds = logistic_model.predict_proba(X_valid)[:, 1]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:35:30.146289Z","iopub.execute_input":"2022-07-24T22:35:30.146733Z","iopub.status.idle":"2022-07-24T22:35:30.154143Z","shell.execute_reply.started":"2022-07-24T22:35:30.146686Z","shell.execute_reply":"2022-07-24T22:35:30.153080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\nroc_auc = roc_auc_score(y_valid, y_valid_preds)\nprint(f'검증 데이터 ROC AUC : {roc_auc:.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:36:32.051064Z","iopub.execute_input":"2022-07-24T22:36:32.051446Z","iopub.status.idle":"2022-07-24T22:36:32.071147Z","shell.execute_reply.started":"2022-07-24T22:36:32.051417Z","shell.execute_reply":"2022-07-24T22:36:32.069878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 7.3.4 예측 및 결과 제출","metadata":{}},{"cell_type":"code","source":"y_preds = logistic_model.predict_proba(X_test)[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:37:01.019722Z","iopub.execute_input":"2022-07-24T22:37:01.020115Z","iopub.status.idle":"2022-07-24T22:37:01.039466Z","shell.execute_reply.started":"2022-07-24T22:37:01.020084Z","shell.execute_reply":"2022-07-24T22:37:01.038534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:37:08.988822Z","iopub.execute_input":"2022-07-24T22:37:08.989481Z","iopub.status.idle":"2022-07-24T22:37:09.003222Z","shell.execute_reply.started":"2022-07-24T22:37:08.989441Z","shell.execute_reply":"2022-07-24T22:37:09.001902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['target'] = y_preds\nsubmission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-24T22:37:27.019651Z","iopub.execute_input":"2022-07-24T22:37:27.020109Z","iopub.status.idle":"2022-07-24T22:37:27.776468Z","shell.execute_reply.started":"2022-07-24T22:37:27.020073Z","shell.execute_reply":"2022-07-24T22:37:27.775584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}