我正在尝试获取分类所需的组件数量。我已经阅读了类似的问题Finding the dimension with highest variance using scikit-learn PCA和关于此的scikit文档:
然而,这仍然没有解决我的问题。我的所有PCA组件都非常大,因为我可以选择所有这些组件,但如果我这样做,PCA将毫无用处。
我还在scikit中阅读了PCA库 http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html 它表明L:
如果n_components =='mle',则会使用Minka的MLE 猜测尺寸是否为0< n_components< 1,选择数量 组件使得需要解释的方差量 大于n_components指定的百分比
但是,我无法找到有关使用此技术分析PCA的n_components的更多信息
以下是我的PCA分析代码:
from sklearn.decomposition import PCA
pca = PCA()
pca.fit(x_array_train)
print(pca.explained_variance_)
结果:
[ 6.58902714e+50 6.23266555e+49 2.93568652e+49 2.25418736e+49
1.10063872e+49 3.25107359e+40 4.72113817e+39 1.40411862e+39
4.03270198e+38 1.60662882e+38 3.20028861e+28 2.35570241e+27
1.54944915e+27 8.05181151e+24 1.42231553e+24 5.05155955e+23
2.90909468e+23 2.60339206e+23 1.95672973e+23 1.22987336e+23
9.67133111e+22 7.07208772e+22 4.49067983e+22 3.57882593e+22
3.03546737e+22 2.38077950e+22 2.18424235e+22 1.79048845e+22
1.50871735e+22 1.35571453e+22 1.26605081e+22 1.04851395e+22
8.88191944e+21 6.91581346e+21 5.43786989e+21 5.05544020e+21
4.33110823e+21 3.18309135e+21 3.06169368e+21 2.66513522e+21
2.57173046e+21 2.36482212e+21 2.32203521e+21 2.06033130e+21
1.89039408e+21 1.51882514e+21 1.29284842e+21 1.26103770e+21
1.22012185e+21 1.07857244e+21 8.55143095e+20 4.82321416e+20
2.98301261e+20 2.31336276e+20 1.31712446e+20 1.05253795e+20
9.84992112e+19 8.27574150e+19 4.66007620e+19 4.09687463e+19
2.89855823e+19 2.79035170e+19 1.57015298e+19 1.39218538e+19
1.00594159e+19 7.31960049e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.29043685e+18 5.29043685e+18 5.29043685e+18
5.29043685e+18 5.24952686e+18 2.09685699e+18 4.16588190e+17]
我尝试了PCA(n_components =' mle')但是我遇到了这些错误..
Traceback (most recent call last):
File "xx", line 166, in <module>
pca.fit(x_array_train)
File "xx", line 225, in fit
self._fit(X)
File "/Users/lib/python2.7/site-packages/sklearn/decomposition/pca.py", line 294, in _fit
n_samples, n_features)
File "/Users/lib/python2.7/site-packages/sklearn/decomposition/pca.py", line 98, in _infer_dimension_
ll[rank] = _assess_dimension_(spectrum, rank, n_samples, n_features)
File "/Users/lib/python2.7/site-packages/sklearn/decomposition/pca.py", line 83, in _assess_dimension_
(1. / spectrum_[j] - 1. / spectrum_[i])) + log(n_samples)
ValueError: math domain error
非常感谢任何帮助...
答案 0 :(得分:5)
我没有使用Python
,但我在C++
&amp; opencv
。希望你成功将它转换成任何语言。
// choose how many eigenvectors you want:
int nEigensOfInterest = 0;
float sum = 0.0;
for (int i = 0; i < mEiVal.rows; ++i)
{
sum += mEiVal.at<float>(i, 0);
if (((sum * 100) / (sumOfEigens)) > 80)
{
nEigensOfInterest = i;
break;
}
}
logfile << "No of Eigens of interest: " << nEigensOfInterest << std::endl << std::endl;
基本思想是决定&#34;无论%&#34;你需要继续做的组件。我选择那些80
。 mEiVal
是按降序排序的特征值的列矩阵。 sumOfEigens
是所有特征值的总和。
我没有使用scikit-learn
的经验,请告诉我,我会删除答案。
答案 1 :(得分:3)
我自己也是这样学习的,但在我看来,使用0 < n_components < 1
的提法表明您可以将n_components
设置为0.85,以及组件的确切数量你需要解释85%的方差将被使用。您还可以通过打印sum(pca.explained_variance_)
来验证是否选择了正确数量的组件。您应该获得超过0.85(或您选择的任何值)的最小方差百分比总和。
当然,有更多复杂的方法可以选择多个组件,但70%-90%的经验法则是一个合理的开始。
答案 2 :(得分:0)
关于在pca中找到相关特征值的数量的主题,有几个可用的调查。我喜欢破碎的方法和并行分析。谷歌他们或看看the tutorial of this lecture。
答案 3 :(得分:0)
我假设您的火车阵列(x_array_train
)像这样标准化:
from sklearn.preprocessing import StandardScaler
x_array_train = StandardScaler().fit_transform(x_array)
尽管如此,我的解决方案如下:
from sklearn.decomposition import PCA as pca
your_pca = pca(n_components = "mle", svd_solver ="full")
your_pca.fit_transform(x_array_train)
print(your_pca.explained_variance_)
通过这种方式,您应该获得mle
算法所允许的尽可能少的主成分