{"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":35332,"databundleVersionId":3723648,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"- 새로 노트북 열면 아래 코드를 줌, 실행하면 우리가 사용할 수 있는 인풋을 보여줌","metadata":{}},{"cell_type":"markdown","source":"# 0. Start","metadata":{}},{"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-05-14T10:16:14.389961Z","iopub.execute_input":"2024-05-14T10:16:14.390394Z","iopub.status.idle":"2024-05-14T10:16:14.447531Z","shell.execute_reply.started":"2024-05-14T10:16:14.390361Z","shell.execute_reply":"2024-05-14T10:16:14.446378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. EDA","metadata":{}},{"cell_type":"code","source":"import matplotlib.pylab as plt\nimport seaborn as sns\nfrom tqdm import tqdm\nimport time\nfrom sklearn import metrics\nfrom sklearn import model_selection\nfrom sklearn import preprocessing\nfrom sklearn import linear_model\nfrom sklearn import feature_selection\nfrom xgboost import XGBRegressor\nfrom sklearn.model_selection import cross_val_predict, KFold\nfrom sklearn.metrics import mean_squared_error\npd.set_option(\"display.max_columns\", None)\n\nplt.style.use(\"ggplot\")","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:14.449645Z","iopub.execute_input":"2024-05-14T10:16:14.450110Z","iopub.status.idle":"2024-05-14T10:16:14.458581Z","shell.execute_reply.started":"2024-05-14T10:16:14.450050Z","shell.execute_reply":"2024-05-14T10:16:14.457240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call_rows = 30000\n# train_data, 20000행만 불러옴, df 변수에 저장\ndf = pd.read_csv(\"/kaggle/input/amex-default-prediction/train_data.csv\", nrows=call_rows)\n# train_label, 20000행만 불러옴, target 변수에 저장\ntrain_labels = pd.read_csv(\"/kaggle/input/amex-default-prediction/train_labels.csv\", nrows=call_rows)\ntest_df = pd.read_csv(\"/kaggle/input/amex-default-prediction/test_data.csv\", nrows=call_rows)\n# df 정보, 실행하면 190 columns, float64(실수) 185개, int64(정수) 1개, object(객체) 4개로 구성\nprint(df.info())\n\n#column 190 개를 다 보려고 max_columns 300개로 설정\npd.set_option(\"display.max_columns\", 300)\n\n#df 위에 5행만 보여줌\ndisplay(df.head(5))\n\n#target 위에 10행만 보여줌\ndisplay(train_labels.head(5))","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:14.460519Z","iopub.execute_input":"2024-05-14T10:16:14.461075Z","iopub.status.idle":"2024-05-14T10:16:17.323841Z","shell.execute_reply.started":"2024-05-14T10:16:14.460996Z","shell.execute_reply":"2024-05-14T10:16:17.322575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = list(df.columns) # 모든 칼럼\n\n# 객체형 칼럼 (대회 data 섹션에서 가져옴)\ncategorial_col = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\n\n# categorial df만 보기위해 가져옴\ndf_cat = df[categorial_col]\n\n#describe로 평균, std, max, quantile 볼 수 있음\nprint(df_cat.describe())\n\n#D_63, D_64는 NaN 이라서 따로 분류\nprint(df_cat.describe(include='object'))\n\n# 전체보기 20행만\ndf_cat.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:17.327180Z","iopub.execute_input":"2024-05-14T10:16:17.327983Z","iopub.status.idle":"2024-05-14T10:16:17.408209Z","shell.execute_reply.started":"2024-05-14T10:16:17.327942Z","shell.execute_reply":"2024-05-14T10:16:17.407094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"D_64\"].unique()  # unique 쓰면 categorial 한 열에 어떤 항목이 있는지 보임\n\n# 결과로 나온 항목들 뜻을 chatgpt한테 추측해달라고 함","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:17.409339Z","iopub.execute_input":"2024-05-14T10:16:17.409659Z","iopub.status.idle":"2024-05-14T10:16:17.418540Z","shell.execute_reply.started":"2024-05-14T10:16:17.409632Z","shell.execute_reply":"2024-05-14T10:16:17.417414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"D_68\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:17.420064Z","iopub.execute_input":"2024-05-14T10:16:17.420464Z","iopub.status.idle":"2024-05-14T10:16:17.431563Z","shell.execute_reply.started":"2024-05-14T10:16:17.420429Z","shell.execute_reply":"2024-05-14T10:16:17.430491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"D_63\"].unique()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:17.432885Z","iopub.execute_input":"2024-05-14T10:16:17.433561Z","iopub.status.idle":"2024-05-14T10:16:17.444410Z","shell.execute_reply.started":"2024-05-14T10:16:17.433531Z","shell.execute_reply":"2024-05-14T10:16:17.443166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_cat[\"D_66\"].isna().sum() # D_66 NaN 개수","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:17.445831Z","iopub.execute_input":"2024-05-14T10:16:17.446281Z","iopub.status.idle":"2024-05-14T10:16:17.456831Z","shell.execute_reply.started":"2024-05-14T10:16:17.446244Z","shell.execute_reply":"2024-05-14T10:16:17.455476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- 정리하면, D_64에 있는 O,R,U,-1 은 각각 Open, Revolving, Unknown, -1(아마 결측치),\n- D_63에 있는 'CR', 'CO', 'CL', 'XZ', 'XM', 'XL'은 각각\n- Current, Charged Off, Closed, XZ, XM, XL -> 이건 무슨 말인지 모르겠음","metadata":{}},{"cell_type":"markdown","source":"## EDA - Drop N/A , Fill N/A","metadata":{}},{"cell_type":"code","source":"# N/A 개수를 각 열마다 보기\ncol = list(df.columns) # 모든 칼럼\ndel_col = [] # 지우고 싶은 칼럼\nfor i in range(len(list(df.columns))):\n    count = df[col[i]].isna().sum() # 각 칼럼의 n/a 갯수\n    \n    if count > call_rows*0.5:\n        del_col.append(col[i])\nprint(del_col)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:17.458360Z","iopub.execute_input":"2024-05-14T10:16:17.458685Z","iopub.status.idle":"2024-05-14T10:16:17.514255Z","shell.execute_reply.started":"2024-05-14T10:16:17.458659Z","shell.execute_reply":"2024-05-14T10:16:17.513129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_mod = df.drop(columns = del_col)\ndf_mod.info()\ndf_mod.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:17.518820Z","iopub.execute_input":"2024-05-14T10:16:17.519213Z","iopub.status.idle":"2024-05-14T10:16:17.669513Z","shell.execute_reply.started":"2024-05-14T10:16:17.519182Z","shell.execute_reply":"2024-05-14T10:16:17.668437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# row 별 결측치 보기\nrows_list = df.values.tolist()\n\nnan = []\nfor i in range(len(rows_list)):\n    count = pd.isna(rows_list[i]).sum()\n    if count > 0:\n        nan.append(pd.isna(rows_list[i]).sum())\n\nprint(\"max : \", max(nan))\nprint(\"min : \", min(nan))\nprint(\"avg : \", sum(nan)/len(nan)) # 평균\nplt.plot(nan,\"g--\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:17.670899Z","iopub.execute_input":"2024-05-14T10:16:17.671322Z","iopub.status.idle":"2024-05-14T10:16:20.892382Z","shell.execute_reply.started":"2024-05-14T10:16:17.671285Z","shell.execute_reply":"2024-05-14T10:16:20.891260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# row 별 결측치 보기\nrows_list = df_mod.values.tolist() # df_mod 는 필요없는 row를 지웠음\n\nnan = []\nfor i in range(len(rows_list)):\n    count = pd.isna(rows_list[i]).sum()\n    if count > 0:\n        nan_count = pd.isna(rows_list[i]).sum()\n        nan.append(nan_count)\n        if nan_count > 60: # 190개 중 60개 이상 비어있으면 프린트\n            print(f\"{nan_count},{i}-th person\")\n        \nplt.plot(nan,\"g--\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:20.893955Z","iopub.execute_input":"2024-05-14T10:16:20.894302Z","iopub.status.idle":"2024-05-14T10:16:23.602542Z","shell.execute_reply.started":"2024-05-14T10:16:20.894272Z","shell.execute_reply":"2024-05-14T10:16:23.601480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# row 별 결측치 보기\n# 사람이 중요하다 # 필요없는 칼럼만 지우면 된다\n# Convert DataFrame to a list of rows\nrows_list = df_mod.values.tolist()\n\nnan = []\nnan_idx = []\nfor i in range(len(rows_list)):\n    count = pd.isna(rows_list[i]).sum()\n    if count > 60: # 60개 카운트 넘는 row의 idx를 추가하고\n        nan_idx.append(i)\n        print(f\"{count},{i}-th person\")\n    else:\n        nan.append(count) # 아닌 경우에만 nan에 추가\n\n\ndf_mod2 = df_mod.drop(nan_idx)  # df_mod 에서 뺌\n\nplt.plot(nan,\"g--\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:23.603964Z","iopub.execute_input":"2024-05-14T10:16:23.604555Z","iopub.status.idle":"2024-05-14T10:16:25.630987Z","shell.execute_reply.started":"2024-05-14T10:16:23.604526Z","shell.execute_reply":"2024-05-14T10:16:25.629930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_mod2.describe(include='object')\n#df_mod3 = df_mod2.drop(columns = [\"S_2\"])\n#df_encoded = pd.get_dummies(df_mod3)\ndf_encoded.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:25.632332Z","iopub.execute_input":"2024-05-14T10:16:25.632663Z","iopub.status.idle":"2024-05-14T10:16:25.889094Z","shell.execute_reply.started":"2024-05-14T10:16:25.632634Z","shell.execute_reply":"2024-05-14T10:16:25.887972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDA - heatmap and some visuals","metadata":{}},{"cell_type":"code","source":"# Perform one-hot encoding on categorical columns\ndf_encoded = pd.get_dummies(df_mod2, columns=categorial_col.remove(\"D_66\"))\n\n# Calculate the correlation matrix\n#correlation_matrix = df_encoded.corr()\n\n# Plot the correlation matrix as a heatmap\nplt.figure(figsize=(8, 6))\nsns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=\".2f\")\nplt.title('Correlation Matrix after One-Hot Encoding')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:25.890359Z","iopub.execute_input":"2024-05-14T10:16:25.890665Z","iopub.status.idle":"2024-05-14T10:16:26.039162Z","shell.execute_reply.started":"2024-05-14T10:16:25.890639Z","shell.execute_reply":"2024-05-14T10:16:26.037692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10,5))\nsns.countplot(x=train_labels.target)\nplt.show()  # 0이 좀더 많음","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:26.040223Z","iopub.status.idle":"2024-05-14T10:16:26.040630Z","shell.execute_reply.started":"2024-05-14T10:16:26.040438Z","shell.execute_reply":"2024-05-14T10:16:26.040455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['S_2'] = pd.to_datetime(df['S_2'])\ntest_df['S_2'] = pd.to_datetime(test_df['S_2'])\n\ndf['S_2'].min(), df['S_2'].max()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:26.041909Z","iopub.status.idle":"2024-05-14T10:16:26.042303Z","shell.execute_reply.started":"2024-05-14T10:16:26.042114Z","shell.execute_reply":"2024-05-14T10:16:26.042131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['S_2'].min(), test_df['S_2'].max()","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:26.043711Z","iopub.status.idle":"2024-05-14T10:16:26.044118Z","shell.execute_reply.started":"2024-05-14T10:16:26.043906Z","shell.execute_reply":"2024-05-14T10:16:26.043922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Preprocessing","metadata":{}},{"cell_type":"code","source":"# standardscalar","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:26.045491Z","iopub.status.idle":"2024-05-14T10:16:26.045872Z","shell.execute_reply.started":"2024-05-14T10:16:26.045685Z","shell.execute_reply":"2024-05-14T10:16:26.045701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Modeling","metadata":{}},{"cell_type":"code","source":"# Assuming 'y_predict' is the name of the target column\ntarget_column = 'y_predict'\n\nX_train = df_mod2\ny_train = train_labels['target']\n\n# Initialize XGBoost model\nmodel = XGBRegressor()\n\n# Define number of folds for cross-validation\nn_folds = 5\n\n# Initialize KFold cross-validator\nkf = KFold(n_splits=n_folds, shuffle=True, random_state=42)\n\n# Perform 5-fold cross-validation\ny_pred_cv = cross_val_predict(model, X_train, y_train, cv=kf)\n\n# Calculate RMSE for cross-validated predictions\nrmse_cv = np.sqrt(mean_squared_error(y_train, y_pred_cv))\nprint(f\"Cross-validated RMSE: {rmse_cv}\")\n\n# Train the model on the entire training data\nmodel.fit(X_train, y_train)\n\n# Make predictions on test_data\nX_test = test_data  # Assuming test_data doesn't have the target column\ny_pred_test = model.predict(X_test)\n\n# Save predictions to a CSV file\npd.DataFrame({'y_predict': y_pred_test}).to_csv('./kaggle/working/predictions.csv', index=False)  # Update with your desired file path","metadata":{"execution":{"iopub.status.busy":"2024-05-14T10:16:26.047257Z","iopub.status.idle":"2024-05-14T10:16:26.047645Z","shell.execute_reply.started":"2024-05-14T10:16:26.047443Z","shell.execute_reply":"2024-05-14T10:16:26.047466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Todo\n## EDA \n- plotting\n- corr\n- heatmap\n## Preprocess\n- drop NA\n- fillNA2\n## Modeling\n- which model?\n## Parameter Tuning\n- \n","metadata":{}}]}