C3_W2 mean calculation logic

Hello,

this is more a theoretical question: can someone please explain, why is the mean calculation invoked the following way in the code:

tsmean = np.mean(Y[0, R[0, :].astype(bool)])

I tried to visualize it somehow, but still cannot grasp the variables called. Thanks!

Gábor

1 Like

Hello Gábor,

I suggest you to analyze the code with some simple inputs, then delineate the code into smaller blocks, and print those blocks out.

Y = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
R = np.array([[1, 0, 0, 1], [1, 1, 1, 0]])

tmp1 = R[0, :].astype(bool)
print(tmp1)

tmp2 = Y[0, tmp1]
print(tmp2)

tmp3 = np.mean(tmp2)
print(tmp3)

If you understand the results in the three tmp variables, then you might try to replace the simple inputs with what’s provided in the lab, or replace them with another set of inputs that you think worth examining.

Cheers,
Raymond