{"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"},{"sourceId":32089,"sourceType":"datasetVersion","datasetId":25184}],"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        \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","trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:39.164768Z","iopub.execute_input":"2024-11-20T07:37:39.165349Z","iopub.status.idle":"2024-11-20T07:37:39.563273Z","shell.execute_reply.started":"2024-11-20T07:37:39.165299Z","shell.execute_reply":"2024-11-20T07:37:39.562163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import roc_auc_score, roc_curve, classification_report\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n%matplotlib inline\n\nsubmission_df = pd.read_csv('/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/sample_submission.csv')\ntest_df = pd.read_csv('/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/test.csv')\ntrain_df = pd.read_csv('/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-11-20T07:37:39.565280Z","iopub.execute_input":"2024-11-20T07:37:39.565825Z","iopub.status.idle":"2024-11-20T07:37:44.068184Z","shell.execute_reply.started":"2024-11-20T07:37:39.565790Z","shell.execute_reply":"2024-11-20T07:37:44.067004Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 데이터 확인 및 정제","metadata":{}},{"cell_type":"code","source":"print(submission_df.shape)# 결측치는 없음\nprint(test_df.shape)# 결측치는 없음\nprint(train_df.shape)# 결측치는 없음","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:44.069528Z","iopub.execute_input":"2024-11-20T07:37:44.069863Z","iopub.status.idle":"2024-11-20T07:37:44.075808Z","shell.execute_reply.started":"2024-11-20T07:37:44.069829Z","shell.execute_reply":"2024-11-20T07:37:44.074604Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:44.078079Z","iopub.execute_input":"2024-11-20T07:37:44.078460Z","iopub.status.idle":"2024-11-20T07:37:44.399125Z","shell.execute_reply.started":"2024-11-20T07:37:44.078426Z","shell.execute_reply":"2024-11-20T07:37:44.397991Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:44.400405Z","iopub.execute_input":"2024-11-20T07:37:44.400733Z","iopub.status.idle":"2024-11-20T07:37:44.436026Z","shell.execute_reply.started":"2024-11-20T07:37:44.400699Z","shell.execute_reply":"2024-11-20T07:37:44.434962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Amount 확인\nimport seaborn as sns\n\nplt.figure(figsize = (8,4))\nplt.xticks(range(0, 30000, 1000), rotation=60)\nsns.histplot(train_df['Amount'], bins=100, kde=True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:44.437664Z","iopub.execute_input":"2024-11-20T07:37:44.438121Z","iopub.status.idle":"2024-11-20T07:37:45.646058Z","shell.execute_reply.started":"2024-11-20T07:37:44.438070Z","shell.execute_reply":"2024-11-20T07:37:45.644902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\ndef get_preprocessed_df(df=None):\n    df_copy = df.copy()\n    amount_n = np.log1p(df_copy['Amount'])\n    df_copy.insert(0, 'Amount_Scaled', amount_n)\n    df_copy.drop(['Time','Amount'], axis=1, inplace=True)\n    outlier_index = get_outlier(df=df_copy, column='V14', weight=1.5)\n    df_copy.drop(outlier_index, axis=0, inplace=True)\n    return df_copy\n# end of get_preprocessed_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:45.647216Z","iopub.execute_input":"2024-11-20T07:37:45.647588Z","iopub.status.idle":"2024-11-20T07:37:45.653937Z","shell.execute_reply.started":"2024-11-20T07:37:45.647555Z","shell.execute_reply":"2024-11-20T07:37:45.652792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\ndef get_outlier(df=None, column=None, weight=1.5):\n    fraud = df[df['Class']==1][column]\n    quantile_25 = np.percentile(fraud.values, 25)\n    quantile_75 = np.percentile(fraud.values, 75)\n    \n    iqr = quantile_75 - quantile_25\n    iqr_weight = iqr * weight\n    lowest_val = quantile_25 - iqr_weight\n    highest_val = quantile_75 + iqr_weight\n\n    outlier_index = fraud[(fraud < lowest_val) | (fraud > highest_val)].index\n    return outlier_index\n# end of get_outlier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:45.655670Z","iopub.execute_input":"2024-11-20T07:37:45.656136Z","iopub.status.idle":"2024-11-20T07:37:45.673490Z","shell.execute_reply.started":"2024-11-20T07:37:45.656089Z","shell.execute_reply":"2024-11-20T07:37:45.672369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_train_test_dataset(df=None):\n    df_copy = get_preprocessed_df(df)\n    X_features = df_copy.drop(columns=['Class'])\n    y_target = df_copy['Class']\n\n    # 학습 및 데이터 세트 분할\n    X_train, X_test, y_train, y_test = \\\n    train_test_split(X_features, y_target, test_size=0.3, random_state=0, stratify=y_target)\n    return X_train, X_test, y_train, y_test\n# end of def`","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:45.674935Z","iopub.execute_input":"2024-11-20T07:37:45.675253Z","iopub.status.idle":"2024-11-20T07:37:45.692851Z","shell.execute_reply.started":"2024-11-20T07:37:45.675206Z","shell.execute_reply":"2024-11-20T07:37:45.691631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Amount 재확인\n\ntrain_df_for_amount = get_preprocessed_df(train_df)\n\nplt.figure(figsize = (8,4))\nplt.xticks(range(0, 30000, 1000), rotation=60)\nsns.histplot(train_df_for_amount['Amount_Scaled'], bins=100, kde=True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:45.696194Z","iopub.execute_input":"2024-11-20T07:37:45.696650Z","iopub.status.idle":"2024-11-20T07:37:46.925735Z","shell.execute_reply.started":"2024-11-20T07:37:45.696605Z","shell.execute_reply":"2024-11-20T07:37:46.924586Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 필요함수 추가","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score\nfrom sklearn.metrics import roc_auc_score\nfrom xgboost import XGBClassifier\n\ndef get_clf_eval(y_test, pred=None, pred_proba=None):\n    confusion = confusion_matrix( y_test, pred)\n    accuracy = accuracy_score(y_test , pred)\n    precision = precision_score(y_test , pred)\n    recall = recall_score(y_test , pred)\n    f1 = f1_score(y_test,pred)\n    # ROC-AUC 추가\n    roc_auc = roc_auc_score(y_test, pred_proba)\n    print('오차 행렬')\n    print(confusion)\n    # ROC-AUC print 추가\n    print('정확도: {0:.4f}, 정밀도: {1:.4f}, 재현율: {2:.4f},\\\n    F1: {3:.4f}, ROC-AUC:{4:.4f}'.format(accuracy, precision, recall, f1, roc_auc))\n# end of get_clf_eval","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T08:31:14.916557Z","iopub.execute_input":"2024-11-20T08:31:14.917062Z","iopub.status.idle":"2024-11-20T08:31:14.924916Z","shell.execute_reply.started":"2024-11-20T08:31:14.917018Z","shell.execute_reply":"2024-11-20T08:31:14.923609Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 데이터분리","metadata":{}},{"cell_type":"code","source":"# 데이터 정제 후, 데이터 분리\nX_train, X_test, y_train, y_test = get_train_test_dataset(train_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:46.936548Z","iopub.execute_input":"2024-11-20T07:37:46.936845Z","iopub.status.idle":"2024-11-20T07:37:47.144180Z","shell.execute_reply.started":"2024-11-20T07:37:46.936815Z","shell.execute_reply":"2024-11-20T07:37:47.143194Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 학습 :레이블값 비율\ny_train.value_counts()/y_train.shape[0] * 100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:47.145552Z","iopub.execute_input":"2024-11-20T07:37:47.145894Z","iopub.status.idle":"2024-11-20T07:37:47.156968Z","shell.execute_reply.started":"2024-11-20T07:37:47.145853Z","shell.execute_reply":"2024-11-20T07:37:47.155837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 테스트 :레이블값 비율\ny_test.value_counts()/y_test.shape[0] * 100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:47.158471Z","iopub.execute_input":"2024-11-20T07:37:47.159337Z","iopub.status.idle":"2024-11-20T07:37:47.178414Z","shell.execute_reply.started":"2024-11-20T07:37:47.159287Z","shell.execute_reply":"2024-11-20T07:37:47.177216Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 모델 테스트\n\n* XGBoost\n* 랜덤 포레스트 : 앙상블 학습, 해당 데이터에서의 상관관계가 일정한 특징을 띄지 않는 것으로 파악되어 복잡한 관계에서 유리한 모델도 사용해보는 것으로 판단\n* LightGBM : 현재 데이터의 가장 큰 특징인 \"불균형 데이터\"에 가장 적합한 모델이기 때문\n\n편향된 분포를 가진 데이터셋이므로 XGboost, LightGBM, 랜덤포레스트를 적용 하겠음.","metadata":{}},{"cell_type":"markdown","source":"## XGBoost","metadata":{}},{"cell_type":"code","source":"# XGBoost 활용\nfrom xgboost import XGBClassifier\nfrom sklearn.metrics import roc_auc_score\n\n\n\n#######  XGBoost 조기중단 검증 \nxgb_clf = XGBClassifier(n_estimators=500, learning_rate=0.05, early_stopping_rounds=100, random_state=156)\n\n# 성능평가 지표를 auc로 , 조기중단 파라미터는 100으로 학습수행\nxgb_clf.fit( X_train, y_train , eval_set=[(X_train, y_train), (X_test, y_test)])\n\npred = xgb_clf.predict(X_test)\npred_proba = xgb_clf.predict_proba(X_test)[:, 1]\nget_clf_eval(y_test, pred = pred, pred_proba=pred_proba)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:47.179854Z","iopub.execute_input":"2024-11-20T07:37:47.180293Z","iopub.status.idle":"2024-11-20T07:37:57.521935Z","shell.execute_reply.started":"2024-11-20T07:37:47.180223Z","shell.execute_reply":"2024-11-20T07:37:57.520700Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## XGBoost 하이퍼 파라미터 튜닝","metadata":{}},{"cell_type":"code","source":"# XGBoost의 하이퍼 파라미터 튜닝\nfrom hyperopt import hp\n\nxgb_search_space = {'max_depth': hp.quniform('max_depth', 5, 15, 1), \n                    'min_child_weight': hp.quniform('min_child_weight', 1, 6, 1),\n                    'colsample_bytree': hp.uniform('colsample_bytree', 0.5, 0.95),\n                    'learning_rate': hp.uniform('learning_rate', 0.01, 0.2)\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:57.523262Z","iopub.execute_input":"2024-11-20T07:37:57.523595Z","iopub.status.idle":"2024-11-20T07:37:57.531842Z","shell.execute_reply.started":"2024-11-20T07:37:57.523562Z","shell.execute_reply":"2024-11-20T07:37:57.530504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.metrics import roc_auc_score\n\n# 목적 함수 설정. \n# 추후 fmin()에서 입력된 search_space값으로 XGBClassifier 교차 검증 학습 후 -1* roc_auc 평균 값을 반환.  \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                            colsample_bytree=search_space['colsample_bytree'],\n                            learning_rate=search_space['learning_rate']\n                           )\n    # 3개 k-fold 방식으로 평가된 roc_auc 지표를 담는 list\n    roc_auc_list= []\n    \n    # 3개 k-fold방식 적용 \n    kf = KFold(n_splits=3)\n    # X_train을 다시 학습과 검증용 데이터로 분리\n    for tr_index, val_index in kf.split(X_train):\n        # kf.split(X_train)으로 추출된 학습과 검증 index값으로 학습과 검증 데이터 세트 분리 \n        X_tr, y_tr = X_train.iloc[tr_index], y_train.iloc[tr_index]\n        X_val, y_val = X_train.iloc[val_index], y_train.iloc[val_index]\n        # early stopping은 30회로 설정하고 추출된 학습과 검증 데이터로 XGBClassifier 학습 수행. \n        xgb_clf.fit(X_tr, y_tr, early_stopping_rounds=30, eval_metric='auc',\n                   eval_set=[(X_tr, y_tr), (X_val, y_val)])\n    \n        # 1로 예측한 확률값 추출후 roc auc 계산하고 평균 roc auc 계산을 위해 list에 결과값 담음. \n        score = roc_auc_score(y_val, xgb_clf.predict_proba(X_val)[:, 1])\n        roc_auc_list.append(score)\n        \n    # 3개 k-fold로 계산된 roc_auc값의 평균값을 반환하되, \n    # HyperOpt는 목적함수의 최소값을 위한 입력값을 찾으므로 -1을 곱한 뒤 반환. \n    return -1 * np.mean(roc_auc_list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T07:37:57.533334Z","iopub.execute_input":"2024-11-20T07:37:57.533727Z","iopub.status.idle":"2024-11-20T07:37:57.545709Z","shell.execute_reply.started":"2024-11-20T07:37:57.533692Z","shell.execute_reply":"2024-11-20T07:37:57.544537Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from 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=10, # 최대 반복 횟수를 지정합니다.\n            trials=trial_val, \n            rstate=np.random.default_rng(seed=9))\nprint('best:', best)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T08:17:44.381637Z","iopub.execute_input":"2024-11-20T08:17:44.382183Z","iopub.status.idle":"2024-11-20T08:19:08.325831Z","shell.execute_reply.started":"2024-11-20T08:17:44.382136Z","shell.execute_reply":"2024-11-20T08:19:08.324741Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## XGBoost 하이퍼 파라미터 적용","metadata":{}},{"cell_type":"code","source":"## XGBoost  best :  {'colsample_bytree': 0.5972958165106059, 'learning_rate': 0.11009410661181598, 'max_depth': 7.0, 'min_child_weight': 3.0}\n# 최적 파라미터 적용 하기\nxgb_clf = XGBClassifier( n_estimators=100, max_depth = int(best['max_depth'])\n                       , min_child_weight = int(best['min_child_weight'])\n                       , colsample_bytree = best['colsample_bytree']\n                       , learning_rate = round(best['learning_rate'], 5)\n                       , early_stopping_rounds=100,  eval_metric=\"auc\"\n)\n\nxgb_clf.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_test, y_test)])\n\n\n# y_pred = xgb_clf.predict_proba(X_test)\ny_pred_proba = xgb_clf.predict_proba(X_test)[:, 1]\n\n\n# get_clf_eval(y_test, pred = y_pred, pred_proba=y_pred_proba)\n\nxgb_roc_score = roc_auc_score(y_test, y_pred_proba)\nprint('ROC AUC : {0:.4f}'.format(xgb_roc_score))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T08:32:41.266465Z","iopub.execute_input":"2024-11-20T08:32:41.267215Z","iopub.status.idle":"2024-11-20T08:32:44.643340Z","shell.execute_reply.started":"2024-11-20T08:32:41.267169Z","shell.execute_reply":"2024-11-20T08:32:44.642199Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## LightGBM","metadata":{"execution":{"iopub.status.busy":"2024-11-12T05:58:34.001037Z","iopub.execute_input":"2024-11-12T05:58:34.001401Z","iopub.status.idle":"2024-11-12T05:58:34.012868Z","shell.execute_reply.started":"2024-11-12T05:58:34.001365Z","shell.execute_reply":"2024-11-12T05:58:34.011651Z"}}},{"cell_type":"code","source":"## LightGBM 적용 해보기\n\nfrom lightgbm import LGBMClassifier\n\nlgbm_clf = LGBMClassifier(n_estimators = 500, early_stopping_rounds=100, eval_metric=\"auc\")\n\neval_set=[(X_train, y_train), (X_test, y_test)]\nlgbm_clf.fit(X_train, y_train, eval_set=eval_set)\n\nlgbm_roc_score = roc_auc_score(y_test, lgbm_clf.predict_proba(X_test)[:,1])\n\nprint('ROC AUC : {0:.4f}'.format(lgbm_roc_score))\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T01:44:28.372134Z","iopub.execute_input":"2024-11-20T01:44:28.372581Z","iopub.status.idle":"2024-11-20T01:44:33.039606Z","shell.execute_reply.started":"2024-11-20T01:44:28.372543Z","shell.execute_reply":"2024-11-20T01:44:33.038515Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## LightGBM 하이퍼 파라미터 튜닝","metadata":{"execution":{"iopub.status.busy":"2024-11-12T07:49:48.765326Z","iopub.execute_input":"2024-11-12T07:49:48.766566Z","iopub.status.idle":"2024-11-12T07:50:12.636994Z","shell.execute_reply.started":"2024-11-12T07:49:48.766423Z","shell.execute_reply":"2024-11-12T07:50:12.635865Z"}}},{"cell_type":"code","source":"from lightgbm import LGBMClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T08:35:44.499079Z","iopub.execute_input":"2024-11-20T08:35:44.500383Z","iopub.status.idle":"2024-11-20T08:35:45.796525Z","shell.execute_reply.started":"2024-11-20T08:35:44.500313Z","shell.execute_reply":"2024-11-20T08:35:45.795538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 하이퍼 파라미터 튜닝\nlgbm_search_space = {\n    'num_leaves':hp.quniform('num_leaves', 32, 64, 1)\n    ,'max_depth':hp.quniform('max_depth', 100, 160, 1)\n    ,'min_child_samples':hp.quniform('min_child_samples', 60, 100, 1)\n    ,'subsample':hp.uniform('subsample', 0.7, 1)\n    ,'learning_rate':hp.uniform('learning_rate', 0.01, 0.2)\n}\n\n## 목적함수 생성\ndef objective_func(search_space):\n    lgbm_clf = LGBMClassifier( n_estimators=100\n                              , num_leaves = int(search_space['num_leaves'])\n                              , max_depth = int(search_space['max_depth'])\n                            , min_child_samples = int(search_space['min_child_samples'])\n                            , subsample = search_space['subsample']\n                            , learning_rate = search_space['learning_rate']\n                            , early_stopping_rounds=30, eval_metric=\"auc\"\n    )\n\n    # 3개 k-fold방식으로 평가된 roc_auc 지표를 담는 list\n    roc_auc_list = []\n\n    # 3개 k-fold방식 적용\n    kf = KFold(n_splits = 3)\n    #X_train을 다시 학습과 검증용 데이터로 분리\n    for tr_index, val_index in kf.split(X_train) :\n        X_tr, y_tr = X_train.iloc[tr_index], y_train.iloc[tr_index]\n        X_val, y_val = X_train.iloc[val_index], y_train.iloc[val_index]\n\n        # XGBClassifier 학습수행\n        lgbm_clf.fit(X_tr, y_tr, eval_set=[(X_tr, y_tr), (X_val, y_val)])\n\n        # 1로 예측한 확률값 추출 후, roc auc계산하고 평균 roc auc 계산 위해 list에 결과값 담음.\n        score = roc_auc_score(y_val, lgbm_clf.predict_proba(X_val)[:,1])\n        roc_auc_list.append(score)\n\n    # 3개 k-fold로 계산 된 roc_auc값의 평균값을 반환하되,\n    # HyperOpt는 목적함수의 최소값을 위한 입력값을 찾으므로 -1을 곱한 뒤 반환.\n    return -1*np.mean(roc_auc_list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T08:35:51.484260Z","iopub.execute_input":"2024-11-20T08:35:51.485026Z","iopub.status.idle":"2024-11-20T08:35:51.496136Z","shell.execute_reply.started":"2024-11-20T08:35:51.484983Z","shell.execute_reply":"2024-11-20T08:35:51.494779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from hyperopt import fmin, tpe, Trials\n\ntrials = Trials()\n\nbest = fmin(\n    fn = objective_func\n    , space = lgbm_search_space\n    , algo = tpe.suggest\n    , max_evals = 50\n    , trials = trials\n    , rstate = np.random.default_rng(seed=30)\n)\n\nprint('best : ',best)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T08:35:55.754072Z","iopub.execute_input":"2024-11-20T08:35:55.754578Z","iopub.status.idle":"2024-11-20T08:42:08.113282Z","shell.execute_reply.started":"2024-11-20T08:35:55.754534Z","shell.execute_reply":"2024-11-20T08:42:08.112143Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## LightGBM 하이퍼 파라미터 적용","metadata":{"execution":{"iopub.status.busy":"2024-11-12T07:50:38.436954Z","iopub.execute_input":"2024-11-12T07:50:38.437404Z","iopub.status.idle":"2024-11-12T07:55:10.805520Z","shell.execute_reply.started":"2024-11-12T07:50:38.437365Z","shell.execute_reply":"2024-11-12T07:55:10.804360Z"}}},{"cell_type":"code","source":"best_lgbm_clf =  LGBMClassifier(n_estimators=500, num_leaves=int(best['num_leaves']),\n                           max_depth=int(best['max_depth']),\n                           min_child_samples=int(best['min_child_samples']), \n                           subsample=round(best['subsample'], 5),\n                           learning_rate=round(best['learning_rate'], 5)\n                           , early_stopping_rounds=100\n                          )\n\n# evaluation metric을 auc로, early stopping은 100 으로 설정하고 학습 수행. \nlgbm_clf.fit(X_train, y_train, \n            eval_metric=\"auc\",eval_set=[(X_train, y_train), (X_test, y_test)])\n\nlgbm_roc_score = roc_auc_score(y_test, lgbm_clf.predict_proba(X_test)[:,1])\nprint('ROC AUC: {0:.4f}'.format(lgbm_roc_score))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T08:55:51.442649Z","iopub.execute_input":"2024-11-20T08:55:51.443148Z","iopub.status.idle":"2024-11-20T08:56:01.528716Z","shell.execute_reply.started":"2024-11-20T08:55:51.443103Z","shell.execute_reply":"2024-11-20T08:56:01.527483Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 모델선정\n## XGBoost 선정","metadata":{"execution":{"iopub.status.busy":"2024-11-12T06:06:38.058446Z","iopub.execute_input":"2024-11-12T06:06:38.058871Z","iopub.status.idle":"2024-11-12T06:06:38.066175Z","shell.execute_reply.started":"2024-11-12T06:06:38.058833Z","shell.execute_reply":"2024-11-12T06:06:38.065007Z"}}},{"cell_type":"code","source":"# 데이터 정제 후, 데이터 분리\nX_train, X_test, y_train, y_test = get_train_test_dataset(train_df)\n\nbset_xgb_clf = XGBClassifier(n_estimators=100, max_depth=8,\n                        min_child_weight=6,\n                        colsample_bytree=0.7,\n                        learning_rate=0.15\n                       )\n\nbset_xgb_clf.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_test, y_test)])\n\n\n# y_pred = xgb_clf.predict_proba(X_test)\ny_pred_proba = xgb_clf.predict_proba(X_test)[:, 1]\n\n\n# xgb_roc_score = roc_auc_score(y_test, y_pred_proba)\n# print('ROC AUC : {0:.4f}'.format(xgb_roc_score))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T09:06:14.847962Z","iopub.execute_input":"2024-11-20T09:06:14.848459Z","iopub.status.idle":"2024-11-20T09:06:20.242082Z","shell.execute_reply.started":"2024-11-20T09:06:14.848415Z","shell.execute_reply":"2024-11-20T09:06:20.240914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 'Amount_Scaled' 열을 train 데이터에서 계산한 방식대로 test 데이터에 추가\ntest_df['Amount_Scaled'] = np.log1p(test_df['Amount'])\n\n# 학습 데이터에서 사용한 열만 선택\ntrain_columns = X_train.columns\n\n# test 데이터에서 train 데이터에 있는 피처만 선택\ntest_selected = test_df[train_columns]\n\n# 테스트 데이터에 대해 확률 예측\ny_pred_proba_test = bset_xgb_clf.predict_proba(test_selected)[:, 1]  # 두 번째 클래스의 확률값 추출\n\n# 제출 파일 생성 (id 열과 예측값을 포함)\nsubmission = pd.DataFrame({'id': test_df['id'], 'Class': y_pred_proba_test})\n\n# 제출 파일을 CSV로 저장\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-20T09:08:33.210609Z","iopub.execute_input":"2024-11-20T09:08:33.211150Z","iopub.status.idle":"2024-11-20T09:08:33.710453Z","shell.execute_reply.started":"2024-11-20T09:08:33.211104Z","shell.execute_reply":"2024-11-20T09:08:33.709391Z"}},"outputs":[],"execution_count":null}]}