通常情况下,我们直接使用分类结果的错误率就可以做为该分类器的评判标准了,但是当在分类器训练时正例数目和反例数目不相等时,这种评价标准就会出现问题。这种现象也称为非均衡分类问题。此时有以下几个衡量标准。
下面首先通过一副图来说明TP,FP,FN和TN的概念:
从图中可以看出形象的解释就是TP为预测为1而真实情况也为1的样本数量,FP为预测为1而真实情况为0的样本数量,FN为预测为0而真实情况为1的样本数量,TN为预测为0而真实情况为0的样本数量。
(1) 正确率<precise>和召回率<Recall>
如下图所示:其中准确率指预测的真实正例占所有真实正例的比例,等于TP/(TP+FP),而召回率指预测的真实正例占所有真实正例的比例,等于TP/(TP+FN)。通常我们可以很容易的构照一个高正确率或高召回率的分类器,但是很难同时保证两者成立。如果任何样本都被判为了正例,那么召回率达到百分之百而此时准确率很低。构建一个同时使正确率和召回率最大的分类器是具有挑战性的。此时我们可以用F-Score =precise*recall/(precise+ recall) 这个量来衡量,越大越好。
(2) ROC曲线
AUC计算工具:
http://mark.goadrich.com/programs/AUC/
- def plotROC(predStrengths, classLabels):
- import matplotlib.pyplot as plt
- cur = (1.0,1.0) #cursor
- ySum = 0.0 #variable to calculate AUC
- numPosClas = sum(array(classLabels)==1.0)
- yStep = 1/float(numPosClas); xStep = 1/float(len(classLabels)-numPosClas)
- sortedIndicies = predStrengths.argsort()#get sorted index, it's reverse
- fig = plt.figure() #这三行代码用于构建画笔
- fig.clf()
- ax = plt.subplot(111)
- #loop through all the values, drawing a line segment at each point
- for index in sortedIndicies.tolist()[0]:
- if classLabels[index] == 1.0:
- delX = 0; delY = yStep;
- else:
- delX = xStep; delY = 0;
- ySum += cur[1]
- #draw line from cur to (cur[0]-delX,cur[1]-delY)
- ax.plot([cur[0],cur[0]-delX],[cur[1],cur[1]-delY], c='b')
- cur = (cur[0]-delX,cur[1]-delY)
- ax.plot([0,1],[0,1],'b--')
- plt.xlabel('False positive rate'); plt.ylabel('True positive rate')
- plt.title('ROC curve for AdaBoost horse colic detection system')
- ax.axis([0,1,0,1])
- plt.show()
- print "the Area Under the Curve is: ",ySum*xStep
参考ROC文档:1:http://blog.youkuaiyun.com/abcjennifer/article/details/7359370
2:http://zh.wikipedia.org/wiki/ROC%E6%9B%B2%E7%BA%BF
作者:小村长 出处:http://blog.youkuaiyun.com/lu597203933 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:小村长zack, 欢迎交流!)