Skip to content

Commit 67acf4d

Browse files
committed
:art PCA主成分分析
1 parent fae67e6 commit 67acf4d

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

PCA/PCA.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#-*- coding: utf-8 -*-
2+
# Author: Bob
3+
# Date: 2016.11.24
4+
import numpy as np
5+
from matplotlib import pyplot as plt
6+
from scipy import io as spio
7+
8+
'''
9+
主成分分析运行方法
10+
'''
11+
def PCA():
12+
data_2d = spio.loadmat("data.mat")
13+
X = data_2d['X']
14+
plot_data_2d(X) # 显示二维的数据
15+
X_copy = X.copy()
16+
X_norm,mu,sigma = featureNormalize(X_copy) # 归一化数据
17+
plot_data_2d(X_norm) # 显示归一化后的数据
18+
19+
20+
21+
22+
23+
# 可视化二维数据
24+
def plot_data_2d(X):
25+
plt.plot(X[:,0],X[:,1],'bo')
26+
plt.show()
27+
28+
# 归一化数据
29+
def featureNormalize(X):
30+
'''(每一个数据-当前列的均值)/当前列的标准差'''
31+
n = X.shape[1]
32+
mu = np.zeros((1,n));
33+
sigma = np.zeros((1,n))
34+
35+
mu = np.mean(X,axis=0)
36+
sigma = np.std(X,axis=0)
37+
for i in range(n):
38+
X[:,i] = (X[:,i]-mu[i])/sigma[i]
39+
return X,mu,sigma
40+
41+
42+
43+
if __name__ == "__main__":
44+
PCA()

PCA/data.mat

995 Bytes
Binary file not shown.

PCA/data_faces.mat

10.5 MB
Binary file not shown.

formula/K-Means.wmf

-44 Bytes
Binary file not shown.

readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,6 @@ def predict(Theta1,Theta2,X):
750750
- 最后`10`步之后的聚类中心
751751
![enter description here][36]
752752

753-
-
754-
755753
- 计算每条数据到哪个中心最近实现代码:
756754
```
757755
# 找到每条数据距离哪个类中心最近

0 commit comments

Comments
 (0)