用Python一步一步实现PCA

本文通过Python详细讲解PCA(主成分分析)的实现步骤,包括数据生成、计算均值向量、离散度矩阵、特征值和特征向量,并展示了如何将三维数据降至二维。还探讨了不同PCA实现方式的差异,如matplotlib.mlab.PCA和sklearn.decomposition PCA。
摘要由CSDN通过智能技术生成

Requirements:
Python环境部署:http://blog.csdn.net/luzhangting/article/details/61414485
PCA原理:http://blog.csdn.net/zhongkelee/article/details/44064401


第一步:生成三维的样本数据
生成40个三维数据,分两类,每一类20个
第一类:均值[0,0,0],方差[1,0,0],[0,1,0],[0,0,1];
第一类:均值[1,1,1],方差[1,0,0],[0,1,0],[0,0,1]。

import numpy as np

np.random.seed(1)

mu_vec1 = np.array([0,0,0])
cov_mat1 = np.array([[1,0,0],[0,1,0],[0,0,1]])
class1_sample = np.random.multivariate_normal(mu_vec1, cov_mat1, 20).T
assert class1_sample.shape == (3,20), "The matrix has not the dimensions 3x20"

mu_vec2 = np.array([1,1,1])
cov_mat2 = np.array([[1,0,0],[0,1,0],[0,0,1]])
class2_sample = np.random.multivariate_normal(mu_vec2, cov_mat2, 20).T
assert class2_sample.shape == (3,20), "The matrix has not the dimensions 3x20"

画图查看生成好的数据

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')
plt.rcParams['legend.fontsize'] = 10   
ax.plot(class1_sample[0,:], class1_sample[1,:], class1_sample[2,:], 'o', markersize=8, color='blue', alpha=0.5, label='class1')
ax.plot(class2_sample[0,:], class2_sample[1,:], class2_sample[2,:], '^', markersize=8, alpha=0.5, color='red', label='class2')

plt.title('Samples for class 1 and class 2')
ax.legend(loc='upper right')

plt.show()

这里写图片描述

整理数据,因为PCA方法不对数据进行分类处理,这也是PCA的一个缺点

all_samples = np.concatenate((class1_sample, class2_sample), axis=1)
as
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值