測試資料

Developing Machine Learning Models for Production

Sinan Ozdemir

Data Scientist, Entrepreneur, and Author

資料驗證與結構測試

  • 資料驗證測試 檢查遺漏值、不一致與異常資料
    • 例如偵測離群值
  • 結構測試(Schema) 檢查預期的資料格式與型別

    • 例如確保「time to value」是以秒為單位的整數,而不是分鐘
  • Great Expectations 這類工具可自動化流程

great expectations 標誌

Developing Machine Learning Models for Production

超越基礎測試

  • 資料與結構測試會檢查基本問題
  • 更進一步如 期望測試 可找出更複雜的問題
    • 檢查數值是否落在特定範圍
    • 檢查已知的模式/趨勢
Developing Machine Learning Models for Production

期望測試(Expectation tests)

  • 一種資料驗證測試
  • 確保資料符合使用者或系統定義的「期望」
    • 例如期望網站停留時間約為 4 分鐘,標準差為 1 分鐘
    • 例如期望病患過去就診日期早於目前時間
Developing Machine Learning Models for Production

特徵重要性測試

  • 特徵重要性測試 找出 ML 模型中最關鍵的特徵
  • 範例:Permutation importance
    • 隨機置換特徵值,量測模型效能下降幅度

長條圖

Developing Machine Learning Models for Production

Permutation importance 範例

設定:

import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
# Train a random forest classifier (assuming we have some data)
model = RandomForestClassifier().fit(X_train, y_train)

執行 permutation importance 測試:

# Calculate feature importances using permutation importance
results = permutation_importance(model, X_test, y_test, n_repeats=10, random_state=42)
# Print the feature importances
feature_names = ['feature_1', 'feature_2', 'feature_3', ...]
importances = results.importances_mean
for i in range(len(feature_names)):
    print(f'{feature_names[i]}: {importances[i]}')
Developing Machine Learning Models for Production

偵測資料漂移

資料漂移

  • 有時也稱特徵漂移
  • 模型輸入資料分佈的改變
  • 例如越來越多人使用對應新產品的新詞,導致聊天機器人無法判斷

標籤漂移

  • 標籤分佈的改變
  • 例如從詢問退貨轉為更多人改問退貨進度
Developing Machine Learning Models for Production

一起來練習吧!

Developing Machine Learning Models for Production

Preparing Video For Download...