您的位置:首页 > 其它

机器学习实战笔记之非均衡分类问题

2016-01-10 11:08 561 查看
通常情况下,我们直接使用分类结果的错误率就能够做为该分类器的评判标准了,可是当在分类器训练时正例数目和反例数目不相等时。这样的评价标准就会出现故障(比方我们有1000个正例,10个负例。此时预測中500个正例被预測正确。10个负例也被预測为正例。那么准确率为500/510,很高,可是召回率仅仅有500/1000=50%,很低。)。这样的现象也称为非均衡分类问题。

此时有下面几个衡量标准。

以下首先通过一副图来说明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)。在检索系统中称为查全率。通常我们能够非常easy的构照一个高正确率或高召回率的分类器。可是非常难同一时候保证两者成立。假设不论什么样本都被判为了正例。那么召回率达到百分之百而此时准确率非常低。构建一个同一时候使正确率和召回率最大的分类器是具有挑战性的。此时我们能够用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.csdn.net/abcjennifer/article/details/7359370

2:http://zh.wikipedia.org/wiki/ROC%E6%9B%B2%E7%BA%BF

作者:小村长  出处:http://blog.csdn.net/lu597203933 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:小村长zack, 欢迎交流!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: