# LinAlgError in executinmultivariate_gaussian

Hi,

I get below LinAlgError in Section 2.3.1 of anomaly detection lab. The test that checks mean, variance passed. But I get below error in multivariate_gaussian

# at each data point (row) of X_train

p = multivariate_gaussian(X_train, mu, var)

LinAlgError Traceback (most recent call last)
in
1 # Returns the density of the multivariate normal
2 # at each data point (row) of X_train
----> 3 p = multivariate_gaussian(X_train, mu, var)
4
5 #Plotting code

~/work/utils.py in multivariate_gaussian(X, mu, var)
31
32 X = X - mu
—> 33 p = (2* np.pi)(-k/2) * np.linalg.det(var)(-0.5) *
34 np.exp(-0.5 * np.sum(np.matmul(X, np.linalg.pinv(var)) * X, axis=1))
35

<array_function internals> in det(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/linalg/linalg.py in det(a)
2111 a = asarray(a)
2112 _assert_stacked_2d(a)
→ 2113 _assert_stacked_square(a)
2114 t, result_t = _commonType(a)
2115 signature = ‘D->D’ if isComplexType(t) else ‘d->d’

/opt/conda/lib/python3.7/site-packages/numpy/linalg/linalg.py in _assert_stacked_square(*arrays)
211 m, n = a.shape[-2:]
212 if m != n:
→ 213 raise LinAlgError(‘Last 2 dimensions of the array must be square’)
214
215 def _assert_finite(*arrays):

LinAlgError: Last 2 dimensions of the array must be square

This means there is an error in your estimate_gaussian() function - since that is where the values for mu and var are determined.

That’s right @TMosh but I did pass the test for estimate_gaussian() method. The error suggests that the variance must be a square matrix - nxn for n features but in the test I have to output it as a nx1 vector to match the expected result.

Passing the tests in the notebook does not guarantee your code is perfect.

Hi @TMosh, Based on the error message I am not able to understand what is wrong in my code. I sent you a direct message with my notebook attached, could you please help take a look at it?

Note also, on careful inspection, your code did not give the expected results for var.

Be sure your code computes var correctly.

Hint: It should not be a square matrix.