统计与机器学习 / 19

模型评估:交叉验证与分类指标

从假设、目标到模型和评估。 模型评估:交叉验证与分类指标。

阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。

1. The pipeline of model building and evaluation

模型流程: 明确目标 → 采集与划分数据 → 只在训练部分拟合预处理 → 交叉验证选择模型/超参数 → 重新拟合 → 独立测试 → 误差分析与上线监测。

2. How to evaluate a model

Evaluation nedds data + method (metrics)

  • What data to evaluate: select and split data
  • What method to use: cross validation
  • What metrics to compare: confusion matrix, ROC, AUC

2.1 Cross Validation

2.1.1 K-fold Cross Validation

K折轮换: 将开发数据分为K份,第i轮用第i份验证、其余训练;平均各轮验证指标。比如线性模型两折MSE为12和10.2,平均11.1;二次模型13和13.2,平均13.1。图中另一处11.8的平均值笔误不沿用。

Performance=1N∑i=1nperfromanceiPerformance = \frac{1}{N}\sum^n_{i=1}perfromance_i
Performance is based on the validation data.

平均MSE衡量的是某一类model(骨架model)的好坏:

  • Y=aX+bY=aX+b与Y=aX2+bX+cY=aX^2+bX+c是两类model
  • Y=aX+b,  λ=1Y=aX+b, \; \lambda=1与Y=aX+b,  λ=2Y=aX+b, \; \lambda=2也是两类model

最后选择MSE最小的一组Model frameword作为最优的model

2.1.2 Holdout

“Houldout” = “Test”
Holdout data只在最后一次使用

流程:

  1. 在建模前,留一部分data (holdout data)
  2. 一开始用cross validation,选好一个好的模型,做骨架
  3. 然后用所有的training+validation再train一遍,找到parameter,填肉
  4. 最后,用holdout data得到最后的一个客观的evaluation结果。holdout data (test data)只用一次。

Holdout边界: 全量数据先划出独立test;余下开发集用于K折选择。选定配置后在整个开发集重拟合,最后只用test作一次无偏评估。测试结果若用于继续选择,便需要新的独立评估。

2.1.3 各种术语区别

training set: Loss
test set: MSE(model error)
validation set: MSE(model error)

基本: training set + test set
使用cross validation: training set + validation set
cross validation + holdout : (training set + validation set) + test set

2.1.4 Model Selection with Cross validation

  • Use the cross validation method to do hyperparameter tuning
  • Cross validation can only validate your model selection

总结:

  1. Cross validation -> 找骨架(model selection/model infrastructure)
  2. Mixed validation and training data into training -> 找肉,例如y=ax+b里的a和b

划分方式对照: 单次训练/验证划分简单但可能方差较大;K折通过多个验证分区评估同一配置;留一法令K=n,成本较高。时序、同一用户多条记录等数据要按时间或组划分,不能盲目随机打散。

2.2 Confusion Matrix

2.2.1 binary classifcation problem

For binary classifcation problem:

真实\预测正类负类
正类TPFN
负类FPTN

先声明正类含义,再计算precision、recall和accuracy,避免交换行列后误读。

Example: Positive - 有病

  • True positive:你预测对了(true),你说的是“他有病” -> 你预测他有病,他确实有病
  • False positive:你预测错了(false),你说的是“他有病” -> 你预测他有病,他实际没病
  • True negative:你预测对了(true),你说的是“他没病” -> 你预测他没病,他确实没病
  • False negative:你预测错了(false),你说的是“他没病” -> 你预测他没病,他实际有病

2.2.2 Different metrics for model evaluation:

  • Accuracy = (tp + tn) / (tp + fp + tn + fn)
    Testing data: 1000
    Truth -> Positive:10, Negative: 990
    Prediction -> 1 positive (correct), all others negative 999
    Accuracy: (1 + 990) / 1000 = 99.1%
  • Precision = tp / (tp + fp) = tp / p
    在所有你认为的positive的数据中,有多少真的是positive?
    Precision: 1 / 1 = 100%
  • Recall = tp / (tp + fn)
    在所有positive的数据中,有多少被你正确地识别出来(是positive)?
    Recall: 1/ (1 + 9) = 10%
  • F1-score = 2 / (1 / precison + 1 / recall)
    F-score能同时考虑Precision和Recall,平衡的反映这个算法的精确度。
    F1: 2 / (1/ 0.1 + 1 / 1) = 18.18%

2.2.3 Precison vs Recall

Precison vs Recall

  • 要求precision高:
    • Spam email
  • 要求recall高:
    • Disease detection
    • Cybersecurity

Precision, Recall -> classifier; threshold (分割positive or negative的临界概率,默认0.5)

2.3 Result Evaluation Metric - ROC curve

  • Receiver operating characteristic curve

ROC坐标: 横轴FPR=FP/(FP+TN),纵轴TPR=TP/(TP+FN)。阈值从高到低逐步接纳正预测,曲线由(0,0)走向(1,1)。随机评分的参考线为TPR=FPR,不把示意曲线当作实验数据。

  • Define False Positive Rate as X axis, True Positive Rate as Y axis

    • False Positive Rate = Number of False positive / Number of real negative
    • True Positive Rate = Number of True positive / Number of real positive = recall
  • Special Points in ROC space
    Best case: (0, 1), Worst case: (1, 0)
    对角线上的点:

    • 当Threshold设定为最高时,所有样本都被预测为Negative. 此时得到的点在(0, 0)
    • 当Threshold设定为最低时,所有样本都被预测为Positive. 此时得到的点在(1, 1)

    (Advanced Topic) why does Equal Error Rate mean FPR = FNR?
    已知固定关系:FNR = 1 - TP / Number of real positive = 1 - TPR
    根据图中焦点性质可知:
    FPR = 1 - TPR
    (x)    (y)
    => FPR = FNR

  • Area under the Curve of ROC (AUC)
    AUC value: [0, 1]
    The larger the value is, the better classification performance your classifier has.
    AUC value is a probability value.

面试题:机器学习里0-1的值,都希望有一个概率。怎么用概率来解释AUC?
ROC AUC is the probability that a randomly-chosen positive example is ranked more highly than a randomly-chosen negative example.

整理注: AUC在并列分数时计半个正确排序;ROC不是校准度,类别极不平衡时也应查看PR曲线和业务代价。

Example:

原五项例子的独立表述: A、B、C、D、E的分数为0.6、0.4、0.3、0.7、0.8,标签为1、1、0、1、1。全部正类都排在唯一负类C前,故AUC=1;阈值0.5时TPR=3/4、FPR=0,阈值0.75时TPR=1/4、FPR=0。若改变标签/分数导致负例穿插,AUC会下降。

3. Failure Analysis

减少failures的策略:

  • 调整模型
  • 调参
  • 通过data解决
  • 不能解决的

Failure Analysis目的是进一步提高模型的性能,进行迭代开发、retraining

总结:

  1. Cross calidation -> 找骨架 (model selection/model infrastructure)
  2. Mixed validation and training data into training -> 找肉,例如y=ax+b里面的a和b
  3. Model evaluation -> ROC, AUC, Precision, Recall
  4. Failure analysis -> 确定问题在哪,然后更新Step1-3

4. Machine Learning End-to-End Pipeline

End to end ML/DL in production usually contains many steps:

  • Data acquisition
    • Collect data from outside system
    • Pre-process into the format that needed
  • Data preparation
    • Clean, transform, validate and select data
    • Annotation
    • Feature extraction
    • Prepare data into: training, validaton and evaluation datasets
  • Training & Validation
    • Select the model
    • Train the model
    • Tune the model
    • Validate the training process
  • Testing/Evaluation
    • Evaluate the performance metrics on evaluation dataset
    • Analyze failures
  • Deployment & Inference
    • Integrated into production apps
    • Config models based on system needs
    • Predict on real-life data
    • Log failures/errors

整理参考资料