drnb notebooks
  • Notebooks
  • GitHub
  1. Articles
  2. PaCMAP: local scaling vs. k-nearest neighbors
  • Experiments in Dimensionality Reduction
  • Articles
    • Do Hubs Hurt?
    • How Does Dimensionality Reduction Transform Distances?
    • Hubness
    • Heavy-tailed UMAP
    • Leopold: Lightweight Estimate of Preservation of Local Density
    • Measuring Media Bias
    • Normalized Compression Distance with 20 Newsgroups
    • PaCMAP: local scaling vs. k-nearest neighbors
    • PaCMAP Repeats
    • Neighbor Overlap and PCA
    • Neighbor Recall: UMAP vs t-SNE
    • Approximating Isomap with k-Nearest Neighbors and Stochastic Metric Multidimensional Scaling
    • Stochastic Metric Multidimensional Scaling
    • Renormalizing after TF-IDF + SVD
    • t-SNE Consistency with Random Initialization
    • UMAP with Annealed Neighbors. 1: Defaults
    • UMAP with Annealed Neighbors. 2: t-UMAP
    • UMAP with Annealed Neighbors. 3: PCA Initialization
    • Spectral Embedding
    • UMATO Evaluation
    • t-SNE Affinity: Gaussian vs. Uniform

On this page

  • Effect of PCA Preprocessing
  1. Articles
  2. PaCMAP: local scaling vs. k-nearest neighbors

PaCMAP: local scaling vs. k-nearest neighbors

  • Show All Code
  • Hide All Code

PaCMAP uses locally-scaled neighbors by default, but it can also use k-nearest neighbors. Let’s see if there are any datasets where we can see a big difference. Note that we expect to see some variability in the results just due to the vagaries of the random number generator. See pacmap-repeats.ipynb for more details on that.

Code
import drnb.embed.pipeline as pl
from drnb.experiment import Experiment, merge_experiments, read_experiment
Code
experiment = Experiment(name="pacmap-local-scaling")
Code
experiment.add_method(
    "pacmap",
    name="pacmap-lsn",
)

experiment.add_method(
    pl.embedder("pacmap", local_scale=False),
    name="pacmap-knn",
)

A potential use for local scaling is to reduce hubness, so apart from the typical datasets I tend to use with PaCMAP, I thought it would be good to try it on some datasets which are known to have hubness issues (see the hubness notebook for more details).

tasic2018 and lamanno2020 are transcriptomic datasets like macosko2015, iclr is a TF-IDF dataset, and we will swap out ng20pacmap for ng20, a 3000D version of the same underlying newsgroups TF-IDF data.

Code
experiment.add_datasets(
    [
        "curve2d",
        "mammoth",
        "scurvehole",
        "isoswiss",
        "hierarchical",
        "spheres",
        "coil20",
        "coil100",
        "macosko2015",
        "tasic2018",
        "lamanno2020",
        "iclr",
        "mnist",
        "fashion",
        "ng20",
    ]
)
Code
experiment.run()
[23:42:37] INFO     Running pacmap-lsn on curve2d                                   experiment.py:65
[23:42:38] INFO     Running pacmap-lsn on mammoth                                   experiment.py:65
[23:42:56] INFO     Running pacmap-lsn on scurvehole                                experiment.py:65
[23:42:59] INFO     Running pacmap-lsn on isoswiss                                  experiment.py:65
[23:43:05] INFO     Running pacmap-lsn on hierarchical                              experiment.py:65
[23:43:07] INFO     Loading faiss with AVX2 support.                                   loader.py:110
           INFO     Successfully loaded faiss with AVX2 support.                       loader.py:112
[23:43:29] INFO     Running pacmap-lsn on spheres                                   experiment.py:65
           WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 101 to 100 dimensions.                                                     
