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