{"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":50160,"databundleVersionId":7602123,"sourceType":"competition"},{"sourceId":28903,"sourceType":"datasetVersion","datasetId":22535}],"dockerImageVersionId":30646,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#!pip install googletrans==3.1.0a0 --upgrade --quiet","metadata":{"execution":{"iopub.status.busy":"2024-03-11T12:59:19.491172Z","iopub.execute_input":"2024-03-11T12:59:19.491674Z","iopub.status.idle":"2024-03-11T12:59:19.498131Z","shell.execute_reply.started":"2024-03-11T12:59:19.491636Z","shell.execute_reply":"2024-03-11T12:59:19.496450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n#from googletrans import Translator\nsns.set_theme(style=\"ticks\")#這段程式碼還設置了 seaborn 的主題樣式為 \"ticks\"。這將為 seaborn 繪製的圖形添加背景格線。這樣設置是為了美化圖形並使其更易於閱讀。\nimport warnings\n# warnings.simplefilter(\"ignore\", UserWarning)\nwarnings.simplefilter(\"ignore\")\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T12:59:19.500671Z","iopub.execute_input":"2024-03-11T12:59:19.501445Z","iopub.status.idle":"2024-03-11T12:59:19.510105Z","shell.execute_reply.started":"2024-03-11T12:59:19.501400Z","shell.execute_reply":"2024-03-11T12:59:19.509088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_base.csv')\ntest_base = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/csv_files/test/test_base.csv')\ntrain_sta_0_0 = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_static_0_0.csv')\ntrain_sta_0_1 = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_static_0_1.csv')\n#train_tax_registry_c_1 = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_tax_registry_c_1.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T12:59:19.511864Z","iopub.execute_input":"2024-03-11T12:59:19.512562Z","iopub.status.idle":"2024-03-11T13:00:09.311998Z","shell.execute_reply.started":"2024-03-11T12:59:19.512515Z","shell.execute_reply":"2024-03-11T13:00:09.310686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_static_cb_0 = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_static_cb_0.csv')\n#train_person_1 = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/csv_files/train/train_person_1.csv')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:09.316955Z","iopub.execute_input":"2024-03-11T13:00:09.319871Z","iopub.status.idle":"2024-03-11T13:00:09.326851Z","shell.execute_reply.started":"2024-03-11T13:00:09.319807Z","shell.execute_reply":"2024-03-11T13:00:09.325304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#   將train_base資料中的date_decision，\n# 轉換成datetime64的型態。","metadata":{}},{"cell_type":"code","source":"#轉成日期與時間：https://vimsky.com/examples/usage/python-pandas-to_datetime.html\n# overwriting data after changing format \ntrain_base[\"date_decision\"]= pd.to_datetime(train_base[\"date_decision\"]) \n  \n# info of data \ntrain_base.info() \n  \n# display \ntrain_base","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:09.328524Z","iopub.execute_input":"2024-03-11T13:00:09.328931Z","iopub.status.idle":"2024-03-11T13:00:09.689032Z","shell.execute_reply.started":"2024-03-11T13:00:09.328887Z","shell.execute_reply":"2024-03-11T13:00:09.687290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fig = figure()\n#train_base.bar(kind='line', \n#                                x = 'MONTH',\n#                                y= 'target',\n#                                title = 'MONTHE by target',\n#                                xlabel = 'month',\n#                                ylabel = 'target',\n#                                legend = True,\n#                                marker = 'o',\n#                                linestyle = '--')\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:09.690541Z","iopub.execute_input":"2024-03-11T13:00:09.690924Z","iopub.status.idle":"2024-03-11T13:00:09.697696Z","shell.execute_reply.started":"2024-03-11T13:00:09.690892Z","shell.execute_reply":"2024-03-11T13:00:09.696204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_tax_registry_c_1","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:09.699168Z","iopub.execute_input":"2024-03-11T13:00:09.699748Z","iopub.status.idle":"2024-03-11T13:00:09.710657Z","shell.execute_reply.started":"2024-03-11T13:00:09.699712Z","shell.execute_reply":"2024-03-11T13:00:09.709294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 加入type變數，以便操作","metadata":{}},{"cell_type":"code","source":"train_base['type'] = \"train\"\ntest_base['type'] = \"test\"\ndata_base = pd.concat([train_base,test_base],axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:09.712700Z","iopub.execute_input":"2024-03-11T13:00:09.713116Z","iopub.status.idle":"2024-03-11T13:00:13.408128Z","shell.execute_reply.started":"2024-03-11T13:00:09.713081Z","shell.execute_reply":"2024-03-11T13:00:13.406528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_base.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:13.409826Z","iopub.execute_input":"2024-03-11T13:00:13.410327Z","iopub.status.idle":"2024-03-11T13:00:13.794332Z","shell.execute_reply.started":"2024-03-11T13:00:13.410283Z","shell.execute_reply":"2024-03-11T13:00:13.793286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 準備資料\ntype_base = data_base[\"type\"]\ntarget = data_base[\"target\"]\n\n# 建立盒狀圖\nplt.boxplot([target[type_base == \"train\"], target[type_base == \"test\"]], labels=[\"train\", \"test\"])\nplt.title(\"target by train & test\")\nplt.xlabel(\"train & test\")\nplt.ylabel(\"target\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:13.798849Z","iopub.execute_input":"2024-03-11T13:00:13.801028Z","iopub.status.idle":"2024-03-11T13:00:14.645313Z","shell.execute_reply.started":"2024-03-11T13:00:13.800957Z","shell.execute_reply":"2024-03-11T13:00:14.643387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_base[\"date_decision\"]= pd.to_datetime(data_base[\"date_decision\"]) \ndata_base.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:14.647227Z","iopub.execute_input":"2024-03-11T13:00:14.647969Z","iopub.status.idle":"2024-03-11T13:00:19.513141Z","shell.execute_reply.started":"2024-03-11T13:00:14.647929Z","shell.execute_reply":"2024-03-11T13:00:19.511541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = sns.FacetGrid(data_base,col=\"target\")\n#建立了一個分面網格，根據資料框 data_base 中的 target 欄位來分面。\ng.map(plt.hist,\"type\",bins=20)#bins=20：這是直方圖的垃圾桶數量","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:19.514874Z","iopub.execute_input":"2024-03-11T13:00:19.515285Z","iopub.status.idle":"2024-03-11T13:00:26.380132Z","shell.execute_reply.started":"2024-03-11T13:00:19.515251Z","shell.execute_reply":"2024-03-11T13:00:26.378847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_df1 = data_base.groupby(['type','MONTH']).count().reset_index().rename(columns={'case_id':'count'})[['MONTH','count','type']]\nplot_df1","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:26.381909Z","iopub.execute_input":"2024-03-11T13:00:26.382895Z","iopub.status.idle":"2024-03-11T13:00:26.628603Z","shell.execute_reply.started":"2024-03-11T13:00:26.382853Z","shell.execute_reply":"2024-03-11T13:00:26.627360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_df1.MONTH = plot_df1.MONTH.astype('str')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:26.630373Z","iopub.execute_input":"2024-03-11T13:00:26.630732Z","iopub.status.idle":"2024-03-11T13:00:26.641413Z","shell.execute_reply.started":"2024-03-11T13:00:26.630701Z","shell.execute_reply":"2024-03-11T13:00:26.639670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 把0層與base層合併，合併為data","metadata":{}},{"cell_type":"code","source":"train_sta_0 = pd.concat([train_sta_0_0,train_sta_0_1],axis=0).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:26.645150Z","iopub.execute_input":"2024-03-11T13:00:26.645623Z","iopub.status.idle":"2024-03-11T13:00:38.014261Z","shell.execute_reply.started":"2024-03-11T13:00:26.645583Z","shell.execute_reply":"2024-03-11T13:00:38.013139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = train_base.merge(train_sta_0,on='case_id',how='left')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:38.015640Z","iopub.execute_input":"2024-03-11T13:00:38.016939Z","iopub.status.idle":"2024-03-11T13:00:54.182301Z","shell.execute_reply.started":"2024-03-11T13:00:38.016898Z","shell.execute_reply":"2024-03-11T13:00:54.177174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:54.185746Z","iopub.execute_input":"2024-03-11T13:00:54.186386Z","iopub.status.idle":"2024-03-11T13:00:55.231262Z","shell.execute_reply.started":"2024-03-11T13:00:54.186329Z","shell.execute_reply":"2024-03-11T13:00:55.227749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data[\"date_decision\"]= pd.to_datetime(data[\"date_decision\"]) \n#data.info() \n#data\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:55.237698Z","iopub.execute_input":"2024-03-11T13:00:55.239139Z","iopub.status.idle":"2024-03-11T13:00:55.254814Z","shell.execute_reply.started":"2024-03-11T13:00:55.239009Z","shell.execute_reply":"2024-03-11T13:00:55.251212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## data 整理與探勘\n>  先取出data之中，類別型(object)的變數特徵來做翻譯。","metadata":{}},{"cell_type":"code","source":"# 取得類別型變數的欄位名稱\ncategorical_features = data.select_dtypes(include=[\"object\"]).columns.tolist()\n\n# 輸出變數名稱\nprint(categorical_features)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:00:55.260638Z","iopub.execute_input":"2024-03-11T13:00:55.263736Z","iopub.status.idle":"2024-03-11T13:01:02.216720Z","shell.execute_reply.started":"2024-03-11T13:00:55.263671Z","shell.execute_reply":"2024-03-11T13:01:02.211851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(categorical_features)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:02.221128Z","iopub.execute_input":"2024-03-11T13:01:02.224614Z","iopub.status.idle":"2024-03-11T13:01:02.255724Z","shell.execute_reply.started":"2024-03-11T13:01:02.224544Z","shell.execute_reply":"2024-03-11T13:01:02.250618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the list into a DataFrame\ncategorical_features_df = pd.DataFrame(categorical_features, columns=['variable_names'])\n\n# Print the DataFrame\nprint(categorical_features_df)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:02.260067Z","iopub.execute_input":"2024-03-11T13:01:02.262394Z","iopub.status.idle":"2024-03-11T13:01:02.298397Z","shell.execute_reply.started":"2024-03-11T13:01:02.261943Z","shell.execute_reply":"2024-03-11T13:01:02.292665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> 資料探勘：看actualdpdtolerance_344P>=1時，有哪些情況。\n","metadata":{}},{"cell_type":"code","source":"x = data.loc[data['actualdpdtolerance_344P']>=1]\nx.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:02.306049Z","iopub.execute_input":"2024-03-11T13:01:02.307095Z","iopub.status.idle":"2024-03-11T13:01:02.489854Z","shell.execute_reply.started":"2024-03-11T13:01:02.306960Z","shell.execute_reply":"2024-03-11T13:01:02.486733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> 看0層的維度","metadata":{}},{"cell_type":"code","source":"train_sta_0_0.shape,train_sta_0_1.shape,train_base.shape,","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:02.493652Z","iopub.execute_input":"2024-03-11T13:01:02.494757Z","iopub.status.idle":"2024-03-11T13:01:02.516267Z","shell.execute_reply.started":"2024-03-11T13:01:02.494709Z","shell.execute_reply":"2024-03-11T13:01:02.510841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_sta_0 = pd.concat([train_static_0_0,train_static_0_1],axis=0).reset_index(drop=True)\n#pd.concat()用於將兩個或多個資料框架按照指定的軸（axis）進行連接。\n#axis=0 表示要沿著行（row）的方向進行連接\n#.reset_index(drop=True) 是將新連接後的資料框架重新設置索引（index），並丟棄舊的索引。\n#drop=True 表示要丟棄舊的索引，不保留在資料框架中，而是重新生成一個新的連續的整數索引。","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:02.519643Z","iopub.execute_input":"2024-03-11T13:01:02.521259Z","iopub.status.idle":"2024-03-11T13:01:02.539860Z","shell.execute_reply.started":"2024-03-11T13:01:02.521082Z","shell.execute_reply":"2024-03-11T13:01:02.534769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data = train_base.merge(train_sta_0,on='case_id',how='left')\n#.merge() 是 pandas 套件提供的方法，用於將兩個資料框架按照指定的欄位進行合併。\n#on='case_id' 表示合併時所依據的欄位是 'case_id'，也就是兩個資料框架中共同的欄位。\n#how='left' 表示使用左邊的資料框架 train_base 為基準，保留所有左邊資料框架中的資料，\n#並將右邊資料框架 train_static_0 中的資料按照 'case_id' 欄位的值與左邊資料框架進行合併。\n#如果右邊資料框架中沒有與左邊資料框架匹配的資料，則填充為 NaN（缺失值）。\n#data","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:02.545976Z","iopub.execute_input":"2024-03-11T13:01:02.547423Z","iopub.status.idle":"2024-03-11T13:01:02.559284Z","shell.execute_reply.started":"2024-03-11T13:01:02.547263Z","shell.execute_reply":"2024-03-11T13:01:02.557975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 觀察data的target、MONTH、WEEK_NUM分布情況。\n> # (探討一個維度時，變數能呈現的圖表，及分析。)","metadata":{}},{"cell_type":"code","source":"# Check target distribution\ndata.target.value_counts().sort_index(ascending=True).plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:02.561759Z","iopub.execute_input":"2024-03-11T13:01:02.562960Z","iopub.status.idle":"2024-03-11T13:01:03.296177Z","shell.execute_reply.started":"2024-03-11T13:01:02.562897Z","shell.execute_reply":"2024-03-11T13:01:03.291494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MON_COUNT = data.MONTH.value_counts().sort_index(ascending=True).plot(kind = 'bar')#計算 data 資料框中 MONTH 欄位的各個值出現的次數\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:03.301304Z","iopub.execute_input":"2024-03-11T13:01:03.302914Z","iopub.status.idle":"2024-03-11T13:01:04.324304Z","shell.execute_reply.started":"2024-03-11T13:01:03.302717Z","shell.execute_reply":"2024-03-11T13:01:04.322627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MON_COUNT = data.MONTH.value_counts().sort_index(ascending=True)\nMON_COUNT\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:04.336001Z","iopub.execute_input":"2024-03-11T13:01:04.336499Z","iopub.status.idle":"2024-03-11T13:01:04.365989Z","shell.execute_reply.started":"2024-03-11T13:01:04.336464Z","shell.execute_reply":"2024-03-11T13:01:04.364591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MON_COUNT = data['MONTH'].value_counts().sort_index(ascending=True)  # 根據 MONTH 欄位計算每個月份的計數並排序\nMON_COUNT_N = pd.DataFrame({'MONTH': MON_COUNT.index, 'COUNTS': MON_COUNT.values})  # 創建 DataFrame\nprint(MON_COUNT_N)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:04.367823Z","iopub.execute_input":"2024-03-11T13:01:04.368302Z","iopub.status.idle":"2024-03-11T13:01:04.400528Z","shell.execute_reply.started":"2024-03-11T13:01:04.368264Z","shell.execute_reply":"2024-03-11T13:01:04.398534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MON_COUNT_N","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:04.402738Z","iopub.execute_input":"2024-03-11T13:01:04.403270Z","iopub.status.idle":"2024-03-11T13:01:04.427818Z","shell.execute_reply.started":"2024-03-11T13:01:04.403212Z","shell.execute_reply":"2024-03-11T13:01:04.425865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MON_COUNT_N.MONTH = MON_COUNT_N.MONTH.astype('str')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:04.429863Z","iopub.execute_input":"2024-03-11T13:01:04.430433Z","iopub.status.idle":"2024-03-11T13:01:04.441031Z","shell.execute_reply.started":"2024-03-11T13:01:04.430386Z","shell.execute_reply":"2024-03-11T13:01:04.439474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"percentage = MON_COUNT_N['COUNTS'] / MON_COUNT_N['COUNTS'].sum()\n\n# 將 'A' 和 'B' 欄位中的資料進行配對\nresult_df = pd.DataFrame({'MONTH': MON_COUNT_N['MONTH'], 'COUNTS': MON_COUNT_N['COUNTS'], 'percentage': percentage})\nprint(result_df)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:04.443054Z","iopub.execute_input":"2024-03-11T13:01:04.443627Z","iopub.status.idle":"2024-03-11T13:01:04.470152Z","shell.execute_reply.started":"2024-03-11T13:01:04.443585Z","shell.execute_reply":"2024-03-11T13:01:04.468566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 提取資料\nmonths = result_df['MONTH']\npercentages = result_df['percentage']\n\n# 設置圖表大小\nplt.figure(figsize=(12, 6))\n\n# 繪製折線圖\nplt.plot(months, percentages, marker='o', color='b', linestyle='-')\n\n# 添加標籤和標題\nplt.xlabel('Month')\nplt.ylabel('Percentage')\nplt.title('Percentage Trend by Month')#月份的百分比趨勢\n\n# 自動調整標籤\nplt.xticks(rotation=45, ha='right')\n\n# 顯示網格線\nplt.grid(True, linestyle='--', alpha=0.7)\n\n# 顯示圖表\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:04.471849Z","iopub.execute_input":"2024-03-11T13:01:04.472401Z","iopub.status.idle":"2024-03-11T13:01:05.086835Z","shell.execute_reply.started":"2024-03-11T13:01:04.472315Z","shell.execute_reply":"2024-03-11T13:01:05.085288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base_target = train_base['target'].value_counts(normalize=True)#頻率，並將頻率標準化為百分比\ntrain_base_target","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:05.088570Z","iopub.execute_input":"2024-03-11T13:01:05.089111Z","iopub.status.idle":"2024-03-11T13:01:05.118173Z","shell.execute_reply.started":"2024-03-11T13:01:05.089062Z","shell.execute_reply":"2024-03-11T13:01:05.116792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:05.120463Z","iopub.execute_input":"2024-03-11T13:01:05.121051Z","iopub.status.idle":"2024-03-11T13:01:05.153885Z","shell.execute_reply.started":"2024-03-11T13:01:05.120995Z","shell.execute_reply":"2024-03-11T13:01:05.152126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 設定標籤和顏色\nlabels = train_base_target.index.to_list()\ncolors = ['tomato', 'dodgerblue']\n\n# 繪製圓餅圖\nplt.pie(train_base_target, labels=labels, autopct=\"%1.1f%%\", colors=colors)\nplt.title('Frequency Percentage of target (pie chart)')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:05.156311Z","iopub.execute_input":"2024-03-11T13:01:05.157906Z","iopub.status.idle":"2024-03-11T13:01:05.299031Z","shell.execute_reply.started":"2024-03-11T13:01:05.157779Z","shell.execute_reply":"2024-03-11T13:01:05.297602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25, 8))  # Adjust width and height as needed\ndata.WEEK_NUM.value_counts().sort_index(ascending=True).plot(kind='bar')\nplt.xticks(rotation=45)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:01:05.300969Z","iopub.execute_input":"2024-03-11T13:01:05.301371Z","iopub.status.idle":"2024-03-11T13:01:06.299138Z","shell.execute_reply.started":"2024-03-11T13:01:05.301317Z","shell.execute_reply":"2024-03-11T13:01:06.297963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 查看每個week_num中，\n# target中二元的數量分布狀況。","metadata":{}},{"cell_type":"code","source":"gn = sns.FacetGrid(data,col=\"WEEK_NUM\")\ngn.map(plt.hist,\"target\",bins=20)\n\n#train_base[\"date_decision\"] = pd.to_datetime(train_base[\"date_decision\"]).dt.date\n# delete redundat cols\n#del train_base[\"MONTH\"], train_base[\"WEEK_NUM\"]\n#train_base","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-03-11T13:01:06.300989Z","iopub.execute_input":"2024-03-11T13:01:06.301716Z","iopub.status.idle":"2024-03-11T13:02:10.059442Z","shell.execute_reply.started":"2024-03-11T13:01:06.301672Z","shell.execute_reply":"2024-03-11T13:02:10.057829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> #  月份&target\n> #  探討二維度時，狀況呈現與分析。","metadata":{}},{"cell_type":"code","source":"gn_M = sns.FacetGrid(data,col=\"MONTH\")\ngn_M.map(plt.hist,\"target\",bins=20)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:02:10.061917Z","iopub.execute_input":"2024-03-11T13:02:10.062379Z","iopub.status.idle":"2024-03-11T13:02:46.028478Z","shell.execute_reply.started":"2024-03-11T13:02:10.062322Z","shell.execute_reply":"2024-03-11T13:02:46.027100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 先按月份和目標變數進行分組計數\ngrouped_data_mtc = data.groupby(['MONTH', 'target']).size().reset_index(name='count')\n\n# 使用 Seaborn 繪製二維圖\nplt.figure(figsize=(10, 6))  # 設定圖形大小\nsns.barplot(data=grouped_data_mtc, x='MONTH', y='count', hue='target', palette='muted')\nplt.xlabel('MONTH')\nplt.ylabel('count')\nplt.title('target 0&1 count by MONTH')\nplt.xticks(rotation=45)  # 將x軸刻度文字旋轉45度\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:02:46.030515Z","iopub.execute_input":"2024-03-11T13:02:46.030913Z","iopub.status.idle":"2024-03-11T13:02:46.740040Z","shell.execute_reply.started":"2024-03-11T13:02:46.030877Z","shell.execute_reply":"2024-03-11T13:02:46.738503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:02:46.741935Z","iopub.execute_input":"2024-03-11T13:02:46.742480Z","iopub.status.idle":"2024-03-11T13:02:46.765819Z","shell.execute_reply.started":"2024-03-11T13:02:46.742440Z","shell.execute_reply":"2024-03-11T13:02:46.764232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.loc[(data['MONTH'] < 202004)]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:02:46.767793Z","iopub.execute_input":"2024-03-11T13:02:46.768221Z","iopub.status.idle":"2024-03-11T13:02:49.820912Z","shell.execute_reply.started":"2024-03-11T13:02:46.768183Z","shell.execute_reply":"2024-03-11T13:02:49.819528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 使用 Seaborn 的 FacetGrid\ng = sns.FacetGrid(data, col=\"MONTH\", col_wrap=2, height=4)\ng.map(sns.histplot, \"target\", bins=20)\n\n\n# 調整 x 軸範圍\ng.set(ylim=(179, 121659))  # 將 min_value 和 max_value 替換為您的資料中的最小值和最大值\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:02:49.823169Z","iopub.execute_input":"2024-03-11T13:02:49.823721Z","iopub.status.idle":"2024-03-11T13:03:30.377222Z","shell.execute_reply.started":"2024-03-11T13:02:49.823672Z","shell.execute_reply":"2024-03-11T13:03:30.376113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 使用 groupby 函數按月份和目標變數分組\nd_mt_p = data.groupby(['MONTH', 'target'])\n\n# 計算每個組合的計數\ncounts_mt = d_mt_p.size().unstack()\n\nprint(counts_mt)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:30.378937Z","iopub.execute_input":"2024-03-11T13:03:30.379894Z","iopub.status.idle":"2024-03-11T13:03:30.482850Z","shell.execute_reply.started":"2024-03-11T13:03:30.379855Z","shell.execute_reply":"2024-03-11T13:03:30.481586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 使用 groupby 函數按月份和目標變數分組\nd_mt_p = data.groupby(['MONTH', 'target'])\n\n# 計算每個組合的計數\ncounts_mt = d_mt_p.size().unstack()\n\n# 計算每個組合的0和1的占比\nproportions = counts_mt.div(counts_mt.sum(axis=1), axis=0)\n\nprint(proportions)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:30.484696Z","iopub.execute_input":"2024-03-11T13:03:30.485895Z","iopub.status.idle":"2024-03-11T13:03:30.580036Z","shell.execute_reply.started":"2024-03-11T13:03:30.485847Z","shell.execute_reply":"2024-03-11T13:03:30.578982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"proportions","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:30.581909Z","iopub.execute_input":"2024-03-11T13:03:30.582297Z","iopub.status.idle":"2024-03-11T13:03:30.597801Z","shell.execute_reply.started":"2024-03-11T13:03:30.582263Z","shell.execute_reply":"2024-03-11T13:03:30.596444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> #  檢查變數值","metadata":{}},{"cell_type":"code","source":"variable_names = data.columns.tolist()\nlen(variable_names)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:30.600166Z","iopub.execute_input":"2024-03-11T13:03:30.601473Z","iopub.status.idle":"2024-03-11T13:03:30.612127Z","shell.execute_reply.started":"2024-03-11T13:03:30.601413Z","shell.execute_reply":"2024-03-11T13:03:30.611077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 設定圖形大小\nplt.figure(figsize=(10, 6))\n\n# 提取月份和目標變數0和1的占比資料\nmonths = proportions.index.astype(str)  # 將月份轉換為字串格式\ntarget_0 = proportions[0]\ntarget_1 = proportions[1]\n\n# 繪製折線圖\nplt.plot(months, target_0, marker='o', label='Target 0')\nplt.plot(months, target_1, marker='o', label='Target 1')\n\n\n# 圖表標題和軸標籤\nplt.title(' Percentage Trend by Month')\nplt.xlabel('target')\nplt.ylabel('Percentage ')\n\n# 顯示圖例\nplt.legend(title='target')\nplt.xticks(rotation=45) # 旋轉 x 軸刻度標籤\nplt.grid(True)\nplt.tight_layout()\nplt.show()\n\n#目標變數為 0 和 1 的值，並以折線圖的方式顯示它們在不同月份下的占比情況。","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:30.613698Z","iopub.execute_input":"2024-03-11T13:03:30.614523Z","iopub.status.idle":"2024-03-11T13:03:31.121336Z","shell.execute_reply.started":"2024-03-11T13:03:30.614482Z","shell.execute_reply":"2024-03-11T13:03:31.120111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.loc[(data['MONTH'] == 202009)]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.123117Z","iopub.execute_input":"2024-03-11T13:03:31.123502Z","iopub.status.idle":"2024-03-11T13:03:31.255938Z","shell.execute_reply.started":"2024-03-11T13:03:31.123470Z","shell.execute_reply":"2024-03-11T13:03:31.254609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_categories = data.loc[data['MONTH'] == 202005, 'target'].value_counts().index.tolist()\nprint(\"變數'target'在202005月份的類別有：\", target_categories)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.257573Z","iopub.execute_input":"2024-03-11T13:03:31.258019Z","iopub.status.idle":"2024-03-11T13:03:31.269656Z","shell.execute_reply.started":"2024-03-11T13:03:31.257928Z","shell.execute_reply":"2024-03-11T13:03:31.268201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_categories = data.loc[data['MONTH'] == 202004, 'target'].value_counts().index.tolist()\nprint(\"變數'target'在202004月份的類別有：\", target_categories)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.271507Z","iopub.execute_input":"2024-03-11T13:03:31.272290Z","iopub.status.idle":"2024-03-11T13:03:31.284153Z","shell.execute_reply.started":"2024-03-11T13:03:31.272251Z","shell.execute_reply":"2024-03-11T13:03:31.282705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_categories = data.loc[data['MONTH'] == 202010, 'target'].value_counts().index.tolist()\nprint(\"變數'target'在202010月份的類別有：\", target_categories)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.286076Z","iopub.execute_input":"2024-03-11T13:03:31.286478Z","iopub.status.idle":"2024-03-11T13:03:31.299313Z","shell.execute_reply.started":"2024-03-11T13:03:31.286444Z","shell.execute_reply":"2024-03-11T13:03:31.297701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_categories = data.loc[data['MONTH'] == 202009, 'target'].value_counts().index.tolist()\nprint(\"變數'target'在202009月份的類別有：\", target_categories)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.301401Z","iopub.execute_input":"2024-03-11T13:03:31.301954Z","iopub.status.idle":"2024-03-11T13:03:31.316599Z","shell.execute_reply.started":"2024-03-11T13:03:31.301902Z","shell.execute_reply":"2024-03-11T13:03:31.314736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_categories = data.loc[data['MONTH'] == 202004, 'WEEK_NUM'].value_counts().index.tolist()\nprint(\"變數'WEEK_NUM'在202004月份的類別有：\", target_categories)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.318404Z","iopub.execute_input":"2024-03-11T13:03:31.318821Z","iopub.status.idle":"2024-03-11T13:03:31.331321Z","shell.execute_reply.started":"2024-03-11T13:03:31.318787Z","shell.execute_reply":"2024-03-11T13:03:31.329636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.loc[data['MONTH'] == 202009, 'target']","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.333572Z","iopub.execute_input":"2024-03-11T13:03:31.334119Z","iopub.status.idle":"2024-03-11T13:03:31.350764Z","shell.execute_reply.started":"2024-03-11T13:03:31.334071Z","shell.execute_reply":"2024-03-11T13:03:31.349171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.352792Z","iopub.execute_input":"2024-03-11T13:03:31.354211Z","iopub.status.idle":"2024-03-11T13:03:31.371913Z","shell.execute_reply.started":"2024-03-11T13:03:31.354152Z","shell.execute_reply":"2024-03-11T13:03:31.370591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 提取月份和週期資料\nmonths = train_base['MONTH'].astype(str)\ncycles = train_base['WEEK_NUM']\n\n# 繪製散布圖\nplt.figure(figsize=(10, 6))\nplt.scatter(cycles, months, c=train_base['target'], cmap='coolwarm', alpha=0.8, s=100, edgecolors='k')\nplt.colorbar(label='Target Variable')\nplt.xlabel('Cycle')\nplt.ylabel('Month')\nplt.title('Scatter Plot of Cycle vs Month with Target Variable')\nplt.xticks(rotation=45)\nplt.grid(True, linestyle='--', alpha=0.5)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:03:31.373890Z","iopub.execute_input":"2024-03-11T13:03:31.374786Z","iopub.status.idle":"2024-03-11T13:04:10.883942Z","shell.execute_reply.started":"2024-03-11T13:03:31.374737Z","shell.execute_reply":"2024-03-11T13:04:10.882539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 察看資料中目標變數=1時，data的情況","metadata":{}},{"cell_type":"code","source":"data.loc[data['target']==1]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:10.886210Z","iopub.execute_input":"2024-03-11T13:04:10.886728Z","iopub.status.idle":"2024-03-11T13:04:11.347739Z","shell.execute_reply.started":"2024-03-11T13:04:10.886677Z","shell.execute_reply":"2024-03-11T13:04:11.346488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(data.columns.values)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:11.349387Z","iopub.execute_input":"2024-03-11T13:04:11.349756Z","iopub.status.idle":"2024-03-11T13:04:11.355332Z","shell.execute_reply.started":"2024-03-11T13:04:11.349724Z","shell.execute_reply":"2024-03-11T13:04:11.353925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 查看每個欄位的缺失值數量","metadata":{}},{"cell_type":"code","source":"# 查看每個欄位的缺失值數量\nmissing_values = data.isnull().sum()\nprint(missing_values)\n\n# 如果需要，可以進行缺失值填補，例如使用中位數\n#data = data.fillna(data.median())\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:11.357021Z","iopub.execute_input":"2024-03-11T13:04:11.357463Z","iopub.status.idle":"2024-03-11T13:04:16.658143Z","shell.execute_reply.started":"2024-03-11T13:04:11.357426Z","shell.execute_reply":"2024-03-11T13:04:16.656622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:16.659711Z","iopub.execute_input":"2024-03-11T13:04:16.660069Z","iopub.status.idle":"2024-03-11T13:04:16.668267Z","shell.execute_reply.started":"2024-03-11T13:04:16.660038Z","shell.execute_reply":"2024-03-11T13:04:16.666751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# NAN在每個特徵所佔的比例\n#print(\"---------\")\nmissing_values_rate = data.isnull().sum()/data.shape[0]\nprint(data.isnull().sum()/data.shape[0])","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:16.670067Z","iopub.execute_input":"2024-03-11T13:04:16.670489Z","iopub.status.idle":"2024-03-11T13:04:27.150927Z","shell.execute_reply.started":"2024-03-11T13:04:16.670455Z","shell.execute_reply":"2024-03-11T13:04:27.149665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#missing_values_rate.index.to_list()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:27.152599Z","iopub.execute_input":"2024-03-11T13:04:27.153808Z","iopub.status.idle":"2024-03-11T13:04:27.158848Z","shell.execute_reply.started":"2024-03-11T13:04:27.153761Z","shell.execute_reply":"2024-03-11T13:04:27.157652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" data.MONTH.value_counts().sort_index(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:27.160468Z","iopub.execute_input":"2024-03-11T13:04:27.160900Z","iopub.status.idle":"2024-03-11T13:04:27.192735Z","shell.execute_reply.started":"2024-03-11T13:04:27.160867Z","shell.execute_reply":"2024-03-11T13:04:27.191695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nm_labels = missing_values_rate.index.to_list()\ncolors = ['tomato', 'dodgerblue']\n\n# 繪製圓餅圖\nplt.pie(missing_values_rate, labels=m_labels, autopct=\"%1.1f%%\", colors=colors)\nplt.title('Frequency Percentage of target (pie chart)')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:27.195072Z","iopub.execute_input":"2024-03-11T13:04:27.196173Z","iopub.status.idle":"2024-03-11T13:04:29.254285Z","shell.execute_reply.started":"2024-03-11T13:04:27.196119Z","shell.execute_reply":"2024-03-11T13:04:29.250830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 察看所有變數特徵的定義","metadata":{}},{"cell_type":"code","source":"feature_definitions = pd.read_csv('/kaggle/input/home-credit-credit-risk-model-stability/feature_definitions.csv')\nfeature_definitions","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:29.255979Z","iopub.execute_input":"2024-03-11T13:04:29.256379Z","iopub.status.idle":"2024-03-11T13:04:29.278743Z","shell.execute_reply.started":"2024-03-11T13:04:29.256334Z","shell.execute_reply":"2024-03-11T13:04:29.277387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 察看資料的資訊","metadata":{}},{"cell_type":"code","source":"feature_definitions.info()\nprint('-------------------------------------------------------------------')\ntrain_base.info()\nprint('-------------------------------------------------------------------')\ntest_base.info()\nprint('-------------------------------------------------------------------')\ntrain_sta_0_0.info()#validfrom_1069D自客戶開展活動以來的日期。\nprint('-------------------------------------------------------------------')\ntrain_sta_0_1.info()#validfrom_1069D自客戶開展活動以來的日期。\nprint('-------------------------------------------------------------------')\n#train_sta_cb_0.info()\n#print('-------------------------------------------------------------------')\n#train_tax_registry_c_1.info()#processingdate_168D處理稅款扣除的日期。\n#print('-------------------------------------------------------------------')\n#train_person_1.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:29.280806Z","iopub.execute_input":"2024-03-11T13:04:29.281283Z","iopub.status.idle":"2024-03-11T13:04:29.508043Z","shell.execute_reply.started":"2024-03-11T13:04:29.281222Z","shell.execute_reply":"2024-03-11T13:04:29.506650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  檢視data中，非數值欄位","metadata":{}},{"cell_type":"code","source":"data_ob = data.dtypes[data.dtypes=='object']\nlen(data_ob)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:29.509995Z","iopub.execute_input":"2024-03-11T13:04:29.510470Z","iopub.status.idle":"2024-03-11T13:04:29.519470Z","shell.execute_reply.started":"2024-03-11T13:04:29.510428Z","shell.execute_reply":"2024-03-11T13:04:29.518437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 列出所有類別型變數\ncategorical_columns = data.select_dtypes(include=['object']).columns.tolist()\n\n# 印出類別型變數\nprint(categorical_columns)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:29.521260Z","iopub.execute_input":"2024-03-11T13:04:29.521801Z","iopub.status.idle":"2024-03-11T13:04:33.925774Z","shell.execute_reply.started":"2024-03-11T13:04:29.521753Z","shell.execute_reply":"2024-03-11T13:04:33.924388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 列出所有bool變數\ncategorical_columns_bl = data.select_dtypes(include=['bool']).columns.tolist()\n\n# 印出bool變數\nprint(categorical_columns_bl)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:33.927284Z","iopub.execute_input":"2024-03-11T13:04:33.927823Z","iopub.status.idle":"2024-03-11T13:04:33.936882Z","shell.execute_reply.started":"2024-03-11T13:04:33.927788Z","shell.execute_reply":"2024-03-11T13:04:33.935395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['isbidproduct_1095L']","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:33.938742Z","iopub.execute_input":"2024-03-11T13:04:33.939157Z","iopub.status.idle":"2024-03-11T13:04:33.952489Z","shell.execute_reply.started":"2024-03-11T13:04:33.939124Z","shell.execute_reply":"2024-03-11T13:04:33.951172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['isbidproduct_1095L'].info()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:33.956534Z","iopub.execute_input":"2024-03-11T13:04:33.956966Z","iopub.status.idle":"2024-03-11T13:04:33.974726Z","shell.execute_reply.started":"2024-03-11T13:04:33.956934Z","shell.execute_reply":"2024-03-11T13:04:33.972961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(categorical_columns)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:33.976466Z","iopub.execute_input":"2024-03-11T13:04:33.976877Z","iopub.status.idle":"2024-03-11T13:04:33.990002Z","shell.execute_reply.started":"2024-03-11T13:04:33.976843Z","shell.execute_reply":"2024-03-11T13:04:33.988628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_describe = data.describe(include=['O'])#檢視非數值欄位#0層有布林值\ndata_describe","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:33.992043Z","iopub.execute_input":"2024-03-11T13:04:33.992613Z","iopub.status.idle":"2024-03-11T13:04:50.325387Z","shell.execute_reply.started":"2024-03-11T13:04:33.992570Z","shell.execute_reply":"2024-03-11T13:04:50.324144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(data_describe)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:50.327033Z","iopub.execute_input":"2024-03-11T13:04:50.327860Z","iopub.status.idle":"2024-03-11T13:04:50.335085Z","shell.execute_reply.started":"2024-03-11T13:04:50.327823Z","shell.execute_reply":"2024-03-11T13:04:50.334047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 找data的所有類別型變數，做成表格。","metadata":{}},{"cell_type":"code","source":"# 獲取每個欄位的唯一類別數\nunique_categories = {}\nfor col in categorical_columns:\n  unique_categories[col] = [data[col].nunique()]\n#  unique_categories[col] = data[col].nunique()\n# 輸出結果\nprint(f\"類別型欄位數: {len(categorical_columns)}\")\nprint(f\"每個欄位的唯一類別數: {unique_categories}\")","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:50.336658Z","iopub.execute_input":"2024-03-11T13:04:50.337120Z","iopub.status.idle":"2024-03-11T13:04:55.188714Z","shell.execute_reply.started":"2024-03-11T13:04:50.337089Z","shell.execute_reply":"2024-03-11T13:04:55.187377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_categories","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.190721Z","iopub.execute_input":"2024-03-11T13:04:55.191504Z","iopub.status.idle":"2024-03-11T13:04:55.201700Z","shell.execute_reply.started":"2024-03-11T13:04:55.191457Z","shell.execute_reply":"2024-03-11T13:04:55.200544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://haosquare.com/python-pandas-wide-and-long-data/\nunique_categories_count = pd.DataFrame(unique_categories)\nunique_categories_count = unique_categories_count.melt()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.204664Z","iopub.execute_input":"2024-03-11T13:04:55.205124Z","iopub.status.idle":"2024-03-11T13:04:55.221432Z","shell.execute_reply.started":"2024-03-11T13:04:55.205090Z","shell.execute_reply":"2024-03-11T13:04:55.220194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_categories_count","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.222940Z","iopub.execute_input":"2024-03-11T13:04:55.223397Z","iopub.status.idle":"2024-03-11T13:04:55.242047Z","shell.execute_reply.started":"2024-03-11T13:04:55.223337Z","shell.execute_reply":"2024-03-11T13:04:55.240808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> #  離散型變數之類別統計量","metadata":{}},{"cell_type":"code","source":"data['datelastinstal40dpd_247D']","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.244016Z","iopub.execute_input":"2024-03-11T13:04:55.245116Z","iopub.status.idle":"2024-03-11T13:04:55.259975Z","shell.execute_reply.started":"2024-03-11T13:04:55.245060Z","shell.execute_reply":"2024-03-11T13:04:55.258752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_categories_count.describe()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.262324Z","iopub.execute_input":"2024-03-11T13:04:55.262762Z","iopub.status.idle":"2024-03-11T13:04:55.280876Z","shell.execute_reply.started":"2024-03-11T13:04:55.262727Z","shell.execute_reply":"2024-03-11T13:04:55.279613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sns.barplot(x = 'datefirstoffer_1144D',\n#                                y = 'target',\n#                                data = data)\n#plt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.295332Z","iopub.execute_input":"2024-03-11T13:04:55.296210Z","iopub.status.idle":"2024-03-11T13:04:55.301451Z","shell.execute_reply.started":"2024-03-11T13:04:55.296161Z","shell.execute_reply":"2024-03-11T13:04:55.300277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unique_categories_count.plot(kind = 'barh',\n#                                                 figsize = (2,  1000),\n#                                                 legend = True)\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.303205Z","iopub.execute_input":"2024-03-11T13:04:55.303689Z","iopub.status.idle":"2024-03-11T13:04:55.311588Z","shell.execute_reply.started":"2024-03-11T13:04:55.303649Z","shell.execute_reply":"2024-03-11T13:04:55.310535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#x= 'date_decision' ,y='WEEK_NUM', hue = 'target'\n#sns.scatterplot(x= 'date_decision' ,y='WEEK_NUM', hue = 'target', data = data)\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.313459Z","iopub.execute_input":"2024-03-11T13:04:55.314428Z","iopub.status.idle":"2024-03-11T13:04:55.321712Z","shell.execute_reply.started":"2024-03-11T13:04:55.314380Z","shell.execute_reply":"2024-03-11T13:04:55.320428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" ># summary","metadata":{}},{"cell_type":"code","source":"summary_stats = data.describe()\nprint(summary_stats)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:04:55.323151Z","iopub.execute_input":"2024-03-11T13:04:55.323551Z","iopub.status.idle":"2024-03-11T13:05:07.656310Z","shell.execute_reply.started":"2024-03-11T13:04:55.323516Z","shell.execute_reply":"2024-03-11T13:05:07.654912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" ># 觀察各個變數的唯一值","metadata":{}},{"cell_type":"code","source":"data['bankacctype_710L'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:07.658394Z","iopub.execute_input":"2024-03-11T13:05:07.658933Z","iopub.status.idle":"2024-03-11T13:05:07.779816Z","shell.execute_reply.started":"2024-03-11T13:05:07.658885Z","shell.execute_reply":"2024-03-11T13:05:07.778247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['datefirstoffer_1144D'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:07.782082Z","iopub.execute_input":"2024-03-11T13:05:07.782641Z","iopub.status.idle":"2024-03-11T13:05:07.910985Z","shell.execute_reply.started":"2024-03-11T13:05:07.782593Z","shell.execute_reply":"2024-03-11T13:05:07.909754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['disbursementtype_67L'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:07.912657Z","iopub.execute_input":"2024-03-11T13:05:07.913186Z","iopub.status.idle":"2024-03-11T13:05:08.053952Z","shell.execute_reply.started":"2024-03-11T13:05:07.913027Z","shell.execute_reply":"2024-03-11T13:05:08.052572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['datelastinstal40dpd_247D'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:08.055486Z","iopub.execute_input":"2024-03-11T13:05:08.056660Z","iopub.status.idle":"2024-03-11T13:05:08.190507Z","shell.execute_reply.started":"2024-03-11T13:05:08.056619Z","shell.execute_reply":"2024-03-11T13:05:08.189393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_describe = data.drop(['target'], axis = 1).describe(include='all')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:08.191928Z","iopub.execute_input":"2024-03-11T13:05:08.192390Z","iopub.status.idle":"2024-03-11T13:05:35.025690Z","shell.execute_reply.started":"2024-03-11T13:05:08.192360Z","shell.execute_reply":"2024-03-11T13:05:35.024408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:35.027754Z","iopub.execute_input":"2024-03-11T13:05:35.028722Z","iopub.status.idle":"2024-03-11T13:05:35.044570Z","shell.execute_reply.started":"2024-03-11T13:05:35.028672Z","shell.execute_reply":"2024-03-11T13:05:35.043287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:35.046160Z","iopub.execute_input":"2024-03-11T13:05:35.046615Z","iopub.status.idle":"2024-03-11T13:05:35.233948Z","shell.execute_reply.started":"2024-03-11T13:05:35.046579Z","shell.execute_reply":"2024-03-11T13:05:35.232568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_describe","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:35.235454Z","iopub.execute_input":"2024-03-11T13:05:35.235819Z","iopub.status.idle":"2024-03-11T13:05:35.279872Z","shell.execute_reply.started":"2024-03-11T13:05:35.235786Z","shell.execute_reply":"2024-03-11T13:05:35.278557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_df1 = data.groupby(['type','MONTH']).count().reset_index().rename(columns={'case_id':'count'})[['MONTH','count','type']]","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:35.281800Z","iopub.execute_input":"2024-03-11T13:05:35.282162Z","iopub.status.idle":"2024-03-11T13:05:41.626448Z","shell.execute_reply.started":"2024-03-11T13:05:35.282131Z","shell.execute_reply":"2024-03-11T13:05:41.624563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_df1","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:05:41.628533Z","iopub.execute_input":"2024-03-11T13:05:41.629495Z","iopub.status.idle":"2024-03-11T13:05:41.646547Z","shell.execute_reply.started":"2024-03-11T13:05:41.629444Z","shell.execute_reply":"2024-03-11T13:05:41.644971Z"},"trusted":true},"execution_count":null,"outputs":[]}]}