Biến liên tục

Machine Learning với Mô hình Dựa trên Cây trong R

Sandro Raabe

Data Scientist

Bộ dữ liệu

head(chocolate, 5)
final_grade review_date   cocoa_percent  company_location  bean_type              broad_bean_origin
<dbl>       <int>         <dbl>          <fct>             <fct>                  <fct>
3           2009          0.8            U.K.              "Criollo, Trinitario"  "Madagascar"
3.75        2012          0.7            Guatemala         "Trinitario"           "Madagascar"
2.75        2009          0.75           Colombia          "Forastero (Nacional)" "Colombia"
3.5         2014          0.74           Zealand           ""                     "Papua New Guinea"
3.75        2011          0.72           Australia         ""                     "Bolivia"
Machine Learning với Mô hình Dựa trên Cây trong R

Xây dựng cây hồi quy

spec <- decision_tree() %>%

set_mode("regression") %>%
set_engine("rpart")
print(spec)
Decision Tree Model Specification
(regression)

Computational engine: rpart
model <- spec %>%
  fit(formula = final_grade ~ .,

data = chocolate_train)
print(model)
parsnip model object

Fit time:  20ms 
n= 1437 

node), split, n, deviance, yval
      * denotes terminal node
Machine Learning với Mô hình Dựa trên Cây trong R

Dự đoán bằng cây hồi quy

# Dự đoán của mô hình trên dữ liệu mới
predict(model, new_data = chocolate_test)
.pred
<dbl>
3.281915
3.435234
3.281915
3.833931
3.281915
3.514151
3.273864
3.514151
Machine Learning với Mô hình Dựa trên Cây trong R

Chia để trị

chia để trị

Machine Learning với Mô hình Dựa trên Cây trong R

Siêu tham số

Mục tiêu với cây hồi quy:
  • Phương sai/độ lệch nhỏ trong từng nhóm
Quyết định thiết kế:
  • min_n: số điểm dữ liệu ở nút để tiếp tục tách (mặc định: 20)
  • tree_depth: độ sâu tối đa của cây (mặc định: 30)
  • cost_complexity: phạt độ phức tạp (mặc định: 0.01)
Thiết lập ngay bước đầu:
decision_tree(tree_depth = 4, cost_complexity = 0.05) %>% 
    set_mode("regression")
Machine Learning với Mô hình Dựa trên Cây trong R

Hiểu đầu ra mô hình

decision_tree(tree_depth = 1) %>%
  set_mode("regression") %>%              
  set_engine("rpart")  %>%
  fit(formula = final_grade ~ .,
      data = chocolate_train)
parsnip model object

Fit time:  1ms
n= 1000

node), split, n, yval

1) root                 1000  2.347450
2) cocoa_percent>=0.905   16  2.171875 *
3) cocoa_percent<0.905   984  3.190803 *
  • Mô hình với tree_depth = 1

 

 

  • Trực quan hóa:

cây quyết định

Machine Learning với Mô hình Dựa trên Cây trong R

Hồi quy thôi nào!

Machine Learning với Mô hình Dựa trên Cây trong R

Preparing Video For Download...