我尝试使用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 方法来解决这个问题,让它更快。
1条答案
按热度按时间6g8kf2rb1#
将所有内容强制转换为
n x d
,然后使用np.where
。