|
| 1 | +#coding:utf-8 |
| 2 | +""" |
| 3 | +CreatedCreated on Fri May 22 2015 |
| 4 | +@author: wepon |
| 5 | +@blog; |
| 6 | +
|
| 7 | +""" |
| 8 | +from time import time |
| 9 | +import numpy as np |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +from mpl_toolkits.mplot3d.axes3d import Axes3D |
| 12 | +from sklearn import (manifold, datasets, decomposition, ensemble, lda,random_projection) |
| 13 | + |
| 14 | +#%% |
| 15 | +#加载数据,显示数据 |
| 16 | +digits = datasets.load_digits(n_class=5) |
| 17 | +X = digits.data |
| 18 | +y = digits.target |
| 19 | +print X.shape |
| 20 | +n_img_per_row = 20 |
| 21 | +img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row)) |
| 22 | +for i in range(n_img_per_row): |
| 23 | + ix = 10 * i + 1 |
| 24 | + for j in range(n_img_per_row): |
| 25 | + iy = 10 * j + 1 |
| 26 | + img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8)) |
| 27 | +plt.imshow(img, cmap=plt.cm.binary) |
| 28 | +plt.title('A selection from the 64-dimensional digits dataset') |
| 29 | + |
| 30 | +#LLE,Isomap,LTSA需要设置n_neighbors这个参数 |
| 31 | +n_neighbors = 30 |
| 32 | + |
| 33 | + |
| 34 | +#%% |
| 35 | +# 将降维后的数据可视化,2维 |
| 36 | +def plot_embedding_2d(X, title=None): |
| 37 | + #坐标缩放到[0,1]区间 |
| 38 | + x_min, x_max = np.min(X,axis=0), np.max(X,axis=0) |
| 39 | + X = (X - x_min) / (x_max - x_min) |
| 40 | + |
| 41 | + #降维后的坐标为(X[i, 0], X[i, 1]),在该位置画出对应的digits |
| 42 | + fig = plt.figure() |
| 43 | + ax = fig.add_subplot(1, 1, 1) |
| 44 | + for i in range(X.shape[0]): |
| 45 | + ax.text(X[i, 0], X[i, 1],str(digits.target[i]), |
| 46 | + color=plt.cm.Set1(y[i] / 10.), |
| 47 | + fontdict={'weight': 'bold', 'size': 9}) |
| 48 | + |
| 49 | + if title is not None: |
| 50 | + plt.title(title) |
| 51 | + |
| 52 | +#%% |
| 53 | +#将降维后的数据可视化,3维 |
| 54 | +def plot_embedding_3d(X, title=None): |
| 55 | + #坐标缩放到[0,1]区间 |
| 56 | + x_min, x_max = np.min(X,axis=0), np.max(X,axis=0) |
| 57 | + X = (X - x_min) / (x_max - x_min) |
| 58 | + |
| 59 | + #降维后的坐标为(X[i, 0], X[i, 1],X[i,2]),在该位置画出对应的digits |
| 60 | + fig = plt.figure() |
| 61 | + ax = fig.add_subplot(1, 1, 1, projection='3d') |
| 62 | + for i in range(X.shape[0]): |
| 63 | + ax.text(X[i, 0], X[i, 1], X[i,2],str(digits.target[i]), |
| 64 | + color=plt.cm.Set1(y[i] / 10.), |
| 65 | + fontdict={'weight': 'bold', 'size': 9}) |
| 66 | + |
| 67 | + if title is not None: |
| 68 | + plt.title(title) |
| 69 | + |
| 70 | + |
| 71 | +#%% |
| 72 | +#随机映射 |
| 73 | +print("Computing random projection") |
| 74 | +rp = random_projection.SparseRandomProjection(n_components=2, random_state=42) |
| 75 | +X_projected = rp.fit_transform(X) |
| 76 | +plot_embedding_2d(X_projected, "Random Projection") |
| 77 | + |
| 78 | +#%% |
| 79 | +#PCA |
| 80 | +print("Computing PCA projection") |
| 81 | +t0 = time() |
| 82 | +X_pca = decomposition.TruncatedSVD(n_components=3).fit_transform(X) |
| 83 | +plot_embedding_2d(X_pca[:,0:2],"PCA 2D") |
| 84 | +plot_embedding_3d(X_pca,"PCA 3D (time %.2fs)" %(time() - t0)) |
| 85 | + |
| 86 | +#%% |
| 87 | +#LDA |
| 88 | +print("Computing LDA projection") |
| 89 | +X2 = X.copy() |
| 90 | +X2.flat[::X.shape[1] + 1] += 0.01 # Make X invertible |
| 91 | +t0 = time() |
| 92 | +X_lda = lda.LDA(n_components=3).fit_transform(X2, y) |
| 93 | +plot_embedding_2d(X_lda[:,0:2],"LDA 2D" ) |
| 94 | +plot_embedding_3d(X_lda,"LDA 3D (time %.2fs)" %(time() - t0)) |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +#%% |
| 99 | +#Isomap |
| 100 | +print("Computing Isomap embedding") |
| 101 | +t0 = time() |
| 102 | +X_iso = manifold.Isomap(n_neighbors, n_components=2).fit_transform(X) |
| 103 | +print("Done.") |
| 104 | +plot_embedding_2d(X_iso,"Isomap (time %.2fs)" %(time() - t0)) |
| 105 | + |
| 106 | + |
| 107 | +#%% |
| 108 | +#standard LLE |
| 109 | +print("Computing LLE embedding") |
| 110 | +clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2,method='standard') |
| 111 | +t0 = time() |
| 112 | +X_lle = clf.fit_transform(X) |
| 113 | +print("Done. Reconstruction error: %g" % clf.reconstruction_error_) |
| 114 | +plot_embedding_2d(X_lle,"Locally Linear Embedding (time %.2fs)" %(time() - t0)) |
| 115 | + |
| 116 | + |
| 117 | +#%% |
| 118 | +#modified LLE |
| 119 | +print("Computing modified LLE embedding") |
| 120 | +clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2,method='modified') |
| 121 | +t0 = time() |
| 122 | +X_mlle = clf.fit_transform(X) |
| 123 | +print("Done. Reconstruction error: %g" % clf.reconstruction_error_) |
| 124 | +plot_embedding_2d(X_mlle,"Modified Locally Linear Embedding (time %.2fs)" %(time() - t0)) |
| 125 | + |
| 126 | + |
| 127 | +#%% |
| 128 | +# HLLE |
| 129 | +print("Computing Hessian LLE embedding") |
| 130 | +clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2,method='hessian') |
| 131 | +t0 = time() |
| 132 | +X_hlle = clf.fit_transform(X) |
| 133 | +print("Done. Reconstruction error: %g" % clf.reconstruction_error_) |
| 134 | +plot_embedding_2d(X_hlle,"Hessian Locally Linear Embedding (time %.2fs)" %(time() - t0)) |
| 135 | + |
| 136 | + |
| 137 | +#%% |
| 138 | +# LTSA |
| 139 | +print("Computing LTSA embedding") |
| 140 | +clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2,method='ltsa') |
| 141 | +t0 = time() |
| 142 | +X_ltsa = clf.fit_transform(X) |
| 143 | +print("Done. Reconstruction error: %g" % clf.reconstruction_error_) |
| 144 | +plot_embedding_2d(X_ltsa,"Local Tangent Space Alignment (time %.2fs)" %(time() - t0)) |
| 145 | + |
| 146 | +#%% |
| 147 | +# MDS |
| 148 | +print("Computing MDS embedding") |
| 149 | +clf = manifold.MDS(n_components=2, n_init=1, max_iter=100) |
| 150 | +t0 = time() |
| 151 | +X_mds = clf.fit_transform(X) |
| 152 | +print("Done. Stress: %f" % clf.stress_) |
| 153 | +plot_embedding_2d(X_mds,"MDS (time %.2fs)" %(time() - t0)) |
| 154 | + |
| 155 | +#%% |
| 156 | +# Random Trees |
| 157 | +print("Computing Totally Random Trees embedding") |
| 158 | +hasher = ensemble.RandomTreesEmbedding(n_estimators=200, random_state=0,max_depth=5) |
| 159 | +t0 = time() |
| 160 | +X_transformed = hasher.fit_transform(X) |
| 161 | +pca = decomposition.TruncatedSVD(n_components=2) |
| 162 | +X_reduced = pca.fit_transform(X_transformed) |
| 163 | + |
| 164 | +plot_embedding_2d(X_reduced,"Random Trees (time %.2fs)" %(time() - t0)) |
| 165 | + |
| 166 | +#%% |
| 167 | +# Spectral |
| 168 | +print("Computing Spectral embedding") |
| 169 | +embedder = manifold.SpectralEmbedding(n_components=2, random_state=0,eigen_solver="arpack") |
| 170 | +t0 = time() |
| 171 | +X_se = embedder.fit_transform(X) |
| 172 | +plot_embedding_2d(X_se,"Spectral (time %.2fs)" %(time() - t0)) |
| 173 | + |
| 174 | +#%% |
| 175 | +# t-SNE |
| 176 | +print("Computing t-SNE embedding") |
| 177 | +tsne = manifold.TSNE(n_components=3, init='pca', random_state=0) |
| 178 | +t0 = time() |
| 179 | +X_tsne = tsne.fit_transform(X) |
| 180 | +print X_tsne.shape |
| 181 | +plot_embedding_2d(X_tsne[:,0:2],"t-SNE 2D") |
| 182 | +plot_embedding_3d(X_tsne,"t-SNE 3D (time %.2fs)" %(time() - t0)) |
| 183 | + |
| 184 | +plt.show() |
0 commit comments