資源簡介
通過主成分分析對手寫數字進行特征提取和降維
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
#?@Time????:?4/12/2020?12:20
#?@Author??:?Eddie?Shen
#?@Email???:?sheneddie@outlook.com
#?@File????:?HW5_2.py
#?@Software:?PyCharm
import?matplotlib.pyplot?as?plt
import?numpy?as?np
import?numpy.linalg?as?la
path?=?‘D:\\shene\\Documents\\SUFE\\3D\\Data_Mining\\Data\\zip.train‘
data?=?np.loadtxt(path)
#?Choose?number?9.
k?=?9
data_k?=?data[data[:?0]?==?k?1:]
#?Show?the?6th?handwriting?number.
plt.figure()
plt.imshow(data_k[5?:].reshape(16?16)?cmap=“gray_r“)
plt.show()
#?Show?the?mean?of?data_k.
mu?=?np.mean(data_k?axis=0)
plt.figure()
plt.imshow(mu.reshape(16?16)?cmap=“gray_r“)
plt.show()
#?Principal?component?analysis.
cov_x?=?np.cov(data_k?rowvar=False)
u?v?=?la.eig(cov_x)
#?Show?the?first?principal?component.
plt.figure()
plt.imshow(v[:?0].reshape(16?16)?cmap=“gray_r“)
plt.show()
#?Choose?handwriting?numbers?of?the?five?biggest?and?five?smallest?scores.
xi1:?np.ndarray?=?(data_k?-?mu)?@?v[:?0:1]
ind
評論
共有 條評論