【发布时间】:2015-10-17 21:14:09
【问题描述】:
我试图弄清楚损失函数公式到底是什么,以及在class_weight='auto' 时如何手动计算它,以防svm.svc、svm.linearSVC 和linear_model.LogisticRegression。
对于平衡数据,假设您有一个经过训练的分类器:clf_c。物流损失应该是(我对吗?):
def logistic_loss(x,y,w,b,b0):
'''
x: nxp data matrix where n is number of data points and p is number of features.
y: nx1 vector of true labels (-1 or 1).
w: nx1 vector of weights (vector of 1./n for balanced data).
b: px1 vector of feature weights.
b0: intercept.
'''
s = y
if 0 in np.unique(y):
print 'yes'
s = 2. * y - 1
l = np.dot(w, np.log(1 + np.exp(-s * (np.dot(x, np.squeeze(b)) + b0))))
return l
我意识到logisticRegression 有predict_log_proba(),它在数据平衡时为您提供准确的信息:
b, b0 = clf_c.coef_, clf_c.intercept_
w = np.ones(len(y))/len(y)
-(clf_c.predict_log_proba(x[xrange(len(x)), np.floor((y+1)/2).astype(np.int8)]).mean() == logistic_loss(x,y,w,b,b0)
注意,np.floor((y+1)/2).astype(np.int8) 只是将 y=(-1,1) 映射到 y=(0,1)。
但这在数据不平衡时不起作用。
此外,您希望分类器(此处为logisticRegression)在数据平衡和class_weight=None 与数据不平衡和class_weight='auto' 时表现相似(就损失函数值而言)。我需要有一种方法来计算两种场景的损失函数(没有正则化项)并进行比较。
简而言之,class_weight = 'auto'究竟是什么意思?它是指class_weight = {-1 : (y==1).sum()/(y==-1).sum() , 1 : 1.} 还是class_weight = {-1 : 1./(y==-1).sum() , 1 : 1./(y==1).sum()}?
非常感谢任何帮助。我尝试浏览源代码,但我不是程序员,我被卡住了。 提前非常感谢。
【问题讨论】:
标签: scikit-learn svm logistic-regression