การลดมิติข้อมูล

Feature Engineering in R

Jorge Zazueta

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

ฟีเจอร์ที่มีความแปรปรวนเป็นศูนย์

บางชุดข้อมูลมีคอลัมน์ที่มีค่าคงที่หรือมีความแปรปรวนเป็นศูนย์ เราสามารถกรองฟีเจอร์เหล่านั้นออกได้โดยเพิ่ม step_zv() ลงใน recipe()

ตารางแสดงตัวอย่างฟีเจอร์ที่มีความแปรปรวนเป็นศูนย์

Feature Engineering in R

ฟีเจอร์ที่มีความแปรปรวนใกล้ศูนย์

ฟีเจอร์ที่มีความแปรปรวนใกล้ศูนย์ ได้แก่ ตัวทำนายที่มีค่าเดียว และ ตัวทำนายที่มีลักษณะดังต่อไปนี้ครบทั้งสองข้อ:

  • จำนวนค่าที่ไม่ซ้ำกันน้อยมากเมื่อเทียบกับจำนวนตัวอย่าง

  • อัตราส่วนของความถี่ค่าที่พบบ่อยที่สุดต่อค่าที่พบบ่อยรองลงมามีค่าสูงมาก

ตัวอย่างความแปรปรวนใกล้ศูนย์:

  • ใน 100 การสังเกต มีค่าต่างกัน 2 ค่า แต่ค่าหนึ่งปรากฏเพียงครั้งเดียว

step_nzv() ระบุและลบตัวทำนายที่มีลักษณะเหล่านี้ออก

Feature Engineering in R

การวิเคราะห์องค์ประกอบหลัก (PCA)

ชุดข้อมูลสามมิติดั้งเดิมที่มีสองคลาส

กราฟสามมิติแสดงข้อมูลสองคลาส

ชุดข้อมูลที่ลดมิติแล้ว แสดงข้อมูลด้วย principal component สองตัวแรก

กราฟสองมิติแสดงข้อมูลสองคลาสในรูป scatterplot ของ principal component สองตัวแรก

Feature Engineering in R

มาเตรียม recipe กันเถอะ

สร้าง recipe สำหรับทำ PCA และดึงผลลัพธ์ออกมาด้วย prep()

pc_recipe <- 
recipe(~., data = loans_num) %>%
  step_nzv(all_numeric()) %>%
  step_normalize(all_numeric()) %>%
  step_pca(all_numeric())
pca_output <- prep(pc_recipe)

ดูข้อมูลที่มีได้โดยเรียก names() บน pca_output

names(pca_output)
 [1] "var_info"       "term_info"     
 [3] "steps"          "template"      
 [5] "levels"         "retained"      
 [7] "requirements"   "tr_info"       
 [9] "orig_lvls"      "last_term_info"
Feature Engineering in R

ค้นหาความแปรปรวนที่อธิบายได้

ดึงค่าเบี่ยงเบนมาตรฐานจากออบเจกต์ pca_output และคำนวณความแปรปรวนที่อธิบายได้

stdv <- pca_output$steps[[3]]$res$sdev
var_explained <- stdv^2/sum(stdv^2)
PCA = tibble(PC = 1:length(stdv),
          var_explained = var_explained, 
          cumulative = cumsum(var_explained))

ตารางแสดงความแปรปรวนที่อธิบายได้แยกตาม principal component

# A tibble: 5 × 3
     PC var_explained cumulative
  <int>         <dbl>      <dbl>
1     1        0.315       0.315
2     2        0.214       0.529
3     3        0.202       0.730
4     4        0.198       0.928
5     5        0.0722      1
Feature Engineering in R

การแสดงผลความแปรปรวนที่อธิบายได้

สร้างกราฟผลลัพธ์เป็น column chart ด้วย ggplot2

PCA %>% 
ggplot(aes(x = PC, 
           y = var_explained)) +
  geom_col(fill = "steelblue") +
  xlab("Principal components") +
  ylab("Variance explained")

ความแปรปรวนที่อธิบายได้แยกตาม principal component

Column chart แสดงความแปรปรวนที่อธิบายได้แยกตาม principal component

Feature Engineering in R

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

Feature Engineering in R

Preparing Video For Download...