我的任务是编写函数,为每个类返回 P(y|x) 分布(使用朴素贝叶斯分类器)。结果是 N x M 矩阵。
输入数据
`p_y` -> a prior probabilities for labels 1 x M
`p_x_1_y` -> probability distribution P(x = 1|y), matrix M x D
`X` -> data for which we make distribution, (True of False in each cell) matrix N x D
`return` -> Probability distribution P(y|x), matrix N x M
当前代码
到目前为止,我的尝试,如果有人可以尝试使用数学逻辑,因为我必须错过一些东西,因为提供的测试没有通过。
p_y_x_matrix = np.zeros([X.shape[0], len(p_y)])
p_x = np.sum(X, axis=0) / X.shape[0]
for i in range(len(p_y)):
x_temp = np.zeros(X.shape)
for j in range(X.shape[0]):
for k in range(X.shape[1]):
if X[j, k]:
x_temp[j, k] += p_x_1_y[i, k]
elif X[j, k] is not True:
x_temp[j, k] = (1 - p_x_1_y[i, k])
x_temp *= p_y[i]
for j in range(p_x.shape[1]):
x_temp[:, j] /= p_x[0, j]
p_y_x_matrix[:, i] = x_temp.sum(axis=1) / x_temp.shape[1]
return p_y_x_matrix
我尽力了,但我的数学逻辑一定有错误。