HR 分析:用 Python 預測員工流失
Hrant Davtyan
Assistant Professor of Data Science American University of Armenia
# 將 "salary" 欄位型別改為 categorical
data.salary = data.salary.astype('category')
# 指定類別的正確順序
data.salary = data.salary.cat.reorder_categories(['low',
'medium',
'high'])
# 以整數編碼各類別
data.salary = data.salary.cat.codes
| Old values | New values |
|---|---|
| low | 0 |
| medium | 1 |
| high | 2 |
# 產生虛擬變數,並存成新的 DataFrame
departments = pd.get_dummies(data.department)
範例輸出:
IT RandD accounting hr management marketing product_mng sales support technical
0 0 0 0 0 0 0 0 0 0 1
departments.head()
IT RandD accounting hr management marketing product_mng sales support technical
0 0 0 0 0 0 0 0 0 0 1
departments = departments.drop("technical", axis = 1)
departments.head()
IT RandD accounting hr management marketing product_mng sales support
0 0 0 0 0 0 0 0 0 0
HR 分析:用 Python 預測員工流失