C3 W4 NB 2 - Closest Neg ques

I have a question about the “Closest Negative” code in the lecture notebook.

Notice that this code from the NB does not actually print sim_ap, in prints np.diag(sim_ap).
sim_ap = np.diag(sim)
print(“sim_ap :”)
print(np.diag(sim_ap), “\n”)

If I visually construct the mask matrix using the matrix printed here as sim_ap, I get a different result than the mask matrix the code produces. If I use “sim_an > sim_ap”, with sim_ap as printed, to construct mask_2 this is ‘T’ where the off-diag elements of sim are positive and where the diag elements are negative. So visually constructing mask_2 I get:

F F T F
F F T F
T T T F
F F F F

However, the code uses the command “mask_2 = sim_an > sim_ap.reshape(b, 1)”. This produces a different mask_2 than my visual inspection using sim_an > sim_ap. Is the code correct? If so, it would make sense to modify the comments and the output to match the code. Or am I missing something?

Thank you

Hi John8,

Thanks for noticing this.

The statement print(“simp_ap :”) followed by print(np.diag(sim_ap), “\n”) indeed creates some confusion. To make it clearer what is going on it would be better to simply use print(sim_ap, “\n”). This would make it easier to understand what sim_an > sim_ap.reshape(b, 1) is doing (comparing per row each element of sim_an to the diagonal value).

I’ll make a note of this for the team working on the backend.

Thanks again!