การเข้ารหัสข้อมูลเชิงหมวดหมู่โดยใช้ supervised learning

Feature Engineering in R

Jorge Zazueta

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

แนะนำการเข้ารหัสแบบ supervised

การเข้ารหัสแบบ supervised ใช้ค่า outcome เพื่อสร้างฟีเจอร์เชิงตัวเลขจากตัวทำนายเชิงนามบัญญัติ

Feature Engineering in R

แนะนำการเข้ารหัสแบบ supervised

การเข้ารหัสแบบ supervised ใช้ค่า outcome เพื่อสร้างฟีเจอร์เชิงตัวเลขจากตัวทำนายเชิงนามบัญญัติ

ฟังก์ชันการเข้ารหัสแบบ supervised ในแพ็กเกจ embed

ฟังก์ชัน คำอธิบาย
step_lencode_glm() ใช้ likelihood encoding เพื่อแปลงตัวทำนายเชิงนามบัญญัติเป็นชุดคะแนนเดียวที่ได้จาก generalized linear model
step_lencode_bayes() ใช้ Bayesian likelihood encoding เพื่อแปลงตัวทำนายเชิงนามบัญญัติเป็นชุดคะแนนเดียวที่ได้จาก generalized linear model ประมาณการด้วย Bayesian analysis
step_lencode_mixed() แปลงตัวทำนายเชิงนามบัญญัติเป็นชุดคะแนนเดียวที่ได้จาก generalized linear mixed model
Feature Engineering in R

การทำนายความสำเร็จของใบสมัครทุน

เราต้องการทำนายความสำเร็จของใบสมัครทุนโดยใช้เฉพาะรหัสผู้สนับสนุน

lr_model <- logistic_reg() # declare model

lr_recipe_glm <- # Set recipe glm
  recipe(class ~ sponsor_code, 
         data = grants_train) %>%
  step_lencode_glm(sponsor_code, 
                   # Declare outcome variable
                   outcome = vars(class))

lr_workflow_glm <- # Create Workflow
  workflow() %>%
  add_model(lr_model) %>%
  add_recipe(lr_recipe_glm)

สรุป Workflow

lr_workflow_glm
-- Workflow ------------------------------------
Preprocessor: Recipe
Model: logistic_reg()

-- Preprocessor --------------------------------
1 Recipe Step

- step_lencode_glm()

-- Model --------------------------------------
Logistic Regression Model Specification (classification)

Computational engine: glm
Feature Engineering in R

การ fit, augment และประเมินผล

เรา fit และประเมินโมเดล

lr_fit_glm <- # Fit
  lr_workflow_glm %>%
  fit(grants_train)

lr_aug_glm <- # Augment
  lr_fit_glm %>%
  augment(grants_test)

glm_model <- lr_aug_glm %>% # Assess
  class_evaluate(truth = class,
                 estimate = .pred_class,
                 .pred_successful)

ผลการประเมินถูกเก็บไว้ใน glm_model

glm_model
# A tibble: 2 × 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.728
2 roc_auc  binary         0.684
Feature Engineering in R

การรวมโมเดลเข้าด้วยกัน

เราสร้าง bayes_model และ mixed_model เพื่อเปรียบเทียบประสิทธิภาพของแต่ละ step

# Define model names
model <- c("glm", "glm",
           "bayes","bayes",
           "mixed", "mixed")
# Bind models in a tibble
models <- 
bind_rows(glm_model,
          bayes_model,
          mixed_model)%>%
  add_column(model = model)%>%
  select(-.estimator) %>%
  spread(model,.estimate)

ตารางสรุปประสิทธิภาพ

models
# A tibble: 2 × 4
  .metric  bayes   glm mixed
  <chr>    <dbl> <dbl> <dbl>
1 accuracy 0.718 0.728 0.720
2 roc_auc  0.686 0.684 0.682
Feature Engineering in R

การแสดงผลลัพธ์

แสดงผลด้วย parallel coordinates chart จากแพ็กเกจ Gally

# Libraries
library(GGally)

# Parallel coordinates chart
ggparcoord(models,
           columns = 2:4, 
           groupColumn = 1,
           scale="globalminmax",
           showPoints = TRUE)

Parallel coordinates chart ของ accuracy และ roc_auc เปรียบเทียบทุกโมเดล

Parallel coordinates chart ของ accuracy และ roc_auc เปรียบเทียบทุกโมเดล

Feature Engineering in R

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

Feature Engineering in R

Preparing Video For Download...