机器学习之路: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("其他评分:
", 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("其他评分:
", 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 '''
原文地址:https://www.cnblogs.com/Lin-Yi/p/8973077.html