Reputation: 388
I have a square matrix and want to use svd to reduce condition number of the matrix by elimination of some rows/columns.
I used numpy/scipy both give sorted list of singular values.
Using sorted list, I can easily reconstruct a smaller matrix by discarding some small singular values. But it is difficult to map to the original matrix, that what values have been eliminated and what values have been retained. I need that further.
Is there any way to identify later for the original matrix, what indices have been retained and what discarded.
Upvotes: 1
Views: 1699
Reputation: 2590
To perform a singular value decomposition of a matrix you can look at the .linalg` module in numpy.
A SVD of a matrix factorizes it into the product of three matrices:
M = U S V*
M is your original matrix. S is a rectangular diagonal matrix with the ('sorted') singular values on the diagonals. U and V are known as the left- and right-singular vectors respectively.
Note: np.linalg.svd doesn't return S but s which is just a 1D array containing the singular values.
Practical implementation
Lets say you have an (m x q) feature space represented by the 2D array X
, where X
is a centered matrix. You can calculate its SVD:
U, s, Vt = np.linalg.svd(X)
where the t
denotes the transpose of V and s is your 'unsorted list of singular values'.
You can then project your original feature space to n
dimensions by using the singular vectors and discarding singular vectors which preserve the least variance:
X_projected = X.dot(Vt.T[:,:n])
where X_projected
is now the representation of your feature space in the lower n-dimensional space.
Importantly, you can transform back from your reduced feature space to your original space:
X_recovered = X_projected.dot(Vt[:,:n])
Notably, this can be used to measure the information lost in your reduced feature set by comparing X_recovered
to your original feature set (X
) to measure things such as reconstruction error.
Upvotes: 1