PCA降维使用
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
iris=load_iris()
X=iris.data
y=iris.target
pca=PCA(n_components=2)
X_dc=pca.fit_transform(X)
看看降维后特征向量的信息量
pca.explained_variance_
查看降维后特征的信息量占原特征信息量的比例
pca.explained_variance_ratio_
pca.explained_variance_ratio_.sum()
用极大似然估计选取n_components,即降维后个数
pca_mle=PCA(n_components='mle')
X_mle=pca_mle.fit_transform(X)
pca_mle.explained_variance_ratio_.sum()
按信息量所占比例选取n_components
svd_solver=auto/full/arpack(特征矩阵为稀疏矩阵)/randomized(适合计算量大)
pca_full=PCA(n_components=0.99, svd_solver='full')
X_full=pca_full.fit_transform(X)
pca_full.explained_variance_ratio_.sum()
查看SVD矩阵分解中的特征向量矩阵
pca_full.components_
PCA 人脸应用
from sklearn.datasets import fetch_lfw_people
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np
faces=fetch_lfw_people(min_faces_per_person=30)
faces.images.shape
X=faces.data
pca=PCA(150)
X_dc=pca.fit_transform(X)
#逆转
X_inverse=pca.inverse_transform(X_dc)
plt.imshow(X[0,:].reshape(62,47), cmap='gray')
plt.show()
pca.explained_variance_ratio_.sum()
网友评论