1

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.

Neil
  • 14,063
  • 3
  • 30
  • 51
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. This includes providing the problematic input and output. – Prune Apr 05 '17 at 18:02
  • 1
    it's one method, i cannot make it simplier – George Tahamo Apr 05 '17 at 19:25
  • Please include problematic input and expected output! – ventiseis Apr 05 '17 at 20:26
  • 1
    That's only the "minimal" part. Your posted code is neither complete nor verifiable. – Prune Apr 05 '17 at 20:27
  • 1
    The input data descriptions are helpful, but it would be useful to see some example data. – brennan Apr 05 '17 at 20:58
  • Are you aware of `np.dot`? It does matrix multiplication. So does the `@` operator for numpy arrays. – Mad Physicist Apr 06 '17 at 19:38

0 Answers0