代码笔记
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
iris = load_iris()
y = iris.target
X = iris.data
import pandas as pd
pd.DataFrame(X)
pca = PCA(n_components=2) #将特征降为二维
pca = pca.fit(X)
X_dr = pca.transform(X)
X_dr
color = ["red","green","blue"]
plt.figure()
for i in [0,1,2]:
plt.scatter(X_dr[y==i, 0]
,X_dr[y==i, 1]
,alpha = 0.7
,c=color[i]
,label=iris.target_names[i])
plt.legend()
plt.title('PCA of IRIS dataset')
plt.show()
查看pca相关的属性
pca.explained_variance_ #属性explained_variance 查看降维后每个新特征向量上所带的信息量大小,可解释性方差的大小,方差越大,该特征越重要
#属性explained_variance_ratio 查看降维后每个新特征向量所占的信息量占原始数据总信息量的比例
pca.explained_variance_ratio_
c = pca.explained_variance_ratio_.sum()
print(c)
特征维数跟原始信息量的关系
import numpy as np
pca_line = PCA().fit(X)
plt.plot([1,2,3,4], np.cumsum(pca_line.explained_variance_ratio_))
plt.xticks([1,2,3,4])
plt.xlabel("number of components after dimension reduction")
plt.ylabel("cumulative explained variance")
plt.show()
随着特征维数的上升,能表达的信息跟原始信息越接近。