{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":80687,"databundleVersionId":8672016,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"- 이 코드는 완벽하지 않아요\n- roc-auc 평가지표일 때 predict과 predict_proba를 비교해보세요\n- 검증데이터를 사용해보세요!","metadata":{}},{"cell_type":"code","source":"import pandas as pd\ntrain = pd.read_csv(\"/kaggle/input/big-data-analytics-certification-kr-2024-2/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/big-data-analytics-certification-kr-2024-2/test.csv\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-19T13:36:11.864052Z","iopub.execute_input":"2024-06-19T13:36:11.864492Z","iopub.status.idle":"2024-06-19T13:36:19.985806Z","shell.execute_reply.started":"2024-06-19T13:36:11.864457Z","shell.execute_reply":"2024-06-19T13:36:19.984357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# EDA\n# 데이터 크기 확인\nprint(train.shape)\nprint(test.shape)\n\n# 샘플확인\nprint(train.head())\nprint(test.head())\n\n# target 확인\nprint(train['Class'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:37:49.678635Z","iopub.execute_input":"2024-06-19T13:37:49.679063Z","iopub.status.idle":"2024-06-19T13:37:49.723789Z","shell.execute_reply.started":"2024-06-19T13:37:49.679030Z","shell.execute_reply":"2024-06-19T13:37:49.721919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 결측치 확인\nprint(train.isnull().sum())\nprint(test.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:38:28.399273Z","iopub.execute_input":"2024-06-19T13:38:28.400897Z","iopub.status.idle":"2024-06-19T13:38:28.433497Z","shell.execute_reply.started":"2024-06-19T13:38:28.400821Z","shell.execute_reply":"2024-06-19T13:38:28.431306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 타입확인\nprint(train.info())\nprint(test.info())","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:38:52.124463Z","iopub.execute_input":"2024-06-19T13:38:52.125005Z","iopub.status.idle":"2024-06-19T13:38:52.177248Z","shell.execute_reply.started":"2024-06-19T13:38:52.124961Z","shell.execute_reply":"2024-06-19T13:38:52.175481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 통계량 확인\nprint(train.describe())\nprint(test.describe())","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:42:15.264268Z","iopub.execute_input":"2024-06-19T13:42:15.264896Z","iopub.status.idle":"2024-06-19T13:42:15.960655Z","shell.execute_reply.started":"2024-06-19T13:42:15.264848Z","shell.execute_reply":"2024-06-19T13:42:15.958879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 데이터 전처리\n# target 값 복사\ntarget = train.pop('Class')","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:43:40.604086Z","iopub.execute_input":"2024-06-19T13:43:40.604543Z","iopub.status.idle":"2024-06-19T13:43:40.611568Z","shell.execute_reply.started":"2024-06-19T13:43:40.604506Z","shell.execute_reply":"2024-06-19T13:43:40.609996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test 데이터 id 복사\ntest_id = test.pop('id')\n\n# train에서 id 삭제\ntrain = train.drop('id', axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:44:43.104676Z","iopub.execute_input":"2024-06-19T13:44:43.105194Z","iopub.status.idle":"2024-06-19T13:44:43.137167Z","shell.execute_reply.started":"2024-06-19T13:44:43.105153Z","shell.execute_reply":"2024-06-19T13:44:43.135620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 확인\nprint(train.info())\nprint(test.info())","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:45:18.603799Z","iopub.execute_input":"2024-06-19T13:45:18.604249Z","iopub.status.idle":"2024-06-19T13:45:18.640932Z","shell.execute_reply.started":"2024-06-19T13:45:18.604214Z","shell.execute_reply":"2024-06-19T13:45:18.639664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 인코딩\nfrom sklearn.preprocessing import RobustScaler\nscaler = RobustScaler()\n\ncolumns = train.columns\nfor c in columns:\n    train[c] = scaler.fit_transform(train[[c]])\n    test[c] = scaler.transform(test[[c]])\n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:49:29.943672Z","iopub.execute_input":"2024-06-19T13:49:29.944795Z","iopub.status.idle":"2024-06-19T13:49:30.420996Z","shell.execute_reply.started":"2024-06-19T13:49:29.944751Z","shell.execute_reply":"2024-06-19T13:49:30.419778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 확인\nprint(train.describe())\nprint(test.describe())","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:50:04.251641Z","iopub.execute_input":"2024-06-19T13:50:04.252852Z","iopub.status.idle":"2024-06-19T13:50:04.859674Z","shell.execute_reply.started":"2024-06-19T13:50:04.252796Z","shell.execute_reply":"2024-06-19T13:50:04.858463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 검증 데이터 분리\nfrom sklearn.model_selection import train_test_split\nx_tr, x_val, y_tr, y_val = train_test_split(train,\n                                           target,\n                                           test_size=0.2,\n                                           random_state=2024)\nprint(x_tr.shape, x_val.shape, y_tr.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:52:23.778290Z","iopub.execute_input":"2024-06-19T13:52:23.779052Z","iopub.status.idle":"2024-06-19T13:52:23.915187Z","shell.execute_reply.started":"2024-06-19T13:52:23.779002Z","shell.execute_reply":"2024-06-19T13:52:23.913047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델 선택 (예측을 predict로 했을 때)\nfrom sklearn.ensemble import RandomForestClassifier\nmodel1 = RandomForestClassifier()\nmodel1.fit(x_tr, y_tr)\npred = model1.predict(x_val)\n\n# 평가지표\nfrom sklearn.metrics import roc_auc_score\nroc_auc_score(y_val, pred)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T13:57:42.333833Z","iopub.execute_input":"2024-06-19T13:57:42.334264Z","iopub.status.idle":"2024-06-19T13:59:52.687753Z","shell.execute_reply.started":"2024-06-19T13:57:42.334230Z","shell.execute_reply":"2024-06-19T13:59:52.686588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 테스트 데이터에 대한 예측\ny_pred = model1.predict(test)\ny_pred = pd.DataFrame(y_pred)\nprint(y_pred.info())","metadata":{"execution":{"iopub.status.busy":"2024-06-19T14:04:06.673952Z","iopub.execute_input":"2024-06-19T14:04:06.674363Z","iopub.status.idle":"2024-06-19T14:04:07.724504Z","shell.execute_reply.started":"2024-06-19T14:04:06.674329Z","shell.execute_reply":"2024-06-19T14:04:07.723257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델 선택 (예측을 predict_proba로 했을 때)\nfrom sklearn.ensemble import RandomForestClassifier\nmodel1 = RandomForestClassifier()\nmodel1.fit(x_tr, y_tr)\npred = model1.predict_proba(x_val)[:,1]\n\n# 평가지표\nfrom sklearn.metrics import roc_auc_score\nroc_auc_score(y_val, pred)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T14:10:24.166092Z","iopub.execute_input":"2024-06-19T14:10:24.166951Z","iopub.status.idle":"2024-06-19T14:12:35.334837Z","shell.execute_reply.started":"2024-06-19T14:10:24.166919Z","shell.execute_reply":"2024-06-19T14:12:35.333626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 테스트 데이터에 대한 예측\ny_pred = model1.predict_proba(test)\ny_pred = pd.DataFrame(y_pred)\n\nfor i in range(len(y_pred)):\n    if (y_pred.iloc[i,0] > y_pred.iloc[i,1]):\n        y_pred['Class'] = 0\n    else:\n        y_pred['Class'] = 1\n\nprint(y_pred)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T14:18:30.718470Z","iopub.execute_input":"2024-06-19T14:18:30.719447Z","iopub.status.idle":"2024-06-19T14:18:53.985092Z","shell.execute_reply.started":"2024-06-19T14:18:30.719413Z","shell.execute_reply":"2024-06-19T14:18:53.984026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = y_pred['Class']","metadata":{"execution":{"iopub.status.busy":"2024-06-19T14:21:33.586425Z","iopub.execute_input":"2024-06-19T14:21:33.586848Z","iopub.status.idle":"2024-06-19T14:21:33.592439Z","shell.execute_reply.started":"2024-06-19T14:21:33.586819Z","shell.execute_reply":"2024-06-19T14:21:33.591069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 제출 파일 생성\nsubmission = pd.DataFrame({'id': test_id, 'Class': result})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T14:21:44.765896Z","iopub.execute_input":"2024-06-19T14:21:44.766800Z","iopub.status.idle":"2024-06-19T14:21:44.928637Z","shell.execute_reply.started":"2024-06-19T14:21:44.766760Z","shell.execute_reply":"2024-06-19T14:21:44.927397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}