1
+ #-*- coding: utf-8 -*-
2
+ # Author:bob
3
+ # Date:2016.12.22
4
+ import numpy as np
5
+ from matplotlib import pyplot as plt
6
+ from scipy import io as spio
7
+ from sklearn .decomposition import pca
8
+ from sklearn .preprocessing import StandardScaler
9
+
10
+ '''二维降为一维主运行函数'''
11
+ def PCA_2d_example ():
12
+ '''加载数据并作图'''
13
+ data = spio .loadmat ('data.mat' )
14
+ X = data ['X' ]
15
+ plt = plot_data_2d (X ,'bo' )
16
+ plt .axis ('square' )
17
+ plt .title ('original data' )
18
+ plt .show ()
19
+ '''归一化数据并作图'''
20
+ scaler = StandardScaler ()
21
+ scaler .fit (X )
22
+ x_train = scaler .transform (X )
23
+
24
+ plot_data_2d (x_train , 'bo' )
25
+ plt .axis ('square' )
26
+ plt .title ('scaler data' )
27
+ plt .show ()
28
+
29
+ '''拟合数据'''
30
+ K = 1 # 要降的维度
31
+ model = pca .PCA (n_components = K ).fit (x_train ) # 拟合数据,n_components定义要降的维度
32
+ Z = model .transform (x_train ) # transform就会执行降维操作
33
+
34
+ '''数据恢复并作图'''
35
+ Ureduce = model .components_ # 得到降维用的Ureduce
36
+ x_rec = np .dot (Z ,Ureduce ) # 数据恢复
37
+
38
+ plot_data_2d (x_rec ,'bo' )
39
+ plt .plot ()
40
+ plt .axis ('square' )
41
+ plt .title ('recover data' )
42
+ plt .show ()
43
+
44
+ def PCA_face_example ():
45
+ '''加载数据并显示'''
46
+ image_data = spio .loadmat ('data_faces.mat' )
47
+ X = image_data ['X' ]
48
+ display_imageData (X [0 :100 ,:]) # 显示100个最初图像
49
+
50
+ '''归一化数据'''
51
+ scaler = StandardScaler ()
52
+ scaler .fit (X )
53
+ x_train = scaler .transform (X )
54
+
55
+ '''拟合模型'''
56
+ K = 100
57
+ model = pca .PCA (n_components = K ).fit (x_train )
58
+ Z = model .transform (x_train )
59
+ Ureduce = model .components_
60
+
61
+ display_imageData (Ureduce [0 :36 ,:]) # 可视化部分U数据
62
+ x_rec = np .dot (Z ,Ureduce )
63
+
64
+ display_imageData (x_rec [0 :100 ,:]) # 显示恢复的数据
65
+
66
+
67
+
68
+
69
+ # 可视化二维数据
70
+ def plot_data_2d (X ,marker ):
71
+ plt .plot (X [:,0 ],X [:,1 ],marker )
72
+ return plt
73
+
74
+ # 显示图片
75
+ def display_imageData (imgData ):
76
+ sum = 0
77
+ '''
78
+ 显示100个数(若是一个一个绘制将会非常慢,可以将要画的图片整理好,放到一个矩阵中,显示这个矩阵即可)
79
+ - 初始化一个二维数组
80
+ - 将每行的数据调整成图像的矩阵,放进二维数组
81
+ - 显示即可
82
+ '''
83
+ m ,n = imgData .shape
84
+ width = np .int32 (np .round (np .sqrt (n )))
85
+ height = np .int32 (n / width );
86
+ rows_count = np .int32 (np .floor (np .sqrt (m )))
87
+ cols_count = np .int32 (np .ceil (m / rows_count ))
88
+ pad = 1
89
+ display_array = - np .ones ((pad + rows_count * (height + pad ),pad + cols_count * (width + pad )))
90
+ for i in range (rows_count ):
91
+ for j in range (cols_count ):
92
+ max_val = np .max (np .abs (imgData [sum ,:]))
93
+ display_array [pad + i * (height + pad ):pad + i * (height + pad )+ height ,pad + j * (width + pad ):pad + j * (width + pad )+ width ] = imgData [sum ,:].reshape (height ,width ,order = "F" )/ max_val # order=F指定以列优先,在matlab中是这样的,python中需要指定,默认以行
94
+ sum += 1
95
+
96
+ plt .imshow (display_array ,cmap = 'gray' ) #显示灰度图像
97
+ plt .axis ('off' )
98
+ plt .show ()
99
+
100
+
101
+ if __name__ == '__main__' :
102
+ #PCA_2d_example()
103
+ PCA_face_example ()
0 commit comments