You dont have to sort explianed_variance_ratio
, output itself would be sorted and contains only the n_component
number of values.
From Documentation:
TruncatedSVD implements a variant of singular value decomposition
(SVD) that only computes the largest singular values, where k is a
user-specified parameter.
X_transformed contains the decomposition using only k components.
The example would give you an idea
>>> from sklearn.decomposition import TruncatedSVD
>>> from sklearn.random_projection import sparse_random_matrix
>>> X = sparse_random_matrix(100, 100, density=0.01, random_state=42)
>>> svd = TruncatedSVD(n_components=5, n_iter=7, random_state=42)
>>> svd.fit(X)
TruncatedSVD(algorithm='randomized', n_components=5, n_iter=7,
random_state=42, tol=0.0)
>>> print(svd.explained_variance_ratio_)
[0.0606... 0.0584... 0.0497... 0.0434... 0.0372...]
>>> print(svd.explained_variance_ratio_.sum())
0.249...
>>> print(svd.singular_values_)
[2.5841... 2.5245... 2.3201... 2.1753... 2.0443...]