1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| >>> from sklearn.decomposition import TruncatedSVD >>> from scipy.sparse import csr_matrix >>> import numpy as np >>> np.random.seed(0) >>> X_dense = np.random.rand(100, 100) >>> X_dense[:, 2 * np.arange(50)] = 0 >>> X = csr_matrix(X_dense) >>> svd = TruncatedSVD(n_components=5, n_iter=7, random_state=42) >>> svd.fit(X) TruncatedSVD(n_components=5, n_iter=7, random_state=42) >>> print(svd.explained_variance_ratio_) [0.0157... 0.0512... 0.0499... 0.0479... 0.0453...] >>> print(svd.explained_variance_ratio_.sum()) 0.2102... >>> print(svd.singular_values_) [35.2410... 4.5981... 4.5420... 4.4486... 4.3288...]
|