为什么 Sklearn TruncatedSVD 的解释方差比不是按降序排列的?
Why Sklearn TruncatedSVD's explained variance ratios are not in descending order?
为什么 Sklearn.decomposition.TruncatedSVD
的解释方差比不按奇异值排序?
我的代码如下:
X = np.array([[1,1,1,1,0,0,0,0,0,0,0,0,0,0],
[0,0,1,1,1,1,1,1,1,0,0,0,0,0],
[0,0,0,0,0,0,1,1,1,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,1,1,1]])
svd = TruncatedSVD(n_components=4)
svd.fit(X4)
print(svd.explained_variance_ratio_)
print(svd.singular_values_)
结果:
[0.17693405 0.46600983 0.21738089 0.13967523]
[3.1918354 2.39740372 1.83127499 1.30808033]
听说奇异值是指成分能解释数据的程度,所以我认为解释方差比也是按照奇异值的顺序排列的。
但是比率不是按降序排列的。
有人可以解释为什么会这样吗?
I heard that a singular value means how much the component can explain data
这适用于 PCA,但不适用于(截断的)SVD;在 explained_variance_ratio_
属性甚至无法用于 TruncatedSVD
的那一天引用相关的 Github thread(2014 - 强调我的):
preserving the variance is not the exact objective function of truncated SVD without centering
因此,奇异值本身确实按降序排序,但如果数据不是 居中.[=23,则这不一定适用于相应的解释方差比=]
但是如果我们之前确实将数据居中,那么解释的方差比实际上是按降序排列的,与奇异值本身相对应:
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import TruncatedSVD
sc = StandardScaler()
Xs = sc.fit_transform(X) # X data from the question here
svd = TruncatedSVD(n_components=4)
svd.fit(Xs)
print(svd.explained_variance_ratio_)
print(svd.singular_values_)
结果:
[4.60479851e-01 3.77856541e-01 1.61663608e-01 8.13905807e-66]
[5.07807756e+00 4.59999633e+00 3.00884730e+00 8.21430014e-17]
有关 PCA 和 SVD 计算中居中和非居中数据之间的数学和计算差异,请参阅 How does centering make a difference in PCA (for SVD and eigen decomposition)?
关于 TruncatedSVD
本身的使用,这里又是用户 ogrisel(scikit-learn 贡献者)在 Difference between scikit-learn implementations of PCA and TruncatedSVD 的相关回答中:
In practice TruncatedSVD
is useful on large sparse datasets which cannot be centered without making the memory usage explode.
所以,目前还不清楚您为什么选择在此处使用 TruncatedSVD
,但是,如果您没有导致内存问题的过大数据集,我想您应该改用 PCA .
为什么 Sklearn.decomposition.TruncatedSVD
的解释方差比不按奇异值排序?
我的代码如下:
X = np.array([[1,1,1,1,0,0,0,0,0,0,0,0,0,0],
[0,0,1,1,1,1,1,1,1,0,0,0,0,0],
[0,0,0,0,0,0,1,1,1,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,1,1,1]])
svd = TruncatedSVD(n_components=4)
svd.fit(X4)
print(svd.explained_variance_ratio_)
print(svd.singular_values_)
结果:
[0.17693405 0.46600983 0.21738089 0.13967523]
[3.1918354 2.39740372 1.83127499 1.30808033]
听说奇异值是指成分能解释数据的程度,所以我认为解释方差比也是按照奇异值的顺序排列的。 但是比率不是按降序排列的。
有人可以解释为什么会这样吗?
I heard that a singular value means how much the component can explain data
这适用于 PCA,但不适用于(截断的)SVD;在 explained_variance_ratio_
属性甚至无法用于 TruncatedSVD
的那一天引用相关的 Github thread(2014 - 强调我的):
preserving the variance is not the exact objective function of truncated SVD without centering
因此,奇异值本身确实按降序排序,但如果数据不是 居中.[=23,则这不一定适用于相应的解释方差比=]
但是如果我们之前确实将数据居中,那么解释的方差比实际上是按降序排列的,与奇异值本身相对应:
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import TruncatedSVD
sc = StandardScaler()
Xs = sc.fit_transform(X) # X data from the question here
svd = TruncatedSVD(n_components=4)
svd.fit(Xs)
print(svd.explained_variance_ratio_)
print(svd.singular_values_)
结果:
[4.60479851e-01 3.77856541e-01 1.61663608e-01 8.13905807e-66]
[5.07807756e+00 4.59999633e+00 3.00884730e+00 8.21430014e-17]
有关 PCA 和 SVD 计算中居中和非居中数据之间的数学和计算差异,请参阅 How does centering make a difference in PCA (for SVD and eigen decomposition)?
关于 TruncatedSVD
本身的使用,这里又是用户 ogrisel(scikit-learn 贡献者)在 Difference between scikit-learn implementations of PCA and TruncatedSVD 的相关回答中:
In practice
TruncatedSVD
is useful on large sparse datasets which cannot be centered without making the memory usage explode.
所以,目前还不清楚您为什么选择在此处使用 TruncatedSVD
,但是,如果您没有导致内存问题的过大数据集,我想您应该改用 PCA .