简体   繁体   English

Python sklearn PCA.explained_variance_ratio_不等于1

[英]Python sklearn PCA.explained_variance_ratio_ doesn't sum to 1

I am using sklearn's PCA module. 我正在使用sklearn的PCA模块。 I am using the following code to set up the analysis. 我正在使用以下代码来设置分析。

from sklearn.decomposition import PCA
pca = PCA(n_components=9)
p = pca.fit([row[:-1] for row in norm])

norm here is my normalized dataset, with a unique identifier in the last column, which is why I am removing it in the last line. 这里的norm是我的规范化数据集,在最后一列中有一个唯一的标识符,这就是为什么我在最后一行中将其删除。 There are 9 features in this dataset, so I would expect with 9 components that there would be no unexplained variance. 此数据集中有9个功能部件,因此我希望有9个组件不会有无法解释的方差。 When I call p.explained_variance_.cumsum() however, I get: 但是,当我调用p.explained_variance_.cumsum()时,我得到:

[ 0.06589563  0.08608778  0.09578116  0.10150195  0.10703567  0.11036608
  0.11241904  0.11422285  0.11591605]

Am I misunderstanding something about PCA? 我是否对PCA有误解? I have used this module before with no issue, but it's been a while. 之前我都没用过这个模块,但是已经有一段时间了。 Am I setting this up wrong? 我设置错了吗? I stripped my data of any identifying information to post here. 我剥离了所有识别信息的数据以发布到此处。 Below is a subset of the data that seems to be replicating the issue. 以下是似乎正在复制问题的数据子集。

[0.3888888888888889, 0.3888888888888889, 0.3888888888888889, 0.436943311456892, 0.7905900031193156, 0.5020468092219706, 0.8389717734280283, 0.7604923090797432, 0.8206054422776056, '0']
[0.3888888888888889, 0.3888888888888889, 0.2222222222222222, 0.4457200178477334, 0.8114779465247448, 0.506899600792241, 0.8368566485573798, 0.760617288778523, 0.8195489478905984, '1']
[0.2777777777777778, 0.2777777777777778, 0.05555555555555555, 0.4426231291814084, 0.7883413226205706, 0.5037172133121759, 0.8370362549229062, 0.7599752704033258, 0.8184218722901648, '2']
[0.1111111111111111, 0.1111111111111111, 0.16666666666666666, 0.4651807845446571, 0.7983379003654792, 0.5250604537887904, 0.8463875215362144, 0.7533582308429306, 0.8241548325954007, '3']
[0.5000000000000001, 0.5000000000000001, 0.3333333333333333, 0.4457200178477334, 0.7878040593905666, 0.506899600792241, 0.8368566485573798, 0.7605016058324149, 0.8195489478905984, '4']
[0.3888888888888889, 0.3888888888888889, 0.2222222222222222, 0.44943322185630036, 0.7843622888520198, 0.5055757644148106, 0.8351253941103399, 0.7604171267769607, 0.8185442945328569, '5']
[0.3888888888888889, 0.3888888888888889, 0.3333333333333333, 0.4424914587425397, 0.7877430312713435, 0.5029950110274568, 0.836692391332608, 0.760611529525946, 0.8198150075184326, '6']
[0.3333333333333333, 0.05555555555555555, 0.7777777777777778, 0.4389415113841421, 0.7878040593905666, 0.506899600792241, 0.8368566485573798, 0.7605016058324149, 0.8195489478905984, '7']
[0.4444444444444444, 0.4444444444444444, 0.4444444444444444, 0.42770705188736874, 0.7976039510596705, 0.5057230657076256, 0.8368566485573798, 0.7605016058324149, 0.8195489478905984, '8']
[0.2222222222222222, 0.2777777777777778, 0.5000000000000001, 0.43182322765312314, 0.7971732873351607, 0.5072390458086798, 0.84541364942531, 0.7613416598875292, 0.8239037851005895, '9']

Here is an example with the iris dataset including a scree plot: 这是虹膜数据集包括碎石图的示例:

在此处输入图片说明


Trying to reproduce your problem with the dataset you just posted: 尝试使用刚刚发布的数据集重现您的问题:

d = matrix([[0.3888888888888889, 0.3888888888888889, 0.3888888888888889, 0.436943311456892, 0.7905900031193156, 0.5020468092219706, 0.8389717734280283, 0.7604923090797432, 0.8206054422776056, '0'],
[0.3888888888888889, 0.3888888888888889, 0.2222222222222222, 0.4457200178477334, 0.8114779465247448, 0.506899600792241, 0.8368566485573798, 0.760617288778523, 0.8195489478905984, '1'],
[0.2777777777777778, 0.2777777777777778, 0.05555555555555555, 0.4426231291814084, 0.7883413226205706, 0.5037172133121759, 0.8370362549229062, 0.7599752704033258, 0.8184218722901648, '2'],
[0.1111111111111111, 0.1111111111111111, 0.16666666666666666, 0.4651807845446571, 0.7983379003654792, 0.5250604537887904, 0.8463875215362144, 0.7533582308429306, 0.8241548325954007, '3'],
[0.5000000000000001, 0.5000000000000001, 0.3333333333333333, 0.4457200178477334, 0.7878040593905666, 0.506899600792241, 0.8368566485573798, 0.7605016058324149, 0.8195489478905984, '4'],
[0.3888888888888889, 0.3888888888888889, 0.2222222222222222, 0.44943322185630036, 0.7843622888520198, 0.5055757644148106, 0.8351253941103399, 0.7604171267769607, 0.8185442945328569, '5'],
[0.3888888888888889, 0.3888888888888889, 0.3333333333333333, 0.4424914587425397, 0.7877430312713435, 0.5029950110274568, 0.836692391332608, 0.760611529525946, 0.8198150075184326, '6'],
[0.3333333333333333, 0.05555555555555555, 0.7777777777777778, 0.4389415113841421, 0.7878040593905666, 0.506899600792241, 0.8368566485573798, 0.7605016058324149, 0.8195489478905984, '7'],
[0.4444444444444444, 0.4444444444444444, 0.4444444444444444, 0.42770705188736874, 0.7976039510596705, 0.5057230657076256, 0.8368566485573798, 0.7605016058324149, 0.8195489478905984, '8'],
[0.2222222222222222, 0.2777777777777778, 0.5000000000000001, 0.43182322765312314, 0.7971732873351607, 0.5072390458086798, 0.84541364942531, 0.7613416598875292, 0.8239037851005895, '9']])

在此处输入图片说明

Amoeba on CV stack exchange gave me the answer in the end - it was a simple typo. 最后,在CV堆栈交换上的变形虫给了我答案-这是一个简单的错字。 I called p.explained_variance_.cumsum() but the proper method call was p.explained_variance_ratio_.cumsum() . 我调用了p.explained_variance_.cumsum()但正确的方法调用是p.explained_variance_ratio_.cumsum() Of course the variance doesn't have to sum to one! 当然,差异不一定要加一!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM