My task is to write function which return P(y|x) distribution for each of class (with use of Naive Bayes classifier). The result is N x M matrix.
Input data
`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
Current Code
My try so far, if someone could try out with math logic, cause I must be missing something as provided test are not passing.
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
I tried my best, but there must be some error with my math logic.