Feature hashing

Feature Engineering in R

Jorge Zazueta

Research Professor and Head of the Modeling Group at the School of Economics, UASLP

Feature hashing คืออะไร?

  • แปลงตัวแปรข้อความเป็นชุดตัวแปรเชิงตัวเลข
  • ใช้ hash values เป็นดัชนีฟีเจอร์
  • แทนข้อมูลด้วยหน่วยความจำที่ต่ำ
  • ช่วยได้เมื่อคาดว่าจะมีหมวดหมู่ใหม่เมื่อรับข้อมูลใหม่

กำหนดหมายเลขดัชนีให้แต่ละสายการบินตามค่าข้อความ

ตารางแสดง feature hashing ของสายการบิน

Feature Engineering in R

มีสายการบินกี่สาย?

ชุดข้อมูล flights มีสายการบินในรูปแบบ factor แต่ยังไม่ทราบว่าจะมีสายการบินใหม่ปรากฏในข้อมูลใหม่หรือไม่

flights %>%
  select(carrier) %>%
  table()
carrier
  9E   AA   AS   B6   DL   EV   F9   FL   HA   MQ   OO   UA   US   VX   WN   YV 
 859 1744   26 2503 2619 3014   38  186   14 1540    2 3367 1228  244  757   41
Feature Engineering in R

มาทำ hashing กับฟีเจอร์นี้กัน

สร้าง dummy hash เพื่อแทนค่า factor โดยใช้แพ็กเกจ textrecipes

recipe <- recipe(~carrier, 
                 data = flights_train) %>%
  step_dummy_hash(carrier, prefix = NULL, 
                  signed = FALSE, 
                  num_terms = 50L)
# Prep the recipe
object <- recipe %>%
  prep()

# Bake the recipe object with new data
baked <- bake(object,
              new_data = flights_test)

ดูผลลัพธ์จาก step_dummy_hash()

bind_cols(flights_test$carrier,baked)[1:6,c(1,18:20)]
New names:
• `` -> `...1`
# A tibble: 10 × 4
   ...1  `_carrier_17` `_carrier_18` `_carrier_19`
   <chr>         <int>         <int>         <int>
 1 EV                0             0             0
 2 B6                0             1             0
 3 EV                0             0             0
 4 MQ                0             0             0
 5 DL                0             0             0
 6 EV                0             0             0
Feature Engineering in R

แสดงภาพ hashing

ดูเมทริกซ์ได้โดยใช้แพ็กเกจ plot.matrix

flights_hash <- 
    as.matrix(baked)[1:50,] 

plot(flights_hash, 
     col = c("white","steelblue"), 
     key = NULL,
     border = NA)

ภาพแสดงตัวอย่างการกำหนด dummy hash ของ factor สายการบินต้นฉบับ

Feature Engineering in R

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

Feature Engineering in R

Preparing Video For Download...