python - Naive Bayes P(Y|X) distribution modelling -


my task write function return p(y|x) distribution each of class (with use of naive bayes classifier). result n x m matrix.

input data

`p_y` -> prior probabilities labels 1 x m   `p_x_1_y` -> probability distribution p(x = 1|y), matrix m x d   `x` -> data 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 far, if try out math logic, cause must missing provided test not passing.

p_y_x_matrix = np.zeros([x.shape[0], len(p_y)]) p_x = np.sum(x, axis=0) / x.shape[0] in range(len(p_y)):     x_temp = np.zeros(x.shape)     j in range(x.shape[0]):         k in range(x.shape[1]):             if x[j, k]:                x_temp[j, k] += p_x_1_y[i, k]             elif x[j, k] not true:                x_temp[j, k] = (1 - p_x_1_y[i, k])     x_temp *= p_y[i]     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 best, there must error math logic.


Comments