今天進(jìn)行主成分分析的練習(xí)
主成分分析的功能:減少系統(tǒng)的維數(shù)角骤,保留足以描述數(shù)據(jù)點(diǎn)特征的信息
scikit-learn庫(kù)中fit-tranform()函數(shù)就是用來(lái)降維的函數(shù),
使用前先導(dǎo)入PCA模塊sklearn.decomposition,
然后使用PCA()構(gòu)造函數(shù)
用n_components選項(xiàng)制定要降到幾維
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()