1 import matplotlib.pyplot as plt 2 from sklearn.decomposition import PCA 3 from sklearn.datasets import load_iris 4 5 data = load_iris() 6 y = data.target 7 X = data.data 8 pca = PCA(n_components=2) 9 reduced_X = pca.fit_transform(X) 10 11 red_x, red_y = [], [] 12 blue_x, blue_y = [], [] 13 green_x, green_y = [], [] 14 15 for i in range(len(reduced_X)): 16 if y[i] == 0: 17 red_x.append(reduced_X[i][0]) 18 red_y.append(reduced_X[i][1]) 19 elif y[i] == 1: 20 blue_x.append(reduced_X[i][0]) 21 blue_y.append(reduced_X[i][1]) 22 else: 23 green_x.append(reduced_X[i][0]) 24 green_y.append(reduced_X[i][1]) 25 26 plt.scatter(red_x, red_y, c=‘r‘, marker=‘x‘) 27 plt.scatter(blue_x, blue_y, c=‘b‘, marker=‘D‘) 28 plt.scatter(green_x, green_y, c=‘g‘, marker=‘.‘) 29 plt.show()
本公式基于欧式距离
第二种:基于KL散度的优化目标,损失函数如下:
1 from numpy.random import RandomState 2 import matplotlib.pyplot as plt 3 from sklearn.datasets import fetch_olivetti_faces 4 from sklearn import decomposition 5 6 7 n_row, n_col = 2, 3 8 n_components = n_row * n_col 9 image_shape = (64, 64) 10 11 12 ############################################################################### 13 # Load faces data 14 dataset = fetch_olivetti_faces(shuffle=True, random_state=RandomState(0)) 15 faces = dataset.data 16 17 ############################################################################### 18 def plot_gallery(title, images, n_col=n_col, n_row=n_row): 19 plt.figure(figsize=(2. * n_col, 2.26 * n_row)) 20 plt.suptitle(title, size=16) 21 22 for i, comp in enumerate(images): 23 plt.subplot(n_row, n_col, i + 1) 24 vmax = max(comp.max(), -comp.min()) 25 26 plt.imshow(comp.reshape(image_shape), cmap=plt.cm.gray, 27 interpolation=‘nearest‘, vmin=-vmax, vmax=vmax) 28 plt.xticks(()) 29 plt.yticks(()) 30 plt.subplots_adjust(0.01, 0.05, 0.99, 0.94, 0.04, 0.) 31 32 33 plot_gallery("First centered Olivetti faces", faces[:n_components]) 34 ############################################################################### 35 36 estimators = [ 37 (‘Eigenfaces - PCA using randomized SVD‘, 38 decomposition.PCA(n_components=6,whiten=True)), 39 40 (‘Non-negative components - NMF‘, 41 decomposition.NMF(n_components=6, init=‘nndsvda‘, tol=5e-3)) 42 ] 43 44 ############################################################################### 45 46 for name, estimator in estimators: 47 print("Extracting the top %d %s..." % (n_components, name)) 48 print(faces.shape) 49 estimator.fit(faces) 50 components_ = estimator.components_ 51 plot_gallery(name, components_[:n_components]) 52 53 plt.show()
原文:https://www.cnblogs.com/nishida-rin/p/12270848.html