袋外評估

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

Bagging(自助集成)

  • 對單一模型,有些樣本可能被抽中多次,

  • 也有些樣本一次都沒被抽中。

Machine Learning with Tree-Based Models in Python

袋外(OOB)樣本

  • 平均而言,每個模型會抽到 63% 的訓練樣本。

  • 剩下的 37% 即為 OOB 樣本。

Machine Learning with Tree-Based Models in Python

OOB 評估

袋外評估示意圖

Machine Learning with Tree-Based Models in Python

sklearn 的 OOB 評估(Breast Cancer 資料集)

# Import models and split utility function
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

# Set seed for reproducibility
SEED = 1

# Split data into 70% train and 30% test
X_train, X_test, y_train, y_test =  train_test_split(X, y, test_size= 0.3,
                                                     stratify= y,
                                                     random_state=SEED)

Machine Learning with Tree-Based Models in Python
# Instantiate a classification-tree 'dt'
dt = DecisionTreeClassifier(max_depth=4,
                            min_samples_leaf=0.16,
                            random_state=SEED)

# Instantiate a BaggingClassifier 'bc'; set oob_score = True bc = BaggingClassifier(base_estimator=dt, n_estimators=300, oob_score=True, n_jobs=-1)
# Fit 'bc' to the training set bc.fit(X_train, y_train) # Predict the test set labels y_pred = bc.predict(X_test)
Machine Learning with Tree-Based Models in Python
# Evaluate test set accuracy
test_accuracy = accuracy_score(y_test, y_pred)

# Extract the OOB accuracy from 'bc' oob_accuracy = bc.oob_score_ # Print test set accuracy print('Test set accuracy: {:.3f}'.format(test_accuracy))
Test set accuracy: 0.936
# Print OOB accuracy
print('OOB accuracy: {:.3f}'.format(oob_accuracy))
OOB accuracy: 0.925
Machine Learning with Tree-Based Models in Python

一起來練習吧!

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...