[23:43:35] INFO     Running pacmap-lsn on coil20                                    experiment.py:65
[23:43:36] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 16384 to 100 dimensions.                                                   
[23:43:40] INFO     Running pacmap-lsn on coil100                                   experiment.py:65
[23:43:47] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 49152 to 100 dimensions.                                                   
[23:44:12] INFO     Running pacmap-lsn on macosko2015                               experiment.py:65
[23:44:14] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 3000 to 100 dimensions.                                                    
[23:44:46] INFO     Running pacmap-lsn on tasic2018                                 experiment.py:65
[23:44:47] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 3000 to 100 dimensions.                                                    
[23:45:02] INFO     Running pacmap-lsn on lamanno2020                               experiment.py:65
           WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 1999 to 100 dimensions.                                                    
[23:45:06] INFO     Running pacmap-lsn on iclr                                      experiment.py:65
[23:45:11] INFO     Running pacmap-lsn on mnist                                     experiment.py:65
[23:45:12] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 784 to 100 dimensions.                                                     
[23:45:54] INFO     Running pacmap-lsn on fashion                                   experiment.py:65
[23:45:55] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 784 to 100 dimensions.                                                     
[23:46:37] INFO     Running pacmap-lsn on ng20                                      experiment.py:65
[23:46:38] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 2500 to 100 dimensions.                                                    
[23:46:51] INFO     Running pacmap-knn on curve2d                                   experiment.py:65
[23:46:52] INFO     Running pacmap-knn on mammoth                                   experiment.py:65
[23:47:09] INFO     Running pacmap-knn on scurvehole                                experiment.py:65
[23:47:11] INFO     Running pacmap-knn on isoswiss                                  experiment.py:65
[23:47:17] INFO     Running pacmap-knn on hierarchical                              experiment.py:65
[23:47:39] INFO     Running pacmap-knn on spheres                                   experiment.py:65
           WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 101 to 100 dimensions.                                                     
[23:47:44] INFO     Running pacmap-knn on coil20                                    experiment.py:65
[23:47:45] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 16384 to 100 dimensions.                                                   
[23:47:49] INFO     Running pacmap-knn on coil100                                   experiment.py:65
[23:47:56] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 49152 to 100 dimensions.                                                   
[23:48:20] INFO     Running pacmap-knn on macosko2015                               experiment.py:65
[23:48:23] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 3000 to 100 dimensions.                                                    
[23:48:54] INFO     Running pacmap-knn on tasic2018                                 experiment.py:65
[23:48:56] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 3000 to 100 dimensions.                                                    
[23:49:11] INFO     Running pacmap-knn on lamanno2020                               experiment.py:65
[23:49:12] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 1999 to 100 dimensions.                                                    
[23:49:16] INFO     Running pacmap-knn on iclr                                      experiment.py:65
[23:49:20] INFO     Running pacmap-knn on mnist                                     experiment.py:65
[23:49:22] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 784 to 100 dimensions.                                                     
[23:50:03] INFO     Running pacmap-knn on fashion                                   experiment.py:65
[23:50:04] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 784 to 100 dimensions.                                                     
[23:50:45] INFO     Running pacmap-knn on ng20                                      experiment.py:65
[23:50:46] WARNING  Precomputed knn cannot be used: dimensionality will be reduced     pacmap.py:146
                    from 2500 to 100 dimensions.                                                    
Code
experiment.plot()

I don’t see huge differences in the results here, within the natural variability that we saw in pacmap-repeats.ipynb.

Code
experiment.save(overwrite=True)

Effect of PCA Preprocessing

PaCMAP uses PCA in two places by default: first, it reduces the dimensionality of the input data to 100D, and second it uses PCA to initialize coordinates.

The hubness notebook mentions that local scaling is effective reducing hubness, but hubness tends to have the largest effect on high-dimensional datasets. So maybe by applying the PCA preprocessing on the input data we are missing most of the benefit of local scaling with PaCMAP specifically for high dimensional datasets?

Let’s repeat the analysis and turn off the PCA preprocessing. A warning will be emitted that the nearest neighbor search will be slow, but in the current version of PaCMAP this is emitted even if we are providing a precomputed knn. We will still use PCA to initialize the coordinates.

Code
experiment = Experiment(name="pacmap-local-scaling-nopca")
Code
experiment.add_method(
    "pacmap",
    params=dict(apply_pca=False),
    name="pacmap-lsn-nopca",
)

experiment.add_method(
    pl.embedder("pacmap", local_scale=False),
    params=dict(apply_pca=False),
    name="pacmap-knn-nopca",
)

If the dataset has less than 100 columns then the PCA isn’t applied, so we will remove all datasets that have fewer than 100 dimensions.

I expect results will look bad with the Euclidean metric with such high dimensionality, but I am curious if they will at least look different once local scaling goes to work on them.

Code
experiment.add_datasets(
    [
        "hierarchical",
        "spheres",
        "coil20",
        "coil100",
        "macosko2015",
        "tasic2018",
        "lamanno2020",
        "mnist",
        "fashion",
        "ng20",
    ]
)
Code
experiment.run()
[23:51:52] INFO     Running pacmap-lsn-nopca on hierarchical                        experiment.py:65
[23:51:55] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:52:15] INFO     Running pacmap-lsn-nopca on spheres                             experiment.py:65
           WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:52:18] INFO     Running pacmap-lsn-nopca on coil20                              experiment.py:65
[23:52:19] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:52:21] INFO     Running pacmap-lsn-nopca on coil100                             experiment.py:65
[23:52:29] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:52:55] INFO     Running pacmap-lsn-nopca on macosko2015                         experiment.py:65
[23:52:59] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:53:21] INFO     Running pacmap-lsn-nopca on tasic2018                           experiment.py:65
[23:53:23] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:53:35] INFO     Running pacmap-lsn-nopca on lamanno2020                         experiment.py:65
[23:53:36] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:53:38] INFO     Running pacmap-lsn-nopca on mnist                               experiment.py:65
[23:53:40] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:54:07] INFO     Running pacmap-lsn-nopca on fashion                             experiment.py:65
[23:54:10] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:54:37] INFO     Running pacmap-lsn-nopca on ng20                                experiment.py:65
[23:54:38] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:54:47] INFO     Running pacmap-knn-nopca on hierarchical                        experiment.py:65
[23:54:49] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:55:08] INFO     Running pacmap-knn-nopca on spheres                             experiment.py:65
[23:55:09] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:55:11] INFO     Running pacmap-knn-nopca on coil20                              experiment.py:65
[23:55:12] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:55:15] INFO     Running pacmap-knn-nopca on coil100                             experiment.py:65
[23:55:22] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:55:48] INFO     Running pacmap-knn-nopca on macosko2015                         experiment.py:65
[23:55:51] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:56:14] INFO     Running pacmap-knn-nopca on tasic2018                           experiment.py:65
[23:56:16] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:56:28] INFO     Running pacmap-knn-nopca on lamanno2020                         experiment.py:65
[23:56:29] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:56:31] INFO     Running pacmap-knn-nopca on mnist                               experiment.py:65
[23:56:32] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:56:59] INFO     Running pacmap-knn-nopca on fashion                             experiment.py:65
[23:57:01] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
[23:57:28] INFO     Running pacmap-knn-nopca on ng20                                experiment.py:65
[23:57:29] WARNING  Running ANNOY Indexing on high-dimensional data. Nearest-neighbor  pacmap.py:881
                    search may be slow!                                                             
Code
experiment.plot()

Code
experiment.save(overwrite=True)

As usual PCA has a big effect on macosko2015, but the effect is subtler on the other datasets. Not really obvious what local scaling achieves structurally as a whole, so I will merge these two experiments and plot them together (only datasets present in both experiments are retained).

Code
exp1 = read_experiment("pacmap-local-scaling")
exp2 = read_experiment("pacmap-local-scaling-nopca")
exp = merge_experiments(exp1, exp2, name="pacmap-merged")
exp.plot()

Still not sure I see any systematic effect with local scaling. For example with PCA, going from the knn to lsn on lamanno2020 seems to create more separated clusters, but that isn’t the pattern when not using PCA. In the non-PCA case with macosko2015 maybe local scaling does separate some of the clusters out from the big cyan blob better, but the opposite might be true for ng20.

Jury’s out on this one. The effects on hubness seem undeniable, but that doesn’t seem to translate to an obvious effect on the embeddings.