优化分类指标
介绍
某些分类指标并不是完全适用我们的模型,有时候我们需要选择和优化指标来调整我们的模型。
Logloss
如果要优化log-loss,我们只需要将它和正确的模型匹配。
- 树模型:XGBoost,LightGBM
- 线性:sklearn,Regression, sklearn.SGDRegressor, Vowpa Wabbit
- 神经网络:PyTorch, Keras, Tensorflow等
Random Forests使用Log loss的话表现效果较差。
Probability Calibration(概率检准)
如果所有的行都拿到相同的分数,那么它们的等级1的分数应该与分数相匹配(所以如果它们的分数都是0.8,那么它们中的80%应该是1和20%应该是0)。如果没有分数,则需要校准概率。为此,我们需要先选择一个模型,然后将其输出发送到一个更好的Log Loss模型。因此,如果我们想使用随机森林,我们使用AUC作为度量标准训练我们的模型,然后使用预测来训练另一个模型,如神经网络,并使用Log Loss作为度量标准。
Platt Scaling
在机器学习中,platt scaling 或者 platt calibration 是把模型的输出转化成一种基于类别的概率分布的方法。这个方法代替了早期的Vapnik提出的方法,但是这个方法可以应用于其他的分类模型。platt是把一个逻辑回归模型拟合分类分数的方法。
在二分类中,对于输入x,我们想要知道它属于两类中的哪一类,我们假定这个分类任务可以用一个函数f解决,预测y=sign(f(x)), 对于many问题,很方便获得P(y=1|x),例如一个分类器不仅给出预测的结果,而且给出置信度。但是有的模型却没有给出。
platt scaling 是一个解决上述问题的算法:
P
(
y
=
1
∣
x
)
=
1
1
+
e
x
p
(
A
f
(
x
)
+
B
)
P(y=1|x)=\frac{1}{1+exp(Af(x)+B)}
P(y=1∣x)=1+exp(Af(x)+B)1
对分类的分数f(x)进行了逻辑回归转化,A和B是分类器学习得到的两个参数。转化后,由P(y=1|x)>0.5
可以预测y=1,如果B不等于0,和之前的y=sign(f(x))相比,概率估计包含了一个纠正项,
使用最大似然法估计A和B,和分类器f用同样的训练集,为了防止过拟合,可以保留calibration set 或者使用交叉验证,并且应该把标签y转化成目标概率:
t
+
=
N
+
+
1
N
+
+
2
t
−
=
1
N
−
+
2
t_{+}=\frac{N_{+}+1}{N_{+}+2}\\ t_{-}=\frac{1}{N_{-}+2}
t+=N++2N++1t−=N−+21
N+和N-分别表示正样本和负样本的个数
This transformation follows by applying Bayes’ rule to a model of out-of-sample data that has a uniform prior over the labels.wiki
使用Logistic Regression去修正我们的预测
Iostonic Regression
使用Iostonic Regression去修正我们的预测
Stacking
使用XGBoost或者神经网络来修正我们的预测
准确性
准确度是一个难以优化的指标,因为它不可以微分。
要优化准确度指标,需要使用不同的度量标准(代理度量标准),像log-loss然后调整阈值
曲线下的面积(AUC)
有些模型可以使用它:
- 基于树的模型:XGboost,LightGBM
- 线性模型:不要使用!
- 神经网络:PyTorch,Keras,Tensorflow
在实践中,我们可以通过logloss来优化模型
二次加权Kappa
通过优化Mean Squared Error然后优化阈值
其他资源
Classification
- Evaluation Metrics for Classification Problems
- Descision Trees: Gini vs Entropy
- Understanding ROC Curves
Ranking
- Learning to Rank Using Gradient Descent - source of pairwise AUC optimization
- From RankNet to LambdaRank
- RankLib (implementation of the two previous papers)
- Learning to Rank Overview
Clustering
练习题:
1、What would be a logloss value for a binary classification task, if we use constant predictor f(x) = 0.5? Round to two decimal places.
0.69
2、The best constant predictor for MAE metric is:Target median, Target 50-th percentile
3、The best constant predictor for mean squared error is :Target Mean, average of the target vector
4、The best constant prediction for AUC is:Any constant will lead to the same AUC value
5、Suppose the target metric is R-squared. What optimization loss should we use for our models?
- RMSE, MSE
6、Calculate AUC for these predictions:
target | prediction |
---|---|
1 | 0.39 |
0 | 0.52 |
1 | 0.91 |
1 | 0.85 |
1 | 0.49 |
0 | 0.02 |
0 | 0.44 |
Round to 2 decimal places.
from sklearn.metrics import roc_auc_score
y_true = [1,0,1,1,1,0,0]
y_score = [0.39,0.52,0.91,0.85,0.49,0.02,0.44]
print(roc_auc_score(y_true, y_score))
0.75