{"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":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-27T05:45:37.660165Z","iopub.execute_input":"2024-06-27T05:45:37.661000Z","iopub.status.idle":"2024-06-27T05:45:37.673954Z","shell.execute_reply.started":"2024-06-27T05:45:37.660947Z","shell.execute_reply":"2024-06-27T05:45:37.671459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# [과정 및 결과 설명]\n### 1. 데이터 이해 및 탐색\n- 개인정보 보호 이유로 대분의 컬럼이 무의미한 이름으로 되어 있고, tain 데이터는 id, time, amount, class로 되어 있고, test에는 id, time, amount로 구성됨\n- class 컬럼이 이상거래를 나타내고 있으며 train 데이터의 class 컬럼의 불균형이 심하여, 조정이 필요함\n- train과 test를 concat하여 info(), describe()로 보면 결측치는 없고, float 타입이며, 이상치는 검토할 필요가 있어 보임 \n### 2. 데이터 전처리 과정\n- IQR을 이용하여 이상치를 제거하려 했으나 1.5일 경우 전체 데이터의 50% 이상이 손실됨\n- 3으로 이상치 범위를 넓히면 데이터 불균형이 더 심하여 이상치 제거 하지 않음\n- 모델 성능을 보고 개선이 필요하면 이상치 처리를 하려 하였으나, 첫 모델에서 양호한 성능을 보여 하지 않음\n- 모든 컬럼의 타임이 float이어서 인코딩은 필요 없음\n- 각 컬럼의 의미가 불분명하여 스케일링도 첫 모델 성능을 보고 판단하기로 하였으나, 첫 모델에서 양호한 성능을 보여 하지 않음\n- 피처 엔지니어링도 첫 모델의 성능을 보고 개선이 필요하면 추가로 진행할 생각이었음\n- 피처 엔지니어링은 Amount 컬럼에 대해 할 생각이었음\n- 거래 금액에 따라 이상거래와 정상 거래의 차이가 보였기 때문임\n- 거래 금액을 상/중/하로 나누어 새로운 컬럼을 만들 생각이었음\n- 거래 시간에 따른 차이도 새로운 컬럼을 만들 수 있을 것으로 생각됨 \n- PCA는 진행하였으나, 0.95 분산은 하나의 주성분으로 설명이 가능한 것으로 나타남 (노트북 제일 하단 참조)\n- PCA도 첫 모델의 성능을 보고 적용 여부를 판단하기로 함\n- 중요도가 적은 컬럼 삭제는 상관분석으로 진행함\n- 각 컬럼의 상관계수를 구한 뒤 임계값을 0.005로 설정함\n- 첫 모델 성능이 개선이 필요한 상황이면 임계값을 변경하며 다시 시도할 생각이었음\n- train 데이터의 Class 컬럼의 불균형한 분포를 보이는 것에 대해 오버샘플링 기법인 SMOTE를 적용함\n### 3. 모델 선택 및 비교\n- 첫 모델로 LightGBM을 선정하였고 주된 이유는 데이터 셋이 풍부했기 때문임\n- 데이터가 메모리에 한 번에 다 로드되지 않아도 처리할 수 있는 \"out-of-core\" 학습을 지원하는 등 학습 속도가 빠르기때문에 먼저 테스트해 보았음\n- LightGBM의 성능은 하이퍼파라미터에 민감할 수 있으므로, 최적의 성능을 얻기 위해서는 하이퍼파라미터 튜닝이 필요한데, 기본값으로 하여도 양호한 결과를 얻을 수 있었음\n- 따라서, 하이퍼 파라미터 튜닝을 별도로 진행하지 않았으나 첫 모델 성능이 좋지 않았다면, GridSearchCV 또는 RandomSearchCV를 이용하여 최적 하이퍼 파라미터를 산출할 생각이었음\n- 만일, LightGBM의 성능이 좋지 않았다면 다음으로 랜덤포레스트를 적용할 생각이었음\n- 이유는 배깅 방식의 앙상블 학습 모델로 LightGBM과 조금 다른 결과를 보일 것으로 생각했기 때문임\n- 랜덤포레스트를 두번째 모델로 고려한 이유는 훈련 시간이 상대적으로 오래 걸릴 수 있기 때문임 (삐른 것부터 보려고 했음)\n- 하지만, 랜덤포레스트는 과적합 방지에 우수하므로 두번째 모델로 생각했음\n- 세번째 모델은 XGBoost로 테스트할 생각이었음. XGBoost는 LigtGBM과 같은 계열이기에 순위에서 나중으로 밀림\n### 4. 성능 평가\n- 성능 평가는 Accuacy, Presision, Recall, F1, ROC-AUC로 평가함\n- Precision (정밀도): 모델이 양성(1)으로 예측한 샘플 중 실제로 양성인 샘플의 비율로 0.80을 보임\n- Recall (재현율): 실제 양성 샘플 중 모델이 올바르게 양성으로 예측한 샘플의 비율로 0.85임\n- F1-Score: 정밀도와 재현율의 조화 평균으로 0.825\n- Support는 각 클래스의 실제 샘플 수를 가리킴\n- ROC-AUC가 0.9622로 매우 높아 정상과 비정상을 잘 구분하는 것으로 판단되어 추가적인 모델 테스트는 진행하지 않음","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:37.677902Z","iopub.execute_input":"2024-06-27T05:45:37.678411Z","iopub.status.idle":"2024-06-27T05:45:37.684969Z","shell.execute_reply.started":"2024-06-27T05:45:37.678378Z","shell.execute_reply":"2024-06-27T05:45:37.683816Z"},"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-27T05:45:37.686286Z","iopub.execute_input":"2024-06-27T05:45:37.686603Z","iopub.status.idle":"2024-06-27T05:45:39.995719Z","shell.execute_reply.started":"2024-06-27T05:45:37.686578Z","shell.execute_reply":"2024-06-27T05:45:39.994516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. 데이터 둘러 보기","metadata":{}},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:39.997143Z","iopub.execute_input":"2024-06-27T05:45:39.997490Z","iopub.status.idle":"2024-06-27T05:45:40.006414Z","shell.execute_reply.started":"2024-06-27T05:45:39.997461Z","shell.execute_reply":"2024-06-27T05:45:40.005101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:40.009623Z","iopub.execute_input":"2024-06-27T05:45:40.010006Z","iopub.status.idle":"2024-06-27T05:45:40.035404Z","shell.execute_reply.started":"2024-06-27T05:45:40.009967Z","shell.execute_reply":"2024-06-27T05:45:40.034234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(3)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:40.036900Z","iopub.execute_input":"2024-06-27T05:45:40.037329Z","iopub.status.idle":"2024-06-27T05:45:40.065715Z","shell.execute_reply.started":"2024-06-27T05:45:40.037291Z","shell.execute_reply":"2024-06-27T05:45:40.064605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/affel-ds2-credit-card/train.csv', index_col=\"id\")\ntest = pd.read_csv('/kaggle/input/affel-ds2-credit-card/test.csv', index_col=\"id\")","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:40.067131Z","iopub.execute_input":"2024-06-27T05:45:40.067561Z","iopub.status.idle":"2024-06-27T05:45:42.212054Z","shell.execute_reply.started":"2024-06-27T05:45:40.067527Z","shell.execute_reply":"2024-06-27T05:45:42.210834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([train, test])\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:42.213800Z","iopub.execute_input":"2024-06-27T05:45:42.214236Z","iopub.status.idle":"2024-06-27T05:45:42.259613Z","shell.execute_reply.started":"2024-06-27T05:45:42.214195Z","shell.execute_reply":"2024-06-27T05:45:42.258348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe().T","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:42.263065Z","iopub.execute_input":"2024-06-27T05:45:42.263378Z","iopub.status.idle":"2024-06-27T05:45:42.752024Z","shell.execute_reply.started":"2024-06-27T05:45:42.263351Z","shell.execute_reply":"2024-06-27T05:45:42.750986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:42.753248Z","iopub.execute_input":"2024-06-27T05:45:42.753550Z","iopub.status.idle":"2024-06-27T05:45:42.760625Z","shell.execute_reply.started":"2024-06-27T05:45:42.753526Z","shell.execute_reply":"2024-06-27T05:45:42.759370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:42.762183Z","iopub.execute_input":"2024-06-27T05:45:42.762897Z","iopub.status.idle":"2024-06-27T05:45:42.772779Z","shell.execute_reply.started":"2024-06-27T05:45:42.762858Z","shell.execute_reply":"2024-06-27T05:45:42.771740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:42.774018Z","iopub.execute_input":"2024-06-27T05:45:42.774379Z","iopub.status.idle":"2024-06-27T05:45:42.790905Z","shell.execute_reply.started":"2024-06-27T05:45:42.774350Z","shell.execute_reply":"2024-06-27T05:45:42.789754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.countplot(x=train['Class'], data=train)\nplt.title('Distribution of the Class Column')\nplt.xlabel('Class')\nplt.ylabel('Count')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:42.792210Z","iopub.execute_input":"2024-06-27T05:45:42.792567Z","iopub.status.idle":"2024-06-27T05:45:43.051364Z","shell.execute_reply.started":"2024-06-27T05:45:42.792537Z","shell.execute_reply":"2024-06-27T05:45:43.050101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 거래 금액 분포\nsns.set(style=\"whitegrid\")\n\n# 정상 거래\nplt.figure(figsize=(6, 6))\nsns.histplot(train[train['Class'] == 0].Amount, bins=50, color='red', alpha=0.5, log_scale=(False, True))\nplt.title('거래 금액 분포 (정상)')\nplt.xlabel('거래 금액')\nplt.ylabel('거래 횟수')\nplt.legend(['정상'])\n\n# 이상거래\nplt.figure(figsize=(6, 6))\nsns.histplot(train[train['Class'] == 1].Amount, bins=50, color='blue', alpha=0.5, log_scale=(False, True))\nplt.title('거래 금액 분포 (비정상)')\nplt.xlabel('거래 금액 분포')\nplt.ylabel('거래 횟수')\nplt.legend(['비정상'])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:43.056436Z","iopub.execute_input":"2024-06-27T05:45:43.056883Z","iopub.status.idle":"2024-06-27T05:45:44.660763Z","shell.execute_reply.started":"2024-06-27T05:45:43.056854Z","shell.execute_reply":"2024-06-27T05:45:44.659656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. 이상치 제거","metadata":{}},{"cell_type":"markdown","source":"- factor를 1.5로 하면 50% 데이터 손실이므로 3으로 설정\n- 이상치 제거 후 데이터 불균형이 더욱 심화되어 이상치 제거 안함","metadata":{}},{"cell_type":"code","source":"def remove_outliers(df):\n    Q1 = df.quantile(0.25)\n    Q3 = df.quantile(0.75)\n    IQR = Q3 - Q1\n    is_outlier = (df < (Q1 - 3.5 * IQR)) | (df > (Q3 + 3.5 * IQR))\n    df_clean = df[~is_outlier.any(axis=1)]\n    return df_clean","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:44.662091Z","iopub.execute_input":"2024-06-27T05:45:44.662408Z","iopub.status.idle":"2024-06-27T05:45:44.668241Z","shell.execute_reply.started":"2024-06-27T05:45:44.662373Z","shell.execute_reply":"2024-06-27T05:45:44.667151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X = train.drop('Class', axis=1) \n# y = train['Class']\n# X_ro = remove_outliers(X)\n# y_ro = y[X_ro.index]\n# train_ro = X_ro.copy()\n# train_ro['Class'] = y_ro\n# train_ro.shape\n# train_ro['Class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:44.669796Z","iopub.execute_input":"2024-06-27T05:45:44.670154Z","iopub.status.idle":"2024-06-27T05:45:44.679333Z","shell.execute_reply.started":"2024-06-27T05:45:44.670124Z","shell.execute_reply":"2024-06-27T05:45:44.678209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. 중요도 적은 컬럼 삭제","metadata":{"execution":{"iopub.status.busy":"2024-06-27T02:16:10.380804Z","iopub.execute_input":"2024-06-27T02:16:10.381274Z","iopub.status.idle":"2024-06-27T02:16:10.402785Z","shell.execute_reply.started":"2024-06-27T02:16:10.381234Z","shell.execute_reply":"2024-06-27T02:16:10.401741Z"}}},{"cell_type":"code","source":"corr = train.corr()\ncorrelation = corr['Class'].drop('Class')\ncorrelation.sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:44.680766Z","iopub.execute_input":"2024-06-27T05:45:44.681145Z","iopub.status.idle":"2024-06-27T05:45:45.162449Z","shell.execute_reply.started":"2024-06-27T05:45:44.681113Z","shell.execute_reply":"2024-06-27T05:45:45.161397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0.005 \ntoBeDeleted_features = correlation[abs(correlation) < threshold]\ntoBeDeleted_features","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:45.164060Z","iopub.execute_input":"2024-06-27T05:45:45.164508Z","iopub.status.idle":"2024-06-27T05:45:45.176093Z","shell.execute_reply.started":"2024-06-27T05:45:45.164470Z","shell.execute_reply":"2024-06-27T05:45:45.175028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Drop columns\ntrain.drop(columns=['V13', 'V22', 'V23', 'V25', 'V26', 'Amount'], inplace=True)\ntest.drop(columns=['V13', 'V22', 'V23', 'V25', 'V26', 'Amount'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:45.177355Z","iopub.execute_input":"2024-06-27T05:45:45.177673Z","iopub.status.idle":"2024-06-27T05:45:45.211400Z","shell.execute_reply.started":"2024-06-27T05:45:45.177647Z","shell.execute_reply":"2024-06-27T05:45:45.210143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. 데이터 불균형 처리","metadata":{}},{"cell_type":"code","source":"X = train.drop('Class', axis=1) \ny = train['Class']","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:45.212824Z","iopub.execute_input":"2024-06-27T05:45:45.213284Z","iopub.status.idle":"2024-06-27T05:45:45.224898Z","shell.execute_reply.started":"2024-06-27T05:45:45.213242Z","shell.execute_reply":"2024-06-27T05:45:45.223784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:45.226301Z","iopub.execute_input":"2024-06-27T05:45:45.226642Z","iopub.status.idle":"2024-06-27T05:45:45.311516Z","shell.execute_reply.started":"2024-06-27T05:45:45.226614Z","shell.execute_reply":"2024-06-27T05:45:45.310397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE\n\nsmote = SMOTE(random_state=42)\nX_resampled, y_resampled = smote.fit_resample(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:45.313101Z","iopub.execute_input":"2024-06-27T05:45:45.313540Z","iopub.status.idle":"2024-06-27T05:45:45.465874Z","shell.execute_reply.started":"2024-06-27T05:45:45.313501Z","shell.execute_reply":"2024-06-27T05:45:45.464611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'SOMTE 전 학습용 피처/레이블: {X_train.shape, y_train.shape}')\nprint(f'SOMTE 후 학습용 피처/레이블: {X_resampled.shape, y_resampled.shape}')\nprint(f'SOMTE 후 레이블 값 분포: {pd.Series(y_resampled).value_counts()}')","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:45.467199Z","iopub.execute_input":"2024-06-27T05:45:45.467525Z","iopub.status.idle":"2024-06-27T05:45:45.477117Z","shell.execute_reply.started":"2024-06-27T05:45:45.467499Z","shell.execute_reply":"2024-06-27T05:45:45.475980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. 학습 및 예측","metadata":{}},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import classification_report, accuracy_score, precision_score, recall_score, f1_score, roc_auc_score\n\nlgb_model = lgb.LGBMClassifier(random_state=42, class_weight='balanced')\nlgb_model.fit(X_resampled, y_resampled)\n\ny_pred_lgb = lgb_model.predict(X_test)\ny_prob_lgb = lgb_model.predict_proba(X_test)[:, 1]","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:45.478536Z","iopub.execute_input":"2024-06-27T05:45:45.478958Z","iopub.status.idle":"2024-06-27T05:45:56.104071Z","shell.execute_reply.started":"2024-06-27T05:45:45.478920Z","shell.execute_reply":"2024-06-27T05:45:56.103136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"LightGBM Classification Report:\")\nprint(classification_report(y_test, y_pred_lgb))\nprint(\"LightGBM Accuracy:\", accuracy_score(y_test, y_pred_lgb))\nprint(\"LightGBM Precision:\", precision_score(y_test, y_pred_lgb))\nprint(\"LightGBM Recall:\", recall_score(y_test, y_pred_lgb))\nprint(\"LightGBM F1 Score:\", f1_score(y_test, y_pred_lgb))\nprint(\"LightGBM ROC-AUC:\", roc_auc_score(y_test, y_prob_lgb))","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:56.105536Z","iopub.execute_input":"2024-06-27T05:45:56.105976Z","iopub.status.idle":"2024-06-27T05:45:56.256576Z","shell.execute_reply.started":"2024-06-27T05:45:56.105935Z","shell.execute_reply":"2024-06-27T05:45:56.255522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. 최종 결과 제출","metadata":{}},{"cell_type":"code","source":"final = lgb_model.predict_proba(test)[:, 1]","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:56.258246Z","iopub.execute_input":"2024-06-27T05:45:56.258577Z","iopub.status.idle":"2024-06-27T05:45:56.886399Z","shell.execute_reply.started":"2024-06-27T05:45:56.258550Z","shell.execute_reply":"2024-06-27T05:45:56.885252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.index.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:56.887694Z","iopub.execute_input":"2024-06-27T05:45:56.888009Z","iopub.status.idle":"2024-06-27T05:45:56.894368Z","shell.execute_reply.started":"2024-06-27T05:45:56.887983Z","shell.execute_reply":"2024-06-27T05:45:56.893230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:56.895883Z","iopub.execute_input":"2024-06-27T05:45:56.896310Z","iopub.status.idle":"2024-06-27T05:45:56.907443Z","shell.execute_reply.started":"2024-06-27T05:45:56.896273Z","shell.execute_reply":"2024-06-27T05:45:56.906136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'id': test.index,  \n    'class': final\n})","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:56.908951Z","iopub.execute_input":"2024-06-27T05:45:56.909325Z","iopub.status.idle":"2024-06-27T05:45:56.919692Z","shell.execute_reply.started":"2024-06-27T05:45:56.909295Z","shell.execute_reply":"2024-06-27T05:45:56.918417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:56.921364Z","iopub.execute_input":"2024-06-27T05:45:56.921820Z","iopub.status.idle":"2024-06-27T05:45:56.938493Z","shell.execute_reply.started":"2024-06-27T05:45:56.921778Z","shell.execute_reply":"2024-06-27T05:45:56.937126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('chanwoo.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:56.939905Z","iopub.execute_input":"2024-06-27T05:45:56.940313Z","iopub.status.idle":"2024-06-27T05:45:57.235920Z","shell.execute_reply.started":"2024-06-27T05:45:56.940283Z","shell.execute_reply":"2024-06-27T05:45:57.234809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:57.237228Z","iopub.execute_input":"2024-06-27T05:45:57.237563Z","iopub.status.idle":"2024-06-27T05:45:57.262723Z","shell.execute_reply.started":"2024-06-27T05:45:57.237535Z","shell.execute_reply":"2024-06-27T05:45:57.261435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.decomposition import PCA\n\n# def perform_pca(data, explained_variance, plot_variance=False):\n#     pca = PCA()\n#     pca.fit(data)\n    \n#     cumulative_variance = np.cumsum(pca.explained_variance_ratio_)\n#     n_components = np.argmax(cumulative_variance >= explained_variance) + 1\n    \n#     pca = PCA(n_components=n_components)\n#     transformed_data = pca.fit_transform(data)\n    \n#     print(f'Number of components selected: {n_components}')\n#     print(f'Explained variance ratio: {pca.explained_variance_ratio_}')\n    \n#     if plot_variance:\n#         plt.figure(figsize=(8, 6))\n#         plt.plot(cumulative_variance, marker='o', linestyle='--')\n#         plt.axhline(y=explained_variance, color='r', linestyle='-')\n#         plt.axvline(x=n_components - 1, color='r', linestyle='-')\n#         plt.title('Cumulative Explained Variance by Number of Principal Components')\n#         plt.xlabel('Number of Principal Components')\n#         plt.ylabel('Cumulative Explained Variance')\n#         plt.grid()\n#         plt.show()\n    \n#     return transformed_data","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:57.264107Z","iopub.execute_input":"2024-06-27T05:45:57.264451Z","iopub.status.idle":"2024-06-27T05:45:57.274634Z","shell.execute_reply.started":"2024-06-27T05:45:57.264420Z","shell.execute_reply":"2024-06-27T05:45:57.273470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#     train_pca = perform_pca(train, explained_variance=0.95, plot_variance=True)\n#     test_pca = perform_pca(test, explained_variance=0.95, plot_variance=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T05:45:57.275809Z","iopub.execute_input":"2024-06-27T05:45:57.276172Z","iopub.status.idle":"2024-06-27T05:45:57.290133Z","shell.execute_reply.started":"2024-06-27T05:45:57.276142Z","shell.execute_reply":"2024-06-27T05:45:57.289130Z"},"trusted":true},"execution_count":null,"outputs":[]}]}