We can plot the first hundred digits from this data set.
importmatplotlib.pyplotaspltfig,axs=plt.subplots(nrows=10,ncols=10,figsize=(6,6))foridx,axinenumerate(axs.ravel()):ax.imshow(X[idx].reshape((8,8)),cmap=plt.cm.binary)ax.axis("off")_=fig.suptitle("A selection from the 64-dimensional digits dataset",fontsize=16)
Helper function to plot embedding
Below, we will use different techniques to embed the digits dataset. We will plot
the projection of the original data onto each embedding. It will allow us to
check whether or digits are grouped together in the embedding space, or
scattered across it.
importnumpyasnpfrommatplotlibimportoffsetboxfromsklearn.preprocessingimportMinMaxScalerdefplot_embedding(X,title):_,ax=plt.subplots()X=MinMaxScaler().fit_transform(X)fordigitindigits.target_names:ax.scatter(*X[y==digit].T,marker=f"${digit}$",s=60,color=plt.cm.Dark2(digit),alpha=0.425,zorder=2,)shown_images=np.array([[1.0,1.0]])# just something bigforiinrange(X.shape[0]):# plot every digit on the embedding# show an annotation box for a group of digitsdist=np.sum((X[i]-shown_images)**2,1)ifnp.min(dist)<4e-3:# don't show points that are too closecontinueshown_images=np.concatenate([shown_images,[X[i]]],axis=0)imagebox=offsetbox.AnnotationBbox(offsetbox.OffsetImage(digits.images[i],cmap=plt.cm.gray_r),X[i])imagebox.set(zorder=1)ax.add_artist(imagebox)ax.set_title(title)ax.axis("off")
Embedding techniques comparison
Below, we compare different techniques. However, there are a couple of things
to note:
the RandomTreesEmbedding is not
technically a manifold embedding method, as it learn a high-dimensional
representation on which we apply a dimensionality reduction method.
However, it is often useful to cast a dataset into a representation in
which the classes are linearly-separable.
the TSNE is initialized with the embedding that is
generated by PCA in this example. It ensures global stability of the embedding,
i.e., the embedding does not depend on random initialization.
Once we declared all the methods of interest, we can run and perform the projection
of the original data. We will store the projected data as well as the computational
time needed to perform each projection.