scikit-learn - sklearn : LogisticRegression - predict_proba(X) - calculation

标签 scikit-learn logistic-regression

我想知道是否有人可以快速查看以下代码片段并指出我在计算模型中每个类的样本概率和相关代码错误时的误解。我尝试手动计算 sklearn 函数 lm.predict_proba(X) 提供的结果,遗憾的是结果不同,所以我犯了一个错误。

我认为该错误将在以下代码演练的“d”部分。也许在数学上,但我不明白为什么。

a) 创建和训练逻辑回归模型(工作正常)

lm = LogisticRegression(random_state=413, multi_class='multinomial', solver='newton-cg')
lm.fit(X, train_labels)

b)保存系数和偏差(工作正常)
W = lm.coef_
b = lm.intercept_

c) 使用 lm.predict_proba(X) (工作正常)
def reshape_single_element(x,num):
    singleElement = x[num]
    nx,ny = singleElement.shape
    return  singleElement.reshape((1,nx*ny))

select_image_number = 6 
X_select_image_data=reshape_single_element(train_dataset,select_image_number)
Y_probabilities =  lm.predict_proba(X_select_image_data)
Y_pandas_probabilities = pd.Series(Y_probabilities[0], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
print"estimate probabilities for each class: \n" ,Y_pandas_probabilities , "\n"
print"all probabilities by lm.predict_proba(..) sum up to ", np.sum(Y_probabilities) , "\n"

输出是:
estimate probabilities for each class: 
a 0.595426
b 0.019244
c 0.001343
d 0.004033
e 0.017185
f 0.004193
g 0.160380
h 0.158245
i 0.003093
j 0.036860
dtype: float64 
all probabilities by lm.predict_proba(..) sum up to 1.0

d) 手动执行 lm.predict_proba 完成的计算(没有错误/警告,但结果不一样)
manual_calculated_probabilities = []
for select_class_k in range(0,10):  #a=0. b=1, c=3 ...
    z_for_class_k = (np.sum(W[select_class_k] *X_select_image_data) + b[select_class_k] )
    p_for_class_k = 1/ (1 + math.exp(-z_for_class_k))
    manual_calculated_probabilities.append(p_for_class_k)

print "formula: ", manual_calculated_probabilities , "\n"

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    e = np.exp(x)
    dist = e / np.sum(np.exp(x),axis=0)
    return dist

abc = softmax(manual_calculated_probabilities)
print "softmax:" , abc

输出是:
formula: [0.9667598370531315, 0.48453459121301334, 0.06154496922245115, 0.16456194859398865, 0.45634781280053394, 0.16999340794727547, 0.8867996361191054, 0.8854473986336552, 0.13124464656251109, 0.642913996162282]

softmax: [ 0.15329642 0.09464644 0.0620015 0.0687293 0.0920159 0.069103610.14151607 0.14132483 0.06647715 0.11088877]

使用了 Softmax,因为 github logistic.py 的评论
For a multi_class problem, if multi_class is set to be "multinomial" the softmax function is used to find the predicted probability of each class.

笔记:
print "shape of X: " , X_select_image_data.shape
print "shape of W: " , W.shape
print "shape of b: " , b.shape

shape of X:  (1, 784)
shape of W:  (10, 784)
shape of b:  (10,)

我发现了一个非常相似的问题 here ,但遗憾的是我无法将其调整到我的代码中,因此预测结果相同。我尝试了许多不同的组合来计算变量“z_for_class_k”和“p_for_class_k”,但遗憾的是没有成功重现“predict_proba(X)”的预测值。

最佳答案

我认为问题在于

p_for_class_k = 1/ (1 + math.exp(-z_for_class_k))


1 / (1 + exp(-logit))是仅适用于二元问题的简化。

真正的等式,在被简化之前,看起来是这样的:
p_for_classA = exp(logit_classA) / [1 + exp(logit_classA) + exp(logit_classB) ... + exp(logit_classC)]
换句话说,在计算特定类别的概率时,您必须将其他类别的所有权重和偏差也纳入您的公式。

我没有数据来测试这一点,但希望这为您指明了正确的方向。

关于scikit-learn - sklearn : LogisticRegression - predict_proba(X) - calculation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35315269/

相关文章:

python - 在 3D 数据上使用 Standardscaler

python - 如何在 python 中执行逻辑套索?

machine-learning - 多类分类中的成本函数是什么?

python - scikit-learn cross_val_predict 准确度分数是如何计算的?

python - 在 sklearn 管道中传递上一步的参数

python - Python 中的正则化逻辑回归(Andrew ng 类(class))

python - 二元对数损失是否排除了基于 y 的方程的一部分?

python - Sklearn LogisticRegressionCV 的类似数组的输入

具有多个变量的 Python for 循环迭代

python - smooth_idf 是多余的吗?