gpt4 book ai didi

scikit-learn - class_weight 在 linearSVC 和 LogisticRegression 损失函数中的作用

转载 作者:行者123 更新时间:2023-12-04 18:15:41 27 4
gpt4 key购买 nike

我试图弄清楚损失函数公式到底是什么以及如何在 class_weight='auto' 时手动计算它在 svm.svc 的情况下, svm.linearSVClinear_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

我意识到逻辑回归有 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)。

但是当数据不平衡时,这不起作用。

更重要的是,当数据处于平衡状态且 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()} ?

非常感谢任何帮助。我尝试浏览源代码,但我不是程序员,我被卡住了。
非常感谢。

最佳答案

class_weight启发式

我对您对 class_weight='auto' 的第一个提议感到有些困惑。启发式,如:

class_weight = {-1 : (y == 1).sum() / (y == -1).sum(), 
1 : 1.}

如果我们对其进行标准化以使权重总和为 1,则与您的第二个命题相同。

反正明白什么 class_weight="auto"有,看这个问题:
what is the difference between class weight = none and auto in svm scikit learn .

我把它复制到这里供以后比较:

This means that each class you have (in classes) gets a weight equal to 1 divided by the number of times that class appears in your data (y), so classes that appear more often will get lower weights. This is then further divided by the mean of all the inverse class frequencies.



请注意这不是完全明显的;)。

此启发式已弃用,并将在 0.18 中删除。它将被另一个启发式替换, class_weight='balanced' .

“平衡”启发式方法根据类的频率倒数按比例加权。

从文档:

The "balanced" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data: n_samples / (n_classes * np.bincount(y)).


np.bincount(y)是一个数组,其中元素 i 是 i 类样本的计数。

这里有一些代码来比较两者:
import numpy as np
from sklearn.datasets import make_classification
from sklearn.utils import compute_class_weight

n_classes = 3
n_samples = 1000

X, y = make_classification(n_samples=n_samples, n_features=20, n_informative=10,
n_classes=n_classes, weights=[0.05, 0.4, 0.55])

print("Count of samples per class: ", np.bincount(y))
balanced_weights = n_samples /(n_classes * np.bincount(y))
# Equivalent to the following, using version 0.17+:
# compute_class_weight("balanced", [0, 1, 2], y)

print("Balanced weights: ", balanced_weights)
print("'auto' weights: ", compute_class_weight("auto", [0, 1, 2], y))

输出:
Count of samples per class:  [ 57 396 547]
Balanced weights: [ 5.84795322 0.84175084 0.60938452]
'auto' weights: [ 2.40356854 0.3459682 0.25046327]

损失函数

现在真正的问题是:这些权重如何用于训练分类器?

不幸的是,我在这里没有一个彻底的答案。

对于 SVClinearSVC文档字符串很清楚

Set the parameter C of class i to class_weight[i]*C for SVC.



因此,高权重意味着该类的正则化较少,并且 svm 对其进行正确分类的激励更高。

我不知道他们如何处理逻辑回归。我会尝试研究它,但大部分代码都在 liblinear 或 libsvm 中,我对它们不太熟悉。

但是,请注意 class_weight 中的权重 不直接影响 predict_proba 等方法 .他们改变输出是因为分类器优化了不同的损失函数。
不确定这是否清楚,所以这里有一个片段来解释我的意思(您需要为导入和变量定义运行第一个):
lr = LogisticRegression(class_weight="auto")
lr.fit(X, y)
# We get some probabilities...
print(lr.predict_proba(X))

new_lr = LogisticRegression(class_weight={0: 100, 1: 1, 2: 1})
new_lr.fit(X, y)
# We get different probabilities...
print(new_lr.predict_proba(X))

# Let's cheat a bit and hand-modify our new classifier.
new_lr.intercept_ = lr.intercept_.copy()
new_lr.coef_ = lr.coef_.copy()

# Now we get the SAME probabilities.
np.testing.assert_array_equal(new_lr.predict_proba(X), lr.predict_proba(X))

希望这可以帮助。

关于scikit-learn - class_weight 在 linearSVC 和 LogisticRegression 损失函数中的作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31657263/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com