程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Algorithm notes (14) PCA principal component analysis and Python code implementation

編輯:Python

Dimensionality reduction is a preprocessing method for high-dimensional feature data . There are many dimensionality reduction algorithms , Like singular value decomposition (SVD)、 Principal component analysis (PCA)、 Factor analysis (FA)、 Independent component analysis (ICA), This section focuses on the introduction PCA Principal component analysis .
Principal component analysis algorithm (PCA) It is the most commonly used linear dimensionality reduction method , Its goal is through some kind of linear projection , Mapping high-dimensional data to low-dimensional space , It is expected that the amount of information of the data is the largest in the projected dimension ( The largest variance ), In order to use fewer data dimensions , At the same time, it retains the characteristics of more original data points .PCA Principal component analysis is an unsupervised learning algorithm, which does not involve the fitting of classification labels .

PCA Principal component analysis Python Code implementation

from sklearn.decomposition import PCA
# Set the main component quantity as 2 So that we can visualize
pca = PCA(n_components=2)
pca.fit(X_scaled)
X_pca = pca.transform(X_scaled)
print(X_pca.shape)
X0 = X_pca[wine.target==0]
plt.scatter(X2[:,0],X2[:,1],c='r',s=60,edgecolor='k')
plt.legend(wine.target_names, loc='best')
plt.xlabel('component 1')
plt.ylabel('component 2')
plt.show()

The output result is shown in the figure

Original features and PCA The relationship between principal components

plt.matshow(pca.components_, cmap='plasma')
plt.yticks([0,1],['component 1','component 2'])
plt.colorbar()
plt.xticks(range(len(wine.feature_names)),wine.feature_names,
rotation=60,ha='left')
plt.show()

  Result analysis : In the two principal components , It involves all 13 Features , If the number corresponding to a feature is positive , It shows that there is a positive correlation between it and the principal components , If it is negative, it is the opposite .

Friends who want complete code , can toutiao Search on “ Programming workshop ” After attention s Believe in me , reply “ Algorithm notes 14“ Free access


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved