This example presents how to chain KNeighborsTransformer and TSNE in a pipeline.
It also shows how to wrap the packages nmslib and pynndescent to replace
KNeighborsTransformer and perform approximate nearest neighbors. These packages
can be installed with pipinstallnmslibpynndescent.
Note: In KNeighborsTransformer we use the definition which includes each
training point as its own neighbor in the count of n_neighbors, and for
compatibility reasons, one extra neighbor is computed when mode=='distance'.
Please note that we do the same in the proposed nmslib wrapper.
# Authors: The scikit-learn developers# SPDX-License-Identifier: BSD-3-Clause
First we try to import the packages and warn the user in case they are
missing.
importsystry:importnmslibexceptImportError:print("The package 'nmslib' is required to run this example.")sys.exit()try:frompynndescentimportPyNNDescentTransformerexceptImportError:print("The package 'pynndescent' is required to run this example.")sys.exit()
We define a wrapper class for implementing the scikit-learn API to the
nmslib, as well as a loading function.
importjoblibimportnumpyasnpfromscipy.sparseimportcsr_arrayfromsklearn.baseimportBaseEstimator,TransformerMixinfromsklearn.datasetsimportfetch_openmlfromsklearn.utilsimportshuffleclassNMSlibTransformer(TransformerMixin,BaseEstimator):"""Wrapper for using nmslib as sklearn's KNeighborsTransformer"""def__init__(self,n_neighbors=5,metric="euclidean",method="sw-graph",n_jobs=-1):self.n_neighbors=n_neighborsself.method=methodself.metric=metricself.n_jobs=n_jobsdeffit(self,X):self.n_samples_fit_=X.shape[0]# see more metric in the manual# https://github.com/nmslib/nmslib/tree/master/manualspace={"euclidean":"l2","cosine":"cosinesimil","l1":"l1","l2":"l2",}[self.metric]self.nmslib_=nmslib.init(method=self.method,space=space)self.nmslib_.addDataPointBatch(X.copy())self.nmslib_.createIndex()returnselfdeftransform(self,X):n_samples_transform=X.shape[0]# For compatibility reasons, as each sample is considered as its own# neighbor, one extra neighbor will be computed.n_neighbors=self.n_neighbors+1ifself.n_jobs<0:# Same handling as done in joblib for negative values of n_jobs:# in particular, `n_jobs == -1` means "as many threads as CPUs".num_threads=joblib.cpu_count()+self.n_jobs+1else:num_threads=self.n_jobsresults=self.nmslib_.knnQueryBatch(X.copy(),k=n_neighbors,num_threads=num_threads)indices,distances=zip(*results)indices,distances=np.vstack(indices),np.vstack(distances)indptr=np.arange(0,n_samples_transform*n_neighbors+1,n_neighbors)kneighbors_graph=csr_array((distances.ravel(),indices.ravel(),indptr),shape=(n_samples_transform,self.n_samples_fit_),)returnkneighbors_graphdefload_mnist(n_samples):"""Load MNIST, shuffle the data, and return only n_samples."""mnist=fetch_openml("mnist_784",as_frame=False)X,y=shuffle(mnist.data,mnist.target,random_state=2)returnX[:n_samples]/255,y[:n_samples]
We benchmark the different exact/approximate nearest neighbors transformers.
importtimefromsklearn.manifoldimportTSNEfromsklearn.neighborsimportKNeighborsTransformerfromsklearn.pipelineimportmake_pipelinedatasets=[("MNIST_10000",load_mnist(n_samples=10_000)),("MNIST_20000",load_mnist(n_samples=20_000)),]max_iter=500perplexity=30metric="euclidean"# TSNE requires a certain number of neighbors which depends on the# perplexity parameter.# Add one since we include each sample as its own neighbor.n_neighbors=int(3.0*perplexity+1)+1tsne_params=dict(init="random",# pca cannot be used with precomputed distancesperplexity=perplexity,method="barnes_hut",random_state=42,max_iter=max_iter,learning_rate="auto",)transformers=[("KNeighborsTransformer",KNeighborsTransformer(n_neighbors=n_neighbors,mode="distance",metric=metric),),("NMSlibTransformer",NMSlibTransformer(n_neighbors=n_neighbors,metric=metric),),("PyNNDescentTransformer",PyNNDescentTransformer(n_neighbors=n_neighbors,metric=metric,parallel_batch_queries=True),),]fordataset_name,(X,y)indatasets:msg=f"Benchmarking on {dataset_name}:"print(f"\n{msg}\n"+str("-"*len(msg)))fortransformer_name,transformerintransformers:longest=np.max([len(name)forname,modelintransformers])start=time.time()transformer.fit(X)fit_duration=time.time()-startprint(f"{transformer_name:<{longest}}{fit_duration:.3f} sec (fit)")start=time.time()Xt=transformer.transform(X)transform_duration=time.time()-startprint(f"{transformer_name:<{longest}}{transform_duration:.3f} sec (transform)")iftransformer_name=="PyNNDescentTransformer":start=time.time()Xt=transformer.transform(X)transform_duration=time.time()-startprint(f"{transformer_name:<{longest}}{transform_duration:.3f} sec"" (transform)")
Notice that the PyNNDescentTransformer takes more time during the first
fit and the first transform due to the overhead of the numba just in time
compiler. But after the first call, the compiled Python code is kept in a
cache by numba and subsequent calls do not suffer from this initial overhead.
Both KNeighborsTransformer and NMSlibTransformer
are only run once here as they would show more stable fit and transform
times (they don’t have the cold start problem of PyNNDescentTransformer).
importmatplotlib.pyplotaspltfrommatplotlib.tickerimportNullFormattertransformers=[("TSNE with internal NearestNeighbors",TSNE(metric=metric,**tsne_params)),("TSNE with KNeighborsTransformer",make_pipeline(KNeighborsTransformer(n_neighbors=n_neighbors,mode="distance",metric=metric),TSNE(metric="precomputed",**tsne_params),),),("TSNE with NMSlibTransformer",make_pipeline(NMSlibTransformer(n_neighbors=n_neighbors,metric=metric),TSNE(metric="precomputed",**tsne_params),),),]# init the plotnrows=len(datasets)ncols=np.sum([1forname,modelintransformersif"TSNE"inname])fig,axes=plt.subplots(nrows=nrows,ncols=ncols,squeeze=False,figsize=(5*ncols,4*nrows))axes=axes.ravel()i_ax=0fordataset_name,(X,y)indatasets:msg=f"Benchmarking on {dataset_name}:"print(f"\n{msg}\n"+str("-"*len(msg)))fortransformer_name,transformerintransformers:longest=np.max([len(name)forname,modelintransformers])start=time.time()Xt=transformer.fit_transform(X)transform_duration=time.time()-startprint(f"{transformer_name:<{longest}}{transform_duration:.3f} sec"" (fit_transform)")# plot TSNE embedding which should be very similar across methodsaxes[i_ax].set_title(transformer_name+"\non "+dataset_name)axes[i_ax].scatter(Xt[:,0],Xt[:,1],c=y.astype(np.int32),alpha=0.2,cmap=plt.cm.viridis,)axes[i_ax].xaxis.set_major_formatter(NullFormatter())axes[i_ax].yaxis.set_major_formatter(NullFormatter())axes[i_ax].axis("tight")i_ax+=1fig.tight_layout()plt.show()
We can observe that the default TSNE estimator with
its internal NearestNeighbors implementation is
roughly equivalent to the pipeline with TSNE and
KNeighborsTransformer in terms of performance.
This is expected because both pipelines rely internally on the same
NearestNeighbors implementation that performs
exacts neighbors search. The approximate NMSlibTransformer is already
slightly faster than the exact search on the smallest dataset but this speed
difference is expected to become more significant on datasets with a larger
number of samples.
Notice however that not all approximate search methods are guaranteed to
improve the speed of the default exact search method: indeed the exact search
implementation significantly improved since scikit-learn 1.1. Furthermore, the
brute-force exact search method does not require building an index at fit
time. So, to get an overall performance improvement in the context of the
TSNE pipeline, the gains of the approximate search
at transform need to be larger than the extra time spent to build the
approximate search index at fit time.
Finally, the TSNE algorithm itself is also computationally intensive,
irrespective of the nearest neighbors search. So speeding-up the nearest
neighbors search step by a factor of 5 would not result in a speed up by a
factor of 5 for the overall pipeline.