One-Hot-Encoding ตัวแปรเชิงหมวดหมู่

Supervised Learning ใน R: การถดถอย

Nina Zumel and John Mount

Win-Vector, LLC

ทำไมต้องแปลงตัวแปรเชิงหมวดหมู่เอง?

  • ฟังก์ชัน R ส่วนใหญ่จัดการการแปลงให้อัตโนมัติ
    • model.matrix()
  • xgboost() ไม่ได้ทำให้
    • ต้องแปลงตัวแปรเชิงหมวดหมู่เป็นตัวเลขเอง
  • การแปลงเป็น indicator: one-hot encoding
Supervised Learning ใน R: การถดถอย

One-hot-encoding และการทำความสะอาดข้อมูลด้วย `vtreat`

แนวคิดพื้นฐาน:

  • designTreatmentsZ() เพื่อออกแบบ treatment plan จากข้อมูล training แล้ว
  • prepare() เพื่อสร้างข้อมูล "clean"
    • เป็นตัวเลขทั้งหมด
    • ไม่มีค่าที่หายไป
      • ใช้ prepare() ร่วมกับ treatment plan สำหรับข้อมูลในอนาคต
Supervised Learning ใน R: การถดถอย

ตัวอย่างการใช้ vtreat เบื้องต้น

ข้อมูล Training

x u y
one 44 0.4855671
two 24 1.3683726
three 66 2.0352837
two 22 1.6396267

ข้อมูล Test

x u y
one 5 2.6488148
three 12 1.5012938
one 56 0.1993731
two 28 1.2778516
Supervised Learning ใน R: การถดถอย

สร้าง Treatment Plan

vars <- c("x", "u")
treatplan <- designTreatmentsZ(dframe, varslist, verbose = FALSE)

อินพุตของ designTreatmentsZ()

  • dframe: ข้อมูล training
  • varlist: รายชื่อตัวแปรอินพุต
  • ตั้ง verbose = FALSE เพื่อซ่อนข้อความแสดงความคืบหน้า
Supervised Learning ใน R: การถดถอย

ดึงตัวแปรใหม่

scoreFrame อธิบายการแมปและประเภทของตัวแปร

(scoreFrame <- treatplan$scoreFrame %>% 
     select(varName, origName, code))
        varName origName  code
1   x_lev_x.one        x   lev
2 x_lev_x.three        x   lev
3   x_lev_x.two        x   lev
4        x_catP        x  catP
5       u_clean        u clean

ดึงชื่อตัวแปร lev และ clean ใหม่

(newvars <- scoreFrame %>% 
     filter(code %in% c("clean", "lev")) %>%
     use_series(varName))
"x_lev_x.one"   "x_lev_x.three" "x_lev_x.two"   "u_clean"
Supervised Learning ใน R: การถดถอย

เตรียมข้อมูล Training สำหรับการสร้างโมเดล

training.treat <- prepare(treatmentplan, dframe, varRestriction = newvars)

อินพุตของ prepare():

  • treatmentplan: treatment plan
  • dframe: data frame
  • varRestriction: รายชื่อตัวแปรที่ต้องการ prepare (ไม่บังคับ)
    • ค่าเริ่มต้น: prepare ตัวแปรทั้งหมด
Supervised Learning ใน R: การถดถอย

ข้อมูลก่อนและหลัง Treatment

ข้อมูล Training

x u y
one 44 0.4855671
two 24 1.3683726
three 66 2.0352837
two 22 1.6396267

ข้อมูล Training หลัง Treatment

x_lev _x. one x_lev _x. three x_lev _x. two u_clean
1 0 0 44
0 0 1 24
0 1 0 66
0 0 1 22
Supervised Learning ใน R: การถดถอย

เตรียมข้อมูล Test ก่อนนำโมเดลไปใช้

(test.treat <- prepare(treatplan, test, varRestriction = newvars))
  x_lev_x.one x_lev_x.three x_lev_x.two u_clean
1           1             0           0       5
2           0             1           0      12
3           1             0           0      56
4           0             0           1      28
Supervised Learning ใน R: การถดถอย

vtreat รองรับกรณีที่ไม่คาดคิด

ระดับ x ที่ไม่เคยพบมาก่อน: four

x u y
one 4 0.2331301
two 14 1.9331760
three 66 3.1251029
four 25 4.0332491

four เข้ารหัสเป็น (0, 0, 0)

prepare(treatplan, toomany, ...)
x_lev _x. one x_lev _x. three x_lev _x. two u_clean
1 0 0 4
0 0 1 14
0 1 0 66
0 0 0 25
Supervised Learning ใน R: การถดถอย

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

Supervised Learning ใน R: การถดถอย

Preparing Video For Download...