[英]Recovering features names of explained_variance_ratio_ in PCA with sklearn
[英]project PCA back into original scales with explained_variance_ratio_ condition
使用scikit時,我有2個關於PCA的問題。
假設我有以下數據:
fullmatrix =[[2.5, 2.4],
[0.5, 0.7],
[2.2, 2.9],
[1.9, 2.2],
[3.1, 3.0],
[2.3, 2.7],
[2.0, 1.6],
[1.0, 1.1],
[1.5, 1.6],
[1.1, 0.9]]
現在,我進行PCA計算:
from sklearn.decomposition import PCA as PCA
sklearn_pca = PCA()
Y_sklearn = sklearn_pca.fit_transform(fullmatrix)
print Y_sklearn # Y_sklearn is now the Data transformed with 2 eigenvectors
sklearn_pca.explained_variance_ratio_ # variance explained by each eigenvector
print sklearn_pca.explained_variance_ratio_
sklearn_pca.components_ # eigenvectors order by highest eigenvalue
print sklearn_pca.components_
第一個問題:如何將這個Y_sklearn投影回原始比例? (我知道我們應該使用所有特征向量來獲取與全矩陣相同的數據,只是為了檢查是否正確)。
第二個問題:如何輸入有關“ sklearn_pca.explained_variance_ratio_”的最小可接受總方差的閾值? 例如,假設我要一直使用特征向量,直到達到95%以上的總explained_variance_ratio_。 在這種情況下很容易,我們只使用第一個特征向量即可,其解釋為0.996318131%。 但是,我們如何才能以更自動化的方式做到這一點呢?
首先: sklearn_pca.inverse_transform(Y_sklearn)
第二:
thr = 0.95
# Is cumulative sum exceeds some threshold
is_exceeds = np.cumsum(sklearn_pca.explained_variance_ratio_) >= thr
# Which minimal index provides such variance
# We need to add 1 to get minimum number of eigenvectors for saving this variance
k = np.min(np.where(is_exceeds))+1
# Or you can just initialize your model with thr parameter
sklearn_pca = PCA(n_components = thr)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.