使用numpy查找对数似然数据

juud5qan  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(108)

我尝试使用numpy来获得原生贝叶斯的对数似然。以下是当label分别为+1和-1时,每个维度中得到1的概率:

positive = [0.07973422 0.02657807]
negative = [0.04651163 0.02491694] #both of these have the dimension d

以下是测试和测试标签

x = np.array([[0,1],[1,0],[1,1]]) # dimension is n*d : note that the d is same as above
y = np.array([-1,1,-1]) #dimension is n

我想要的结果

result = [-3.73983529 -2.55599409 -6.76026018] #dimension is n

logic-〉每个结果元素对应于x中的一行,这取决于使用y的哪个值来使用正和负
即:对于行0,即[0,1],标签-1,这意味着我们取posprob。

-3.73983529 = log( 1 - 0.04651163 ) + log(0.02491694)

,这里我们从1中减去,因为0的概率等于1减去1的概率。
我现在使用的是紧循环。但是我想用 numpy 方法来解决这个问题,让它更快。

6g8kf2rb

6g8kf2rb1#

将所有内容强制转换为n x d,然后使用np.where

positive = [0.07973422, 0.02657807]
negative = [0.04651163, 0.02491694]  # both of these have the dimension d

x = np.array(
    [[0, 1], [1, 0], [1, 1]]
)  # dimension is n*d : note that the d is same as above
y = np.array([-1, 1, -1])  # dimension is n

d = len(positive)
n = len(x)

# Cast all to n x d

positive = np.array([positive]*n)
negative = np.array([negative]*n)

y = np.repeat(y, d).reshape(n, d)

# Determine whether to use pos or neg probabilities
pos_neg = np.where(y == 1, positive, negative)

# Determine whether to use prob or 1-prob
probs = np.where(x == 0, 1 - pos_neg, pos_neg)

# Take logs and then sum
log_probs = np.log(probs)

log_like = np.sum(log_probs, axis = 1)

print(log_like)

相关问题