今天进行主成分分析的练习
主成分分析的功能:减少系统的维数,保留足以描述数据点特征的信息
scikit-learn库中fit-tranform()函数就是用来降维的函数,
使用前先导入PCA模块sklearn.decomposition,
然后使用PCA()构造函数
用n_components选项制定要降到几维
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.decomposition import PCA
iris = datasets.load_iris()
x = iris.data[:,2]
y = iris.data[:,3]
species = iris.target
x_reduced = PCA(n_components = 3).fit_transform(iris.data)
#scatterplot 3D
fig = plt.figure()
ax = Axes3D(fig)
ax.set_title('Iris Dataset by PCA', size = 14)
ax.scatter(x_reduced[:,0],x_reduced[:,1],x_reduced[:,2], c = species)
ax.set_xlabel('First eigenvector')
ax.set_ylabel('Second eigenvector')
ax.set_zlabel('Third eigenvector')
ax.w_xaxis.set_ticklabels(())
ax.w_yaxis.set_ticklabels(())
ax.w_zaxis.set_ticklabels(())
plt.show()