{"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":"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-05-28T02:10:15.641830Z","iopub.execute_input":"2024-05-28T02:10:15.642242Z","iopub.status.idle":"2024-05-28T02:10:18.086861Z","shell.execute_reply.started":"2024-05-28T02:10:15.642209Z","shell.execute_reply":"2024-05-28T02:10:18.085715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# [분류] 신용카드 사기 거래를 탐지\n예측할 값(y): Class (0:Not fraud, 1:fraud)\n\n평가: roc-auc 평가지표\n\n제출 형식 (fraud의 확률값)\n\nid,Class \n0,0.3\n1,0.48\n2,0.3\n3,0.83","metadata":{}},{"cell_type":"code","source":"#EDA\nprint(train.shape, test.shape)\ntrain.head(50)\ntest.head()\ntrain.isnull().sum()\ntest.isnull().sum()\ntrain.info()\ntrain.describe()\n\n# 전처리\n\n# from sklearn.preprocessing import StandardScaler\n# ss = StandardScaler()\n# train['Amount'] = ss.fit_transform(train['Amount'].values.reshape(-1,1))\n# test['Amount'] = ss.transform(test['Amount'].values.reshape(-1,1))\n\ny = train.pop('Class')\ntrain = train.drop('id', axis=1)\ntest_id = test.pop('id')\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T02:10:19.302887Z","iopub.execute_input":"2024-05-28T02:10:19.303278Z","iopub.status.idle":"2024-05-28T02:10:19.621486Z","shell.execute_reply.started":"2024-05-28T02:10:19.303245Z","shell.execute_reply":"2024-05-28T02:10:19.620004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Time만 빼고 추출\ncols = train.columns[1:30]","metadata":{"execution":{"iopub.status.busy":"2024-05-28T02:10:24.161267Z","iopub.execute_input":"2024-05-28T02:10:24.161671Z","iopub.status.idle":"2024-05-28T02:10:24.166511Z","shell.execute_reply.started":"2024-05-28T02:10:24.161635Z","shell.execute_reply":"2024-05-28T02:10:24.165452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 훈련, 검증용 분리\nfrom sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(train, y, test_size=0.2, random_state=0, stratify=y)\nprint(X_train.shape, X_val.shape, y_train.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T02:10:26.163018Z","iopub.execute_input":"2024-05-28T02:10:26.163407Z","iopub.status.idle":"2024-05-28T02:10:26.255817Z","shell.execute_reply.started":"2024-05-28T02:10:26.163376Z","shell.execute_reply":"2024-05-28T02:10:26.254557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 랜덤포레스트\nfrom sklearn.ensemble import RandomForestClassifier\nrf = RandomForestClassifier()\nrf.fit(X_train[cols], y_train)\npred = rf.predict(X_val[cols])\npred_proba = rf.predict_proba(X_val[cols])","metadata":{"execution":{"iopub.status.busy":"2024-05-28T02:10:27.937139Z","iopub.execute_input":"2024-05-28T02:10:27.937533Z","iopub.status.idle":"2024-05-28T02:12:43.128515Z","shell.execute_reply.started":"2024-05-28T02:10:27.937496Z","shell.execute_reply":"2024-05-28T02:12:43.127395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 성능평가\nfrom sklearn.metrics import roc_auc_score\nroc_auc_score(y_val, pred_proba[:,1])\n\n# 0.9567815488116762 (amount 포함)\n# 0.9359095277655606 (amount 제외)\n# 0.942679877502484 (amount 스케일링)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T02:12:52.084207Z","iopub.execute_input":"2024-05-28T02:12:52.084650Z","iopub.status.idle":"2024-05-28T02:12:52.101843Z","shell.execute_reply.started":"2024-05-28T02:12:52.084617Z","shell.execute_reply":"2024-05-28T02:12:52.100269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 학습\nresult = rf.predict_proba(test[cols])","metadata":{"execution":{"iopub.status.busy":"2024-05-28T02:13:09.236244Z","iopub.execute_input":"2024-05-28T02:13:09.237094Z","iopub.status.idle":"2024-05-28T02:13:10.333254Z","shell.execute_reply.started":"2024-05-28T02:13:09.237052Z","shell.execute_reply":"2024-05-28T02:13:10.332095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 제출\nsubmit = pd.DataFrame({\n    'id' : test_id,\n    'Class' : result[:,1]\n})\nsubmit.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T02:13:12.605525Z","iopub.execute_input":"2024-05-28T02:13:12.606798Z","iopub.status.idle":"2024-05-28T02:13:12.741432Z","shell.execute_reply.started":"2024-05-28T02:13:12.606758Z","shell.execute_reply":"2024-05-28T02:13:12.740054Z"},"trusted":true},"execution_count":null,"outputs":[]}]}