I have the following problem: I need the inverse of the covariance matrix using the following data:
x = [255, 239, 213]
y = [255, 240, 245]
I get the following covariance matrix using numpy.cov()
cov_matrix = np.cov(np.array([[255.0, 255.0], [239.0, 240.0], [213.0, 245.0]]))
Then, when I use numpu.linalg.inv() I get this error
inv_matrix = np.linalg.inv(cov_matrix)
LinAlgError: Singular matrix
How is it possible?
You are computing cov_matrix
in the wrong way. Assuming you have 3 observation for two different variables x and y:
x = [255, 239, 213]
y = [255, 240, 245]
cov_matrix = np.cov(x, y)
#array([[449.33333333, 88.33333333],
# [ 88.33333333, 58.33333333]])
inv_matrix = np.linalg.inv(cov_matrix)
#array([[ 0.00316885, -0.00479855],
# [-0.00479855, 0.02440923]])
In case your x and your y are instead two observations on a 3D space, you are computing cov_matrix
in the right way, but you'll have a zero-variance variable (the first one), since var[(255,255)]=0
and this produces a zero-determinant covariance matrix, as @Jasper Row has already said.