机器学习的经典算法——PCA
'''
PCA主成分分析——降维(最小的损失,最大的信息量)
1、中心化:让平均值做零点
2、计算C:为求特征值和特征向量准备
3、求解特征值和特征向量:利用拉格朗日求条件最值,在代码中直接调用函数
4、计算降维:最大的k个特征值对应的特征向量组成的降维矩阵,矩阵相乘得结果
'''
import numpy as np
import matplotlib.pyplot as plt
def pca(data,dim):
N,D= np.shape(data)
data = data-np.mean(data,axis=0,keepdims=True)
c = np.dot(data.T, data)/(N-1)
values, vector=np.linalg.eig(c)
index =np.argsort(-values)[:dim]
picked_vector = vector[:, index]
data_dim=np.dot(data, picked_vector)
return data_dim,picked_vector
def draw_pic(datas, labs):
plt.cla()
unque_labs = np.unique(labs)
colors = [plt.cm.Spectral(each)
for each in np.linspace(0, 1, len(unque_labs))]
p = []
legends = []
for i in range(len(unque_labs)):
index = np.where(labs == unque_labs[i])
pi = plt.scatter(datas[index, 0], datas[index, 1], c=[colors[i]])
p.append(pi)
legends.append(unque_labs[i])
plt.legend(p, legends)
plt.show()
if __name__ == "__main__":
data = np.loadtxt("iris.data",dtype="str",delimiter=',')
feas = data[:,:-1]
feas = np.float32(feas)
labs = data[:,-1]
data_2d, picked_vector=pca(feas,2)
draw_pic(data_2d,labs)