分類用決策樹

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

課程總覽

  • 第 1 章:Classification And Regression Tree(CART)

  • 第 2 章:偏差—變異權衡

  • 第 3 章:Bagging 與隨機森林

  • 第 4 章:Boosting

  • 第 5 章:模型調校

Machine Learning with Tree-Based Models in Python

分類樹

  • 針對各個特徵進行一連串 if-else 判斷。

  • 目標:推斷類別標籤。

  • 能捕捉特徵與標籤間的非線性關係。

  • 不需特徵縮放(如:Standardization 等)。

Machine Learning with Tree-Based Models in Python

乳癌資料集(2D)

BC2D

Machine Learning with Tree-Based Models in Python

決策樹示意圖

CART-rep

Machine Learning with Tree-Based Models in Python

scikit-learn 的分類樹

# Import DecisionTreeClassifier
from sklearn.tree import DecisionTreeClassifier
# Import train_test_split
from sklearn.model_selection import train_test_split
# Import accuracy_score
from sklearn.metrics import accuracy_score

# Split the dataset into 80% train, 20% test X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.2, stratify=y, random_state=1)
# Instantiate dt dt = DecisionTreeClassifier(max_depth=2, random_state=1)
Machine Learning with Tree-Based Models in Python

scikit-learn 的分類樹

# Fit dt to the training set
dt.fit(X_train,y_train) 

# Predict the test set labels
y_pred = dt.predict(X_test)

# Evaluate the test-set accuracy accuracy_score(y_test, y_pred)
0.90350877192982459
Machine Learning with Tree-Based Models in Python

決策區域

決策區域:特徵空間中,所有樣本被指定為同一類別標籤的區域。

決策邊界:分隔不同決策區域的曲面。

DR

Machine Learning with Tree-Based Models in Python

決策區域:CART vs. 線性模型

LRvsDT

Machine Learning with Tree-Based Models in Python

一起來練習吧!

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...