# Gaussian PDF code

Hello team, I am stuck on the following:

I tried implementing Gaussian PDF in code using the normal distribution formula (is it correct?)

def log_prob(train_features, mu_y, sigma_y, log_py):
N, d = train_features.shape

# Calculate the log Gaussian probability density for each class
log_gauss_part_one = 1 / (sigma_y * np.sqrt(2 * np.pi))
log_gauss_part_two =  np.exp((-(d - mu_y)**2)/(2 * (sigma_y)**2))
log_entire_gaussian = log_gauss_part_one * log_gauss_part_two
log_p_x_y = np.log(log_entire_gaussian)
assert log_p_x_y.shape == (N, 2)

return log_p_x_y

However, it is failing the Unit test:

# Performing sanity checks on your implementation
some_feats = np.array([[  1. ,  85. ,  66. ,  29. ,   0. ,  26.6,   0.4,  31. ],
[  8. , 183. ,  64. ,   0. ,   0. ,  23.3,   0.7,  32. ],
[  1. ,  89. ,  66. ,  23. ,  94. ,  28.1,   0.2,  21. ],
[  0. , 137. ,  40. ,  35. , 168. ,  43.1,   2.3,  33. ],
[  5. , 116. ,  74. ,   0. ,   0. ,  25.6,   0.2,  30. ]])
some_labels = np.array([0, 1, 0, 1, 0])

some_mu_y = cc_mean_ignore_missing(some_feats, some_labels)
some_std_y = cc_std_ignore_missing(some_feats, some_labels)
some_log_py = log_prior(some_labels)

some_log_p_x_y = log_prob(some_feats, some_mu_y, some_std_y, some_log_py)

assert np.array_equal(some_log_p_x_y.round(3), np.array([[ -20.822,  -36.606],
[ -60.879,  -27.944],
[ -21.774, -295.68 ],
[-417.359,  -27.944],
[ -23.2  ,  -42.6  ]]))

# Checking against the pre-computed test database
assert test_results['passed'], test_results['message']

This is the Error message I get:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/tmp/ipykernel_60/800464219.py in <module>
11 some_log_py = log_prior(some_labels)
12
---> 13 some_log_p_x_y = log_prob(some_feats, some_mu_y, some_std_y, some_log_py)
14
15 assert np.array_equal(some_log_p_x_y.round(3), np.array([[ -20.822,  -36.606],

/tmp/ipykernel_60/3826132914.py in log_prob(train_features, mu_y, sigma_y, log_py)
8     log_entire_gaussian = log_gauss_part_one * log_gauss_part_two
9     log_p_x_y = np.log(log_entire_gaussian)
---> 10     assert log_p_x_y.shape == (N, 2)
11
12     return log_p_x_y

AssertionError:

I wasn’t able to find the assignment you’re working on.
Please can you give the course name, assignment and week, and the title of the notebook file.