大資料分析 - 決策樹



決策樹是一種用於監督學習問題的演算法,例如分類或迴歸。決策樹或分類樹是一棵樹,其中每個內部(非葉子)節點都用一個輸入特徵標記。來自用特徵標記的節點的弧線用該特徵的每個可能值標記。樹的每個葉子都用一個類或一個類上的機率分佈標記。

可以透過基於屬性值測試將源集拆分為子集來“學習”樹。此過程以遞迴方式重複應用於每個派生子集,稱為**遞迴分割槽**。當節點處的子集的目標變數值都相同時,或者拆分不再增加預測值時,遞迴完成。這種自上而下歸納決策樹的過程是貪婪演算法的一個例子,也是學習決策樹最常用的策略。

資料探勘中使用的決策樹主要有兩種型別:

  • **分類樹** - 當響應為名義變數時,例如電子郵件是否為垃圾郵件。

  • **迴歸樹** - 當預測結果可以被視為實數時(例如,工人的工資)。

決策樹是一種簡單的方法,因此存在一些問題。其中一個問題是決策樹產生的結果模型方差很大。為了緩解這個問題,開發了決策樹的整合方法。目前廣泛使用兩種整合方法:

  • **裝袋決策樹** - 這些樹用於透過重複替換訓練資料進行重新取樣並對樹進行投票以達成共識預測來構建多個決策樹。該演算法被稱為隨機森林。

  • **提升決策樹** - 梯度提升將弱學習器組合起來;在這種情況下,決策樹以迭代方式組合成一個強大的學習器。它將一個弱樹擬合到資料中,並迭代地繼續擬合弱學習器以糾正先前模型的錯誤。

# Install the party package
# install.packages('party') 
library(party) 
library(ggplot2)  

head(diamonds) 
# We will predict the cut of diamonds using the features available in the 
diamonds dataset. 
ct = ctree(cut ~ ., data = diamonds) 

# plot(ct, main="Conditional Inference Tree") 
# Example output 
# Response:  cut  
# Inputs:  carat, color, clarity, depth, table, price, x, y, z  

# Number of observations:  53940  
#  
# 1) table <= 57; criterion = 1, statistic = 10131.878 
#   2) depth <= 63; criterion = 1, statistic = 8377.279 
#     3) table <= 56.4; criterion = 1, statistic = 226.423 
#       4) z <= 2.64; criterion = 1, statistic = 70.393 
#         5) clarity <= VS1; criterion = 0.989, statistic = 10.48 
#           6) color <= E; criterion = 0.997, statistic = 12.829 
#             7)*  weights = 82  
#           6) color > E  

#Table of prediction errors 
table(predict(ct), diamonds$cut) 
#            Fair  Good Very Good Premium Ideal 
# Fair       1388   171        17       0    14 
# Good        102  2912       499      26    27 
# Very Good    54   998      3334     249   355 
# Premium      44   711      5054   11915  1167 
# Ideal        22   114      3178    1601 19988 
# Estimated class probabilities 
probs = predict(ct, newdata = diamonds, type = "prob") 
probs = do.call(rbind, probs) 
head(probs)
廣告

© . All rights reserved.