機器學習之路:python 特征降維 主成分分析 PCA


 

主成分分析:
降低特征維度的方法。
不會拋棄某一列特征,
而是利用線性代數的計算,將某一維度特征投影到其他維度上去,
盡量小的損失被投影的維度特征
api使用:
estimator = PCA(n_components=20)
pca_x_train = estimator.fit_transform(x_train)
pca_x_test = estimator.transform(x_test)

分別使用支持向量機進行學習降維前后的數據再預測

該數據集源自網上 https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/
我把他下載到了本地
訓練樣本3823條, 測試樣本1797條
圖像通過8*8像素矩陣表示共64個維度,1個目標維度表示數字類別

python3 學習api使用

主成分分析方法實現降低維度

使用了網絡上的數據集,我已經下載到了本地,可以去我的git上參考

git:https://github.com/linyi0604/MachineLearning

代碼:

 1 from sklearn.svm import LinearSVC
 2 from sklearn.metrics import classification_report
 3 from sklearn.decomposition import  PCA
 4 import pandas as pd
 5 import numpy as np
 6 
 7 # 博文: http://www.cnblogs.com/Lin-Yi/p/8973077.html
 8 
 9 '''
10 主成分分析:
11     降低特征維度的方法。
12     不會拋棄某一列特征,
13     而是利用線性代數的計算,將某一維度特征投影到其他維度上去,
14     盡量小的損失被投影的維度特征
15     
16     
17 api使用:
18     estimator = PCA(n_components=20)
19     pca_x_train = estimator.fit_transform(x_train)
20     pca_x_test = estimator.transform(x_test)
21 
22 分別使用支持向量機進行學習降維前后的數據再預測
23 
24 該數據集源自網上 https://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/
25 我把他下載到了本地
26 訓練樣本3823條, 測試樣本1797條
27 圖像通過8*8像素矩陣表示共64個維度,1個目標維度表示數字類別
28 
29 '''
30 
31 # 1 准備數據
32 digits_train = pd.read_csv("../data/optdigits/optdigits.tra", header=None)
33 digits_test = pd.read_csv("../data/optdigits/optdigits.tes", header=None)
34 # 從樣本中抽取出64維度像素特征和1維度目標
35 x_train = digits_train[np.arange(64)]
36 y_train = digits_train[64]
37 x_test = digits_test[np.arange(64)]
38 y_test = digits_test[64]
39 
40 # 2 對圖像數據進行降維,64維度降低到20維度
41 estimator = PCA(n_components=20)
42 pca_x_train = estimator.fit_transform(x_train)
43 pca_x_test = estimator.transform(x_test)
44 
45 # 3.1 使用默認配置的支持向量機進行學習和預測未降維的數據
46 svc = LinearSVC()
47 # 學習
48 svc.fit(x_train, y_train)
49 # 預測
50 y_predict = svc.predict(x_test)
51 
52 # 3.2 使用默認配置的支持向量機學習和預測降維后的數據
53 pca_svc = LinearSVC()
54 # 學習
55 pca_svc.fit(pca_x_train, y_train)
56 pca_y_predict = pca_svc.predict(pca_x_test)
57 
58 # 4 模型評估
59 print("原始數據的准確率:", svc.score(x_test, y_test))
60 print("其他評分:\n", classification_report(y_test, y_predict, target_names=np.arange(10).astype(str)))
61 
62 print("降維后的數據准確率:", pca_svc.score(pca_x_test, y_test))
63 print("其他評分:\n", classification_report(y_test, pca_y_predict, target_names=np.arange(10).astype(str)))
64 
65 '''
66 原始數據的准確率: 0.9165275459098498
67 其他評分:
68               precision    recall  f1-score   support
69 
70           0       0.98      0.98      0.98       178
71           1       0.73      0.99      0.84       182
72           2       0.98      0.97      0.98       177
73           3       0.96      0.88      0.92       183
74           4       0.94      0.95      0.95       181
75           5       0.91      0.96      0.93       182
76           6       0.99      0.96      0.98       181
77           7       0.98      0.92      0.95       179
78           8       0.84      0.79      0.81       174
79           9       0.94      0.76      0.84       180
80 
81 avg / total       0.92      0.92      0.92      1797
82 
83 降維后的數據准確率: 0.9220923761825265
84 其他評分:
85               precision    recall  f1-score   support
86 
87           0       0.97      0.97      0.97       178
88           1       0.93      0.86      0.89       182
89           2       0.96      0.97      0.96       177
90           3       0.93      0.87      0.90       183
91           4       0.94      0.97      0.96       181
92           5       0.86      0.96      0.91       182
93           6       0.97      0.98      0.98       181
94           7       0.97      0.88      0.92       179
95           8       0.89      0.89      0.89       174
96           9       0.82      0.88      0.85       180
97 
98 avg / total       0.92      0.92      0.92      1797
99 '''

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM