""" Motivation and help seeked from http://scikit-learn.org/stable/auto_examples/manifold/plot_swissroll.html The code for the plotting 2D manifold has been modified from http://scikit-learn.org/stable/auto_examples/manifold/plot_lle_digits.html#example-manifold-plot-lle-digits-py Proper documentation in R wasnt available """ from sklearn import manifold import numpy as np from scipy import linalg as LA import pylab import numpy from pylab import imread,subplot,imshow,title,gray,figure,show,NullLocator from numpy import linalg,size from sklearn import decomposition import os, glob import pickle from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from matplotlib.ticker import NullFormatter from matplotlib import offsetbox from PIL import Image from sklearn.metrics.pairwise import euclidean_distances from sklearn.decomposition import KernelPCA def plot_embedding(result, imList,list_length,title): x_min, x_max = np.min(result, 0)-np.min(result, 0)*0.25, np.max(result, 0)+np.max(result, 0)*0.25 plt.figure() ax = plt.subplot(111) if hasattr(offsetbox, 'AnnotationBbox'): large_images = np.array([[1., 1]]) for i in range(len(list_length)): plt.plot(result[i][0], result[i][1], 'bo') large_images = np.r_[large_images, [result[i]]] img=Image.open(imList[list_length[i]]) imagebox = offsetbox.AnnotationBbox(offsetbox.OffsetImage(img, cmap=plt.cm.gray_r,zoom=0.04), result[i],xybox=(-30, 50),xycoords='data',boxcoords="offset points",arrowprops=dict(arrowstyle="->"),pad=0) ax.add_artist(imagebox) plt.xticks([]), plt.yticks([]) plt.axis() if title is not None: plt.title(title) def main(): imlist=[] imlist=np.array([j for j in glob.glob('jpg/*')]) x = np.array([np.array(Image.open(counter)).flatten() for counter in glob.glob('jpg/*')],'f') kpca = KernelPCA(kernel="linear", n_components=2) kpca_reusult = kpca.fit_transform(x) plot_embedding(kpca_reusult,imlist,range(143),'2D Manifold, RBF Kernel with images') plt.savefig('2drbfimg.png', bbox_inches='tight') plt.close main()