Mr.M
Mr.M

Reputation: 5

Linearly independent matrix in python

I have a matrix which is 15714 x 541 and it seems that this matrix is not linearly independent. How can I remove columns that are not linearly independent?

I tried using this solution from this post: elimination the linear dependent columns of a non-square matrix in python

But, it says: ValueError: Item wrong length 541 instead of 15714.

Q, R = np.linalg.qr(fixeff.T)
fixeff[np.abs(np.diag(R))>=1e-10]

fixeff is matrix that I described.

Upvotes: 0

Views: 335

Answers (1)

Miguel
Miguel

Reputation: 712

You could do it with scipy.linalg.qr:

from scipy.linalg import qr

Q, R, P = qr(A, mode="economic", pivoting=True)
inv = P.argsort() # reversed order (necessary here)
good_columns = (np.abs(np.diag(R)) > 1e-10)[inv]

A = A[:,good_columns]

Upvotes: 1

Related Questions