カテゴリ変数の変換

HRアナリティクス:Pythonで従業員離職を予測する

Hrant Davtyan

Assistant Professor of Data Science American University of Armenia

カテゴリ変数の種類

  • 順序型 - 順序付けできる2つ以上のカテゴリを持つ変数
    • 例: salary
    • 値: low, medium, high
  • 名義型 - 固有の順序を持たない2つ以上のカテゴリを持つ変数
    • 例: department
    • 値: sales, accounting, hr, technical, support, management, IT, product_mng, marketing, RandD
HRアナリティクス: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アナリティクス:Pythonで従業員離職を予測する

ダミー変数の作成

# 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アナリティクス:Pythonで従業員離職を予測する

ダミー変数トラップ

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で従業員離職を予測する

練習しましょう!

HRアナリティクス:Pythonで従業員離職を予測する

Preparing Video For Download...