{"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":74571,"databundleVersionId":8671431,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-27T07:53:12.857870Z","iopub.execute_input":"2024-06-27T07:53:12.858265Z","iopub.status.idle":"2024-06-27T07:53:13.320845Z","shell.execute_reply.started":"2024-06-27T07:53:12.858226Z","shell.execute_reply":"2024-06-27T07:53:13.319668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/affel-ds2-credit-card/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/affel-ds2-credit-card/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:13.326709Z","iopub.execute_input":"2024-06-27T07:53:13.327059Z","iopub.status.idle":"2024-06-27T07:53:16.331880Z","shell.execute_reply.started":"2024-06-27T07:53:13.327028Z","shell.execute_reply":"2024-06-27T07:53:16.330537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:16.333503Z","iopub.execute_input":"2024-06-27T07:53:16.333891Z","iopub.status.idle":"2024-06-27T07:53:16.363699Z","shell.execute_reply.started":"2024-06-27T07:53:16.333859Z","shell.execute_reply":"2024-06-27T07:53:16.362350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:16.367411Z","iopub.execute_input":"2024-06-27T07:53:16.367904Z","iopub.status.idle":"2024-06-27T07:53:16.390002Z","shell.execute_reply.started":"2024-06-27T07:53:16.367858Z","shell.execute_reply":"2024-06-27T07:53:16.388906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:16.391442Z","iopub.execute_input":"2024-06-27T07:53:16.391811Z","iopub.status.idle":"2024-06-27T07:53:16.422374Z","shell.execute_reply.started":"2024-06-27T07:53:16.391777Z","shell.execute_reply":"2024-06-27T07:53:16.420784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(3)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:16.423722Z","iopub.execute_input":"2024-06-27T07:53:16.424110Z","iopub.status.idle":"2024-06-27T07:53:16.451384Z","shell.execute_reply.started":"2024-06-27T07:53:16.424080Z","shell.execute_reply":"2024-06-27T07:53:16.450137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape, test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:16.452729Z","iopub.execute_input":"2024-06-27T07:53:16.453085Z","iopub.status.idle":"2024-06-27T07:53:16.462294Z","shell.execute_reply.started":"2024-06-27T07:53:16.453056Z","shell.execute_reply":"2024-06-27T07:53:16.461102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.drop(['id', 'Class'], axis=1)\ny_train = train['Class']","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:16.464117Z","iopub.execute_input":"2024-06-27T07:53:16.464469Z","iopub.status.idle":"2024-06-27T07:53:16.490299Z","shell.execute_reply.started":"2024-06-27T07:53:16.464439Z","shell.execute_reply":"2024-06-27T07:53:16.488961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test.drop(['id'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:16.491627Z","iopub.execute_input":"2024-06-27T07:53:16.491986Z","iopub.status.idle":"2024-06-27T07:53:16.509266Z","shell.execute_reply.started":"2024-06-27T07:53:16.491954Z","shell.execute_reply":"2024-06-27T07:53:16.507912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nplt.figure(figsize=(8,4))\nplt.xticks(range(0, 30000, 1000), rotation=60)\nsns.histplot(train['Amount'], bins=100, kde=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:16.510709Z","iopub.execute_input":"2024-06-27T07:53:16.511114Z","iopub.status.idle":"2024-06-27T07:53:18.631073Z","shell.execute_reply.started":"2024-06-27T07:53:16.511083Z","shell.execute_reply":"2024-06-27T07:53:18.629804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\namount_m = scaler.fit_transform(train['Amount'].values.reshape(-1,1))\namount_n = scaler.fit_transform(test['Amount'].values.reshape(-1,1))\n# 변환된 Amount를 Amount_Scaled로 피처명 변경후 DataFrame맨 앞 칼럼으로 입력\ntrain.insert(0, 'Amount_Scaled', amount_m)\ntest.insert(0, 'Amount_Scaled', amount_n)\n# 기존 Time 피처 삭제\ntrain.drop(['Time'], axis=1, inplace=True)\ntest.drop(['Time'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:18.632558Z","iopub.execute_input":"2024-06-27T07:53:18.632943Z","iopub.status.idle":"2024-06-27T07:53:18.735662Z","shell.execute_reply.started":"2024-06-27T07:53:18.632910Z","shell.execute_reply":"2024-06-27T07:53:18.734401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 넘파이의 lop1p()를 이용해 Amount를 로그 변환\namount_m = np.log1p(train['Amount'])\namount_n = np.log1p(test['Amount'])\ntrain.replace('Amount_Scaled', amount_m)\ntest.replace('Amount_Scaled', amount_n)\n# 기존 Amount 피처 삭제\ntrain.drop(['Amount'], axis=1, inplace=True)\ntest.drop(['Amount'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:18.737145Z","iopub.execute_input":"2024-06-27T07:53:18.737593Z","iopub.status.idle":"2024-06-27T07:53:19.034177Z","shell.execute_reply.started":"2024-06-27T07:53:18.737550Z","shell.execute_reply":"2024-06-27T07:53:19.033027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fraud = train[train['Class']==1]\nquantile_25 = np.percentile(fraud.values, 25)\nquantile_75 = np.percentile(fraud.values, 75)\n# IQR을 구하고, IQR에 1.5를 곱해 최댓값과 최솟값 지점 구함\niqr = quantile_75 - quantile_25\nlowest_val = quantile_25 - iqr * 1.5\nhighest_val = quantile_75 + iqr * 1.5\n# 최댓값보다 크거나, 최솟값보다 작은 값을 이상치 데이터로 설정하고 DataFrame index로 반환\noutlier_index = fraud[(fraud < lowest_val) | (fraud > highest_val)].index","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:19.038363Z","iopub.execute_input":"2024-06-27T07:53:19.038774Z","iopub.status.idle":"2024-06-27T07:53:19.053028Z","shell.execute_reply.started":"2024-06-27T07:53:19.038727Z","shell.execute_reply":"2024-06-27T07:53:19.051685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(outlier_index, axis=0, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:19.054799Z","iopub.execute_input":"2024-06-27T07:53:19.055200Z","iopub.status.idle":"2024-06-27T07:53:19.090161Z","shell.execute_reply.started":"2024-06-27T07:53:19.055168Z","shell.execute_reply":"2024-06-27T07:53:19.088717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE\n\nsmote = SMOTE(random_state=0)\nX_train_over, y_train_over = smote.fit_resample(X_train, y_train)\nprint('SMOTE 적용 전 학습용 피처/레이블 데이터 세트:', X_train.shape, y_train.shape)\nprint('SMOTE 적용 후 학습용 피처/레이블 데이터 세트:', X_train_over.shape, y_train_over.shape)\nprint('SMOTE 적용 후 레이블 값 분포: \\n', pd.Series(y_train_over).value_counts())","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:53:19.091801Z","iopub.execute_input":"2024-06-27T07:53:19.092202Z","iopub.status.idle":"2024-06-27T07:53:19.610606Z","shell.execute_reply.started":"2024-06-27T07:53:19.092169Z","shell.execute_reply":"2024-06-27T07:53:19.609424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n\nparam_grid={'C': [0.001, 0.01, 0.1, 1, 10, 100]}\nfrom sklearn.model_selection import GridSearchCV\ngrid_search=GridSearchCV(LogisticRegression(), param_grid, cv=5)\ngrid_search.fit(X_train, y_train)\n\nGridSearchCV(cv=5, estimator=LogisticRegression(),\n            param_grid={'C':[0.001, 0.01, 0.1, 1, 10, 100]})\n\nprint(\"Best Parameter: {}\".format(grid_search.best_params_))\nprint(\"Best Score: {:.4f}\".format(grid_search.best_score_))","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:55:03.199212Z","iopub.execute_input":"2024-06-27T07:55:03.200114Z","iopub.status.idle":"2024-06-27T07:56:25.458200Z","shell.execute_reply.started":"2024-06-27T07:55:03.200069Z","shell.execute_reply":"2024-06-27T07:56:25.456575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n\n# 로지스틱 회귀 모델 학습\nmodel = LogisticRegression(C=0.001, max_iter=1000)\nmodel.fit(X_train, y_train)\n\n# 테스트 데이터에 대한 예측\ny_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T07:56:25.460386Z","iopub.execute_input":"2024-06-27T07:56:25.461441Z","iopub.status.idle":"2024-06-27T07:56:34.731209Z","shell.execute_reply.started":"2024-06-27T07:56:25.461378Z","shell.execute_reply":"2024-06-27T07:56:34.729595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import RandomizedSearchCV\n\nparams = {\n    'max_depth': [8, 16, 24],\n    'min_samples_leaf': [1, 6, 12],\n    'min_samples_split': [2, 8, 16]\n}\n# RandomForestClassifier 객체 생성 후 RandomSearchCV 수행\nmodel1 = RandomForestClassifier(n_estimators=100, random_state=0, n_jobs=-1)\nrandom_cv = RandomizedSearchCV(model1, param_distributions=params, cv=2, n_jobs=-1, random_state=0)\nrandom_cv.fit(X_train, y_train)\n\nprint('최적 하이퍼 파라미터:\\n', random_cv.best_params_)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:18:28.060908Z","iopub.execute_input":"2024-06-27T08:18:28.061337Z","iopub.status.idle":"2024-06-27T08:27:09.016445Z","shell.execute_reply.started":"2024-06-27T08:18:28.061303Z","shell.execute_reply":"2024-06-27T08:27:09.015072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# 랜덤 포레스트 학습\nmodel1 = RandomForestClassifier(n_estimators=100, min_samples_leaf=6, max_depth=16, \n                                min_samples_split=8, random_state=0)\nmodel1.fit(X_train, y_train)\n\n# 테스트 데이터에 대한 예측\ny_pred1 = model1.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:28:05.882552Z","iopub.execute_input":"2024-06-27T08:28:05.883023Z","iopub.status.idle":"2024-06-27T08:30:58.173409Z","shell.execute_reply.started":"2024-06-27T08:28:05.882974Z","shell.execute_reply":"2024-06-27T08:30:58.172185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nftr_importances_values = model1.feature_importances_\nftr_importances = pd.Series(ftr_importances_values, index=X_train.columns)\nftr_top20 = ftr_importances.sort_values(ascending=False)[:20]\n\nplt.figure(figsize=(8,6))\nplt.title('Feature importances Top 20')\nsns.barplot(x=ftr_top20, y=ftr_top20.index)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:31:13.395508Z","iopub.execute_input":"2024-06-27T08:31:13.396071Z","iopub.status.idle":"2024-06-27T08:31:13.866637Z","shell.execute_reply.started":"2024-06-27T08:31:13.396031Z","shell.execute_reply":"2024-06-27T08:31:13.865448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id': test['id'], 'Class': y_pred})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:31:28.496367Z","iopub.execute_input":"2024-06-27T08:31:28.497315Z","iopub.status.idle":"2024-06-27T08:31:28.670695Z","shell.execute_reply.started":"2024-06-27T08:31:28.497262Z","shell.execute_reply":"2024-06-27T08:31:28.669416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id': test['id'], 'Class': y_pred1})\nsubmission.to_csv('submission1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:31:30.647646Z","iopub.execute_input":"2024-06-27T08:31:30.648066Z","iopub.status.idle":"2024-06-27T08:31:30.815913Z","shell.execute_reply.started":"2024-06-27T08:31:30.648032Z","shell.execute_reply":"2024-06-27T08:31:30.814729Z"},"trusted":true},"execution_count":null,"outputs":[]}]}