การแปลงตัวแปรเชิงกลุ่ม

HR Analytics: การพยากรณ์การลาออกของพนักงานด้วย Python

Hrant Davtyan

Assistant Professor of Data Science American University of Armenia

ประเภทของตัวแปรเชิงกลุ่ม

  • Ordinal - ตัวแปรที่มีสองกลุ่มขึ้นไปและสามารถจัดลำดับได้
    • ตัวอย่าง: salary
    • ค่า: low, medium, high
  • Nominal - ตัวแปรที่มีสองกลุ่มขึ้นไปแต่ไม่มีลำดับโดยธรรมชาติ
    • ตัวอย่าง: department
    • ค่า: sales, accounting, hr, technical, support, management, IT, product_mng, marketing, RandD
HR Analytics: การพยากรณ์การลาออกของพนักงานด้วย Python

การเข้ารหัสกลุ่ม (salary)

# Change the type of the "salary" column to categorical
data.salary = data.salary.astype('category')     
# Provide the correct order of categories
data.salary = data.salary.cat.reorder_categories(['low',
                                                  'medium',
                                                  'high'])
# Encode categories with integer values
data.salary = data.salary.cat.codes
ค่าเดิม ค่าใหม่
low 0
medium 1
high 2
HR Analytics: การพยากรณ์การลาออกของพนักงานด้วย Python

การสร้าง dummy variables

# Get dummies and save them inside a new 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
HR Analytics: การพยากรณ์การลาออกของพนักงานด้วย Python

Dummy trap

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 Analytics: การพยากรณ์การลาออกของพนักงานด้วย Python

มาฝึกกันเถอะ!

HR Analytics: การพยากรณ์การลาออกของพนักงานด้วย Python

Preparing Video For Download...