主成分分析:
因子载荷矩阵和特殊方差矩阵的估计:
主成分法 ,主因子法 ,极大似然法
1 #协方差矩阵 2 import numpy as np 3 X = [[2, 0, -1.4], 4 [2.2, 0.2, -1.5], 5 [2.4, 0.1, -1], 6 [1.9, 0, -1.2]] 7 print(np.cov(np.array(X).T)) 8 9 #特征值与特征向量 10 w, v = np.linalg.eig(np.array([[1, -2], [2, -3]])) 11 print('特征值:{} 特征向量:{}'.format(w,v)) 12 13 14 #鸢尾花数据集的降维 15 import matplotlib.pyplot as plt 16 from sklearn.decomposition import PCA 17 from sklearn.datasets import load_iris 18 19 data = load_iris() 20 y = data.target 21 X = data.data 22 pca = PCA(n_components=2) 23 reduced_X = pca.fit_transform(X) 24 25 red_x, red_y = [], [] 26 blue_x, blue_y = [], [] 27 green_x, green_y = [], [] 28 for i in range(len(reduced_X)): 29 if y[i] == 0: 30 red_x.append(reduced_X[i][0]) 31 red_y.append(reduced_X[i][1]) 32 elif y[i] == 1: 33 blue_x.append(reduced_X[i][0]) 34 blue_y.append(reduced_X[i][1]) 35 else: 36 green_x.append(reduced_X[i][0]) 37 green_y.append(reduced_X[i][1]) 38 plt.scatter(red_x, red_y, c='r', marker='x') 39 plt.scatter(blue_x, blue_y, c='b', marker='D') 40 plt.scatter(green_x, green_y, c='g', marker='.') 41 plt.show()
参考:炼数成金