{"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":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-06-15T07:01:53.854057Z","iopub.execute_input":"2024-06-15T07:01:53.854601Z","iopub.status.idle":"2024-06-15T07:01:53.867577Z","shell.execute_reply.started":"2024-06-15T07:01:53.85456Z","shell.execute_reply":"2024-06-15T07:01:53.865816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 코드 탭에서 \"new notebook\"으로 문제 풀기 (생성된 csv파일 다운로드)\n# 예측한 csv파일 \"Submit Predictions\"하기\n# [분류] 신용카드 사기 거래를 탐지\n# 예측할 값(y): Class (0:Not fraud, 1:fraud)\n# 평가: roc-auc 평가지표\n# 제출 형식 (fraud의 확률값)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:02:16.154024Z","iopub.execute_input":"2024-06-15T07:02:16.154546Z","iopub.status.idle":"2024-06-15T07:02:16.160685Z","shell.execute_reply.started":"2024-06-15T07:02:16.154501Z","shell.execute_reply":"2024-06-15T07:02:16.159117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 데이터 import\nimport pandas as pd\n\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')\nsample = pd.read_csv('/kaggle/input/big-data-analytics-certification-kr-2024-2/sample_submission.csv')\n\npd.set_option('display.width',200)\npd.set_option('display.max_columns', None)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:02:17.626473Z","iopub.execute_input":"2024-06-15T07:02:17.627867Z","iopub.status.idle":"2024-06-15T07:02:21.406235Z","shell.execute_reply.started":"2024-06-15T07:02:17.627816Z","shell.execute_reply":"2024-06-15T07:02:21.404818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.combine import SMOTEENN\n\ntrain_y = train.pop('Class')\n\n# SMOTE + Tomek Links 조합 적용\nsmote_enn = SMOTEENN(random_state=42)\nX_resampled, y_resampled = smote_enn.fit_resample(train, train_y)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:01:53.88977Z","iopub.status.idle":"2024-06-15T07:01:53.890256Z","shell.execute_reply.started":"2024-06-15T07:01:53.890035Z","shell.execute_reply":"2024-06-15T07:01:53.890054Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 데이터 확인\n# print(train.info())\n# print(test.info())\nprint(train.describe())","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:02:22.682168Z","iopub.execute_input":"2024-06-15T07:02:22.682661Z","iopub.status.idle":"2024-06-15T07:02:23.029969Z","shell.execute_reply.started":"2024-06-15T07:02:22.682626Z","shell.execute_reply":"2024-06-15T07:02:23.028333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# corr_idx = (train.corr()['Class'].abs().sort_values(ascending=False) > 0.1).index","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:01:53.898608Z","iopub.status.idle":"2024-06-15T07:01:53.899354Z","shell.execute_reply.started":"2024-06-15T07:01:53.899026Z","shell.execute_reply":"2024-06-15T07:01:53.899051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train = train[corr_idx]\n# train","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:01:53.904644Z","iopub.execute_input":"2024-06-15T07:01:53.905158Z","iopub.status.idle":"2024-06-15T07:01:53.918841Z","shell.execute_reply.started":"2024-06-15T07:01:53.90512Z","shell.execute_reply":"2024-06-15T07:01:53.916865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 트테분리\nfrom sklearn.model_selection import train_test_split\ntrain = train.drop('id',axis =1)\ntest_id = test.pop('id')\ntrain_y = train.pop('Class')\nX_tr, X_val, y_tr, y_val = train_test_split(train, train_y, test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:02:25.474308Z","iopub.execute_input":"2024-06-15T07:02:25.47597Z","iopub.status.idle":"2024-06-15T07:02:25.590322Z","shell.execute_reply.started":"2024-06-15T07:02:25.475902Z","shell.execute_reply":"2024-06-15T07:02:25.588781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_tr.shape, X_val.shape, y_tr.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:02:28.664649Z","iopub.execute_input":"2024-06-15T07:02:28.665142Z","iopub.status.idle":"2024-06-15T07:02:28.67495Z","shell.execute_reply.started":"2024-06-15T07:02:28.665106Z","shell.execute_reply":"2024-06-15T07:02:28.672956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델생성\nfrom sklearn.ensemble import RandomForestClassifier\nfrom lightgbm import LGBMClassifier\n\nmodel = RandomForestClassifier(random_state=0)\nmodel.fit(X_tr, y_tr)\npred = model.predict(X_val)\n\nmodel2 = LGBMClassifier(random_state=0)\nmodel2.fit(X_tr, y_tr)\npred2 = model2.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:02:38.631998Z","iopub.execute_input":"2024-06-15T07:02:38.632562Z","iopub.status.idle":"2024-06-15T07:05:28.33758Z","shell.execute_reply.started":"2024-06-15T07:02:38.632525Z","shell.execute_reply":"2024-06-15T07:05:28.335884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 평가\nfrom sklearn.metrics import roc_auc_score\n\nprint(roc_auc_score(y_val, pred))\nprint(roc_auc_score(y_val, pred2))","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:05:42.35378Z","iopub.execute_input":"2024-06-15T07:05:42.354276Z","iopub.status.idle":"2024-06-15T07:05:42.379551Z","shell.execute_reply.started":"2024-06-15T07:05:42.35424Z","shell.execute_reply":"2024-06-15T07:05:42.377934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred3 = model.predict(test)\nresult = pd.DataFrame({\n    'id' : test_id,\n    'Class': pred3\n})\n\nresult.to_csv(\"submission.csv\", index=False)\n\ntmp = pd.read_csv('submission.csv')\ntmp","metadata":{"execution":{"iopub.status.busy":"2024-06-15T07:05:56.485148Z","iopub.execute_input":"2024-06-15T07:05:56.485736Z","iopub.status.idle":"2024-06-15T07:05:58.09018Z","shell.execute_reply.started":"2024-06-15T07:05:56.485686Z","shell.execute_reply":"2024-06-15T07:05:58.088861Z"},"trusted":true},"execution_count":null,"outputs":[]}]}