{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":88400,"databundleVersionId":10121331,"sourceType":"competition"}],"dockerImageVersionId":30786,"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:18.567460Z","iopub.execute_input":"2024-11-12T08:47:18.567934Z","iopub.status.idle":"2024-11-12T08:47:18.577121Z","shell.execute_reply.started":"2024-11-12T08:47:18.567869Z","shell.execute_reply":"2024-11-12T08:47:18.575828Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:18.579711Z","iopub.execute_input":"2024-11-12T08:47:18.580216Z","iopub.status.idle":"2024-11-12T08:47:18.597590Z","shell.execute_reply.started":"2024-11-12T08:47:18.580164Z","shell.execute_reply":"2024-11-12T08:47:18.596126Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 데이터 불러오기","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/train.csv\") \ntest = pd.read_csv(\"/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/test.csv\")\nsubmission = pd.read_csv(\"/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/sample_submission.csv\")\n\npredict_test = test.copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:18.598720Z","iopub.execute_input":"2024-11-12T08:47:18.599099Z","iopub.status.idle":"2024-11-12T08:47:22.473326Z","shell.execute_reply.started":"2024-11-12T08:47:18.599042Z","shell.execute_reply":"2024-11-12T08:47:22.471989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:22.474722Z","iopub.execute_input":"2024-11-12T08:47:22.475323Z","iopub.status.idle":"2024-11-12T08:47:22.512826Z","shell.execute_reply.started":"2024-11-12T08:47:22.475260Z","shell.execute_reply":"2024-11-12T08:47:22.511410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:22.516943Z","iopub.execute_input":"2024-11-12T08:47:22.517407Z","iopub.status.idle":"2024-11-12T08:47:22.551826Z","shell.execute_reply.started":"2024-11-12T08:47:22.517361Z","shell.execute_reply":"2024-11-12T08:47:22.550445Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Cleaning","metadata":{}},{"cell_type":"markdown","source":"## 결측치\n#### -> 따로 보이지 않음","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:22.553515Z","iopub.execute_input":"2024-11-12T08:47:22.554374Z","iopub.status.idle":"2024-11-12T08:47:22.579602Z","shell.execute_reply.started":"2024-11-12T08:47:22.554300Z","shell.execute_reply":"2024-11-12T08:47:22.578288Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 이상치\n#### -> 특별한 이상치는 보이지 않음","metadata":{}},{"cell_type":"code","source":"train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:22.581139Z","iopub.execute_input":"2024-11-12T08:47:22.581635Z","iopub.status.idle":"2024-11-12T08:47:22.913816Z","shell.execute_reply.started":"2024-11-12T08:47:22.581586Z","shell.execute_reply":"2024-11-12T08:47:22.912555Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 변수 간 상관 관계\n#### Target과 변수간 관계가 높지 않음","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (19, 19))\ncorr = train.corr()\nsns.heatmap(corr, annot = True, fmt = '.2f', cmap='coolwarm')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:22.915226Z","iopub.execute_input":"2024-11-12T08:47:22.915560Z","iopub.status.idle":"2024-11-12T08:47:27.203437Z","shell.execute_reply.started":"2024-11-12T08:47:22.915523Z","shell.execute_reply":"2024-11-12T08:47:27.202076Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature Engeering","metadata":{}},{"cell_type":"markdown","source":"## Time\n### 초를 시간대로 변환","metadata":{}},{"cell_type":"code","source":"def second_to_hour(seconds):\n    hours = int(seconds // 3600)\n    if hours == 24:\n        hours = 0\n    elif hours > 24:\n        hours -= 24\n    \n    return hours","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.205391Z","iopub.execute_input":"2024-11-12T08:47:27.205892Z","iopub.status.idle":"2024-11-12T08:47:27.213360Z","shell.execute_reply.started":"2024-11-12T08:47:27.205838Z","shell.execute_reply":"2024-11-12T08:47:27.211929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"time_bin = [0, 6, 12, 18, 23]\nlabel = ['midnight', 'morning', 'afternoon', 'night']\n\ntrain['hour'] = train['Time'].apply(second_to_hour)\npredict_test['hour'] = predict_test['Time'].apply(second_to_hour)\n\ntrain['timezone'] = pd.cut(train['hour'], bins = time_bin, labels = label, right = True, include_lowest = True)\npredict_test['timezone'] = pd.cut(predict_test['hour'], bins = time_bin, labels = label, right = True, include_lowest = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.214956Z","iopub.execute_input":"2024-11-12T08:47:27.215533Z","iopub.status.idle":"2024-11-12T08:47:27.570743Z","shell.execute_reply.started":"2024-11-12T08:47:27.215467Z","shell.execute_reply":"2024-11-12T08:47:27.569489Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = train[train['Class'] == 1]['timezone'].value_counts().reset_index()\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.572244Z","iopub.execute_input":"2024-11-12T08:47:27.572642Z","iopub.status.idle":"2024-11-12T08:47:27.592789Z","shell.execute_reply.started":"2024-11-12T08:47:27.572598Z","shell.execute_reply":"2024-11-12T08:47:27.591347Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 사기 거래는 주로 새벽 시간대에 발생함","metadata":{}},{"cell_type":"code","source":"sns.barplot(data = df, x = df['timezone'], y = df['count'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.594304Z","iopub.execute_input":"2024-11-12T08:47:27.594804Z","iopub.status.idle":"2024-11-12T08:47:27.818640Z","shell.execute_reply.started":"2024-11-12T08:47:27.594757Z","shell.execute_reply":"2024-11-12T08:47:27.817352Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### timezone Column label encoding\n","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nencoder = LabelEncoder()\ntrain['timezone'] = encoder.fit_transform(train['timezone'])\npredict_test['timezone'] = encoder.fit_transform(predict_test['timezone'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.820614Z","iopub.execute_input":"2024-11-12T08:47:27.821150Z","iopub.status.idle":"2024-11-12T08:47:27.907827Z","shell.execute_reply.started":"2024-11-12T08:47:27.821091Z","shell.execute_reply":"2024-11-12T08:47:27.906765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.912410Z","iopub.execute_input":"2024-11-12T08:47:27.912833Z","iopub.status.idle":"2024-11-12T08:47:27.938135Z","shell.execute_reply.started":"2024-11-12T08:47:27.912791Z","shell.execute_reply":"2024-11-12T08:47:27.936832Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Amount Scaling","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\ntrain['Amount'] = scaler.fit_transform(train[['Amount']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.939597Z","iopub.execute_input":"2024-11-12T08:47:27.940114Z","iopub.status.idle":"2024-11-12T08:47:27.954546Z","shell.execute_reply.started":"2024-11-12T08:47:27.940032Z","shell.execute_reply":"2024-11-12T08:47:27.953399Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Drop","metadata":{}},{"cell_type":"code","source":"train.drop(['id', 'Time', 'hour'], axis = 1, inplace = True)\npredict_test.drop(['id', 'Time', 'hour'], axis = 1, inplace = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.956585Z","iopub.execute_input":"2024-11-12T08:47:27.957097Z","iopub.status.idle":"2024-11-12T08:47:27.996159Z","shell.execute_reply.started":"2024-11-12T08:47:27.957016Z","shell.execute_reply":"2024-11-12T08:47:27.994858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:27.997793Z","iopub.execute_input":"2024-11-12T08:47:27.998194Z","iopub.status.idle":"2024-11-12T08:47:28.020868Z","shell.execute_reply.started":"2024-11-12T08:47:27.998152Z","shell.execute_reply":"2024-11-12T08:47:28.019455Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"markdown","source":"## train, test 분할","metadata":{}},{"cell_type":"code","source":"features = train.drop('Class', axis = 1)\nlabel = train['Class']\n\nx_train, x_test, y_train, y_test = train_test_split(features, label, test_size = 0.3, random_state = 0, stratify = label)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.022504Z","iopub.execute_input":"2024-11-12T08:47:28.022991Z","iopub.status.idle":"2024-11-12T08:47:28.158987Z","shell.execute_reply.started":"2024-11-12T08:47:28.022939Z","shell.execute_reply":"2024-11-12T08:47:28.157751Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 모델에 따른 ROC-AUC 평가지표 함수","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\ndef model_eval(model, ftr_train = None, ftr_test = None, tgt_train = None, tgt_test = None):\n    model.fit(ftr_train, tgt_train)\n    pred = model.predict(ftr_test)\n    pred_proba = model.predict_proba(ftr_test)[:, 1]\n    \n    auc_score = roc_auc_score(tgt_test, pred)\n    return auc_score\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.160657Z","iopub.execute_input":"2024-11-12T08:47:28.161180Z","iopub.status.idle":"2024-11-12T08:47:28.168779Z","shell.execute_reply.started":"2024-11-12T08:47:28.161122Z","shell.execute_reply":"2024-11-12T08:47:28.167423Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 모델 평가 점수\n### (Linear Regression, XGBoost, Random Forest, LightGBM)","metadata":{}},{"cell_type":"code","source":"'''\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom xgboost import XGBClassifier\nfrom lightgbm import LGBMClassifier\n\nlr_clf = LogisticRegression(max_iter = 1000)\nprint('logistic Regression')\nprint(f'AUC : {model_eval(lr_clf, ftr_train = x_train, ftr_test = x_test, tgt_train = y_train, tgt_test = y_test)} \\n')\n\n\nxgb = XGBClassifier(n_estimators = 1000, learning_rate = 0.05, max_depth = 5)\nprint('XGBoost')\nprint(f'AUC : {model_eval(xgb, ftr_train = x_train, ftr_test = x_test, tgt_train = y_train, tgt_test = y_test)} \\n')\n\n\nrf_clf = RandomForestClassifier(n_estimators = 200, max_depth = 5)\nprint('Random Forest')\nprint(f'AUC : {model_eval(rf_clf, ftr_train = x_train, ftr_test = x_test, tgt_train = y_train, tgt_test = y_test)} \\n')\n\n\nlgbm_clf = LGBMClassifier(n_estimators = 1000, learning_rate = 0.05, max_depth=5)\nprint('LightGBM')\nlgbm_auc = model_eval(lgbm_clf, ftr_train=x_train, ftr_test=x_test, tgt_train=y_train, tgt_test=y_test)\nprint(f'AUC : {lgbm_auc}')\n\n'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.170412Z","iopub.execute_input":"2024-11-12T08:47:28.170860Z","iopub.status.idle":"2024-11-12T08:47:28.187973Z","shell.execute_reply.started":"2024-11-12T08:47:28.170796Z","shell.execute_reply":"2024-11-12T08:47:28.186445Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Logistic Regression : 0.79\n### XGBoost : 0.9\n### Random Forest : 0.87\n### LightGBM : 0.88\n---\n## Baseline : XGBoost","metadata":{}},{"cell_type":"markdown","source":"## 베이지안 최적화(Hyper Opt)","metadata":{}},{"cell_type":"markdown","source":"### 검색 공간","metadata":{}},{"cell_type":"code","source":"from hyperopt import hp\n\nxgb_search_space = {'max_depth': hp.quniform('max_depth', 5, 20, 1), \n                    'min_child_weight': hp.quniform('min_child_weight', 1, 2, 1),\n                    'learning_rate': hp.uniform('learning_rate', 0.01, 0.2),\n                    'colsample_bytree': hp.uniform('colsample_bytree', 0.5, 1),\n                   }","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.189926Z","iopub.execute_input":"2024-11-12T08:47:28.190344Z","iopub.status.idle":"2024-11-12T08:47:28.207005Z","shell.execute_reply.started":"2024-11-12T08:47:28.190303Z","shell.execute_reply":"2024-11-12T08:47:28.205750Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 목적 함수","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\nfrom xgboost import XGBClassifier\nfrom hyperopt import STATUS_OK\n\ndef objective_func(search_space):\n    xgb_clf = XGBClassifier(n_estimators=100, max_depth=int(search_space['max_depth']),\n                            min_child_weight=int(search_space['min_child_weight']),\n                            learning_rate=search_space['learning_rate'],\n                            colsample_bytree=search_space['colsample_bytree'],\n                            eval_metric='logloss')\n    roc_auc_scores = cross_val_score(xgb_clf, x_train, y_train, scoring='roc_auc', cv=3)\n    \n    return {'loss':-1 * np.mean(roc_auc_scores), 'status': STATUS_OK}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.208836Z","iopub.execute_input":"2024-11-12T08:47:28.209361Z","iopub.status.idle":"2024-11-12T08:47:28.222316Z","shell.execute_reply.started":"2024-11-12T08:47:28.209304Z","shell.execute_reply":"2024-11-12T08:47:28.220781Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### parameter 최적화","metadata":{}},{"cell_type":"code","source":"'''\nfrom hyperopt import fmin, tpe, Trials\n\ntrial_val = Trials()\nbest = fmin(fn = objective_func,\n            space = xgb_search_space,\n            algo = tpe.suggest,\n            max_evals = 50,\n            trials = trial_val, rstate=np.random.default_rng(seed=9))\nprint('best:', best)\n\n'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.226527Z","iopub.execute_input":"2024-11-12T08:47:28.226999Z","iopub.status.idle":"2024-11-12T08:47:28.246431Z","shell.execute_reply.started":"2024-11-12T08:47:28.226957Z","shell.execute_reply":"2024-11-12T08:47:28.245193Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### colsample_bytree = 0.68\n### learning_rate = 0.14\n### max_depth = 9\n### min_child_weight = 2","metadata":{}},{"cell_type":"markdown","source":"## 최종 모델 학습 및 submission 제출","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.248206Z","iopub.execute_input":"2024-11-12T08:47:28.248638Z","iopub.status.idle":"2024-11-12T08:47:28.279353Z","shell.execute_reply.started":"2024-11-12T08:47:28.248595Z","shell.execute_reply":"2024-11-12T08:47:28.278086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict_test.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.280837Z","iopub.execute_input":"2024-11-12T08:47:28.281251Z","iopub.status.idle":"2024-11-12T08:47:28.302698Z","shell.execute_reply.started":"2024-11-12T08:47:28.281205Z","shell.execute_reply":"2024-11-12T08:47:28.301331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb = XGBClassifier(n_estimators = 1000,\n                    colsample_bytree = 0.68,\n                    learning_rate = 0.14,\n                    max_depth = 9,\n                    min_child_weight = 2)\n\nxgb.fit(x_train, y_train)\ny_pred = xgb.predict(predict_test)\n\nsubmission = pd.DataFrame({'id': test['id'], 'Class': y_pred})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T08:47:28.304373Z","iopub.execute_input":"2024-11-12T08:47:28.304873Z","iopub.status.idle":"2024-11-12T08:47:40.660881Z","shell.execute_reply.started":"2024-11-12T08:47:28.304817Z","shell.execute_reply":"2024-11-12T08:47:40.659588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}