C3_W1_Anomaly_Detection IndexError when having 2 nested loop to implement estimate_gaussian

For the method estimate_gaussian, I know it is better to implemented in a vectorized manner by using np.sum() with axis = 0 parameter.
However, I just wanna check is my logic correct when having 2 nested for loops

    mu = np.zeros(n)
    var = np.zeros(n)
    for i in range(n):
        sum1 = 0
        for j in range(m):
            sum1 += X[i][j]
        mu[i] = sum1 / m
        sum2 = 0
        for j in range(m):
            sum2 += (X[i][j] - mu[i]) **2
        var[i] = sum2 / m

When I run the above code, it throws an IndexError: index 2 is out of bounds for axis 0 with size 2
I am quite confused.
Could anyone help to explain why there is that index error?
Thanks in advance!

As written in the unmodified code above yours in the cell:
m, n = X.shape

which means that X has m rows and n columns, try to make the range of the outer loop range(m) and the other loops range(n).

I don’t know if this would calculate the right value, but it will solve your index error.

Hi @kathyH
you used sum1 += X[i][j] but you replace i by j (replaced columns by rows and vice versa) use that sum1 += X[j,i] and in sum2 += (X[i][j] - mu[i]) **2 use also that sum2 += (X[j,i] - mu[i]) **2 and it will run correctly but not efficiently as I didn’t recommend to use for loop specially nested for loop as it take to much time and not accurate such as matrix multiplication

Please feel free to ask any questions,
Thanks,
Abdelrahman

Yeah, thank you so much.

1 Like

Get it. Thank you again :slight_smile: