• 大小: 2.63KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-03-01
  • 语言: Python
  • 标签: python  py  主成分  

资源简介

通过主成分分析对手写数字进行特征提取和降维

资源截图

代码片段和文件信息

# -*- 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

评论

共有 条评论