# 訓練一階段敗血症模型
###### tags: `國泰專案`
## 安裝
```
pip install -r requirements.txt
```
## 資料
1. 將 [`CGH_data`](https://drive.google.com/drive/u/0/folders/1a2nRBd_1R6K9SE27N8VdMB_xuZmJW2M7) 資料夾放到指定路徑
2. 根據 `CGH_data` 所在路徑,修改 `configs/base.py` 中資料的路徑
+ **第 7 - 14 行**
```python
# input data
path_list_dict = {
(division, year): [
f"./CGH_data/{division}-{year}-去個資/{division}-{year}-去個資/{year}Sepsis00101.csv",
f"./CGH_data/{division}-{year}-去個資/{division}-{year}-去個資/{year}Sepsis00102.csv",
f"./CGH_data/{division}-{year}-去個資/{division}-{year}-去個資/{year}Sepsis00103.csv",
f"./CGH_data/{division}-{year}-去個資/{division}-{year}-去個資/{year}Sepsis00110.csv"
] for division, year in product(["總院", "新竹", "汐止"], ["2020", "2021"])
}
```
+ **第 51-54 行**
```python
label_df_path_dict = {
(division, year): f"./CGH_data/{division}-{year}-去個資/{division}-{year}-去個資/{year}Sepsis00104.csv"
for division, year in product(["總院", "新竹", "汐止"], ["2020", "2021"])
}
```
## 訓練模型
1. **修改** `configs/retrain_config.py`
+ **`configs/retrain_config.py` 參數意義**
+ `train_divisions`: 訓練所在院區
+ `val_divisions`: 測試所在院區
+ 1: 汐止
+ 3: 新竹
+ 4: 總院
+ `start`: 測試資料起始時間
+ `end`: 測試資料終止時間
+ `period`: retrain 的週期(以月為單位)。`period=1`,表示每個月都會重新訓練模型,`period=12` 表示一年重新訓練一次模型
+ `window_size`: 訓練資料的時間跨度(以月為單位)
+ `exclude_col`: 訓練時要移除的欄位
+ `y_col`: 標籤所在欄位
+ `model_type`: 訓練所使用的模型
+ `task`: 任務類型
+ **定期訓練(一年訓練一次)**
```python
from sklearn.metrics import make_scorer, fbeta_score
from .base_config import *
## data filter
train_divisions = ['1', '3', '4'] # 訓練所在院區
val_divisions = ['1'] # 測試所在院區
filter_config_list = [
{
'train_division': train_divisions,
'val_division': val_divisions,
'start': '202101',
'end':'202112',
'period': 12,
'window_size': 12
}
]
## da_model
trainer_config = {
'exclude_col': ['DIVISIONNO', 'INTIME', 'ACCOUNTNO', 'ISTRANSFER'],
'y_col': 'SEPSIS',
'model_type': 'XGB',
'task': 'classification',
'encoder': 'label',
'partial_oh_dict': dict(),
'normalize': False,
'auto_fill': True,
'scorer': make_scorer(fbeta_score , beta=3)
}
```
+ **每月訓練**
```python!
from sklearn.metrics import make_scorer, fbeta_score
from .base_config import *
## data filter
train_divisions = ['1', '3', '4'] # 訓練所在院區
val_divisions = ['1'] # 測試所在院區
filter_config_list = [
{
'train_division': train_divisions,
'val_division': val_divisions,
'start': '202101',
'end':'202112',
'period': 1,
'window_size': 12
}
]
## da_model
trainer_config = {
'exclude_col': ['DIVISIONNO', 'INTIME', 'ACCOUNTNO', 'ISTRANSFER'],
'y_col': 'SEPSIS',
'model_type': 'XGB',
'task': 'classification',
'encoder': 'label',
'partial_oh_dict': dict(),
'normalize': False,
'auto_fill': True,
'scorer': make_scorer(fbeta_score , beta=3)
}
```
2. **執行指令**
```python
python run.py
```
:::info
為確保程式有正常運作,可確認
1. 執行後有產生 `aggregate_data.csv` 檔,代表程式有把不同資料表的資料 join 起來,並做資料清洗
2. 執行後會出現以下顯示,代表正在訓練一個模型,如果使用每月訓練模式,最終會出現 12 則顯示;使用定期訓練模型則會出現 1 則顯示

:::
3. **後續**
+ 模型權重會放在 `model_data/` 資料夾
:::warning
新的權重檔會把過去的權重檔洗掉!!!!!!!!
:::
+ `report/confusion_table`: 存放測試期間內每個月的 confusion table
+ `report/feature_importance`: 存放特徵重要性的圖表
:::info
每訓練一次模型就會產生一個特徵重要性圖,換言之,每月訓練會訓練 12 個模型,所以會產生 12 張特徵重要性圖
:::
+ `report/{train_divisions}_{val_divisions}_{start}_{end}_{period}_{window_size}.csv`: 存放實驗結果,可確認實驗表現是否與之前一樣(不要差太多就好了,因為隨機種子不一樣)
+ 定期訓練

+ 每月訓練

## 參考
1. [一階段敗血症專案報告]( https://docs.google.com/presentation/d/1R1OEp8b3jZmPBng4NsSK3wQFajrUP2sCVPHQ0vQ-0mE/edit#slide=id.g13c632321bb_0_0)