drnb notebooks
  • Notebooks
  • GitHub
  1. Articles
  2. Hubness
  • 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

  • Before we begin: you need to have some data saved
  • Datasets
  • 15 neighbors
  • 50 neighbors
  • 150 neighbors
  • NG20 Euclidean vs Cosine
  • Local Scaling
  1. Articles
  2. Hubness

Hubness

  • Show All Code
  • Hide All Code

Hubs, a subject I find fascinating. A good place to start on this subject is Hubs in Space: Popular Nearest Neighbors in High-Dimensional Data. In high-dimensional data, when the k-nearest neighbors are calculated, a tendency emerges for a central point to end up being a neighbor of a lot of other points.

Because there are only k x N edges in the nearest neighbor graph, if one point gets more than its fair share of edges, by necessity other items are getting less. So this also leads to the phenomenon of “anti-hubs”, friendless items in the neighbor graph that no-one considers a near neighbor. Of course they still have k neighbors, but they don’t appear in the neigbor list of anything else.

A likely outcome for dimensionality reduction methods that can be classified as neighbor embeddings is that the hub point appears in many edges and hence gets a disproportionate amount of attention, while an increasing proportion of the dataset are anti-hubs which are effectively outliers. If you were to use the nearest neighbor graph as a search graph, you could never find these anti-hubs by walking the graph, so in that sense they aren’t representing your data usefully.

None of this seems like it’s good for visualization purposes. In reality, how bad is it? Below is some code to calculate the hubness of various datasets, based on the “k-occurrences” of each item. The k-occurrence just counts the number of times each item appears in the k-nearest neighbor list of other items. From a directed graph perspective, you could also say it’s the value of the “out-degree” of the item (count the number edges that point to the item). The hubness of a dataset is the maximum value of any k-occurrence in a dataset. An item is an anti-hub if it has a k-occurrence of 0.

Also, unrelated to hubs, but of interest for neighbor embeddings, I include code to calculate if a given nearest neighbor graph contains more than one component. A graph which is not fully connected can cause a spectral embedding to fail and mean you don’t have a good “global” view of the data.

Code
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from drnb.neighbors.hubness import (
    get_nbrs,
    k_occurrences,
    s_occurrences,
)


def hist_occurrences(
    name: str, n_neighbors: int, metric: str = "euclidean", log: bool = False
):
    """Plot histograms and scatter plot of k-occurrences and s-occurrences.

    Parameters
    ----------
    name : str
        Dataset name
    n_neighbors : int
        Number of neighbors used
    metric : str, default="euclidean"
        Distance metric used
    log : bool, default=False
        Whether to use log scale for histograms
    """
    nbrs = get_nbrs(name, n_neighbors, metric=metric)
    ko = k_occurrences(nbrs.idx) / n_neighbors
    so = s_occurrences(nbrs.idx) / n_neighbors
    df = pd.DataFrame(dict(nk_occurrence=ko, ns_occurrence=so))

    fig, axes = plt.subplots(1, 3, figsize=(12, 4))
    fig.tight_layout(pad=2.0)

    fig.suptitle(f"Hubness Analysis: {name} (k={n_neighbors}, {metric})", y=1.05)

    df.hist("nk_occurrence", ax=axes[0], grid=False, log=log)
    axes[0].set_title("k-occurrence Distribution")
    axes[0].set_xlabel("Normalized k-occurrence")
    axes[0].set_ylabel("Count")

    df.hist("ns_occurrence", ax=axes[1], grid=False, log=log)
    axes[1].set_title("s-occurrence Distribution")
    axes[1].set_xlabel("Normalized s-occurrence")
    axes[1].set_ylabel("Count")

    df.plot.scatter(
        "ns_occurrence", "nk_occurrence", ax=axes[2], grid=False, logy=True, alpha=0.5
    )
    axes[2].set_title("k-occurrence vs s-occurrence")

    sns.despine()
    plt.show()

Please be aware that calculating the k-occurrences (and converting to sparse format, needed for the connected component calculation) is done with numba, otherwise it’s rather slow, especially for larger values of n_neighbors.

Before we begin: you need to have some data saved

If you want to run this yourself, you should have some actual datasets save to DRNB_HOME. See the example notebooks in data-pipeline folder for how to do that.

To warm up the numba JIT and to have something to refer to as I go over the columns, let’s run the code for two small datasets with a low n_neighbors:

Code
from drnb.neighbors.hubness import nbr_stats_summary

hubness15 = nbr_stats_summary(15, names=["iris", "s1k"])
hubness15
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
iris 150 4 2 3.39 0 15.5 30 0.04 1 2.00 1.03 0.67 0 12.0 -0.68 1 0.80 0.67
s1k 1000 9 1 9.09 0 13.0 69 1.60 9 4.60 0.87 0.90 0 9.0 -0.17 10 0.60 1.00

Here are what all those numbers mean:

  • n_items: the number of items in the dataset.
  • n_dim: the number of features (columns) in the dataset.
  • n_comps: the number of connected components in the neighbor graph.
  • dint: an estimate of the intrinsic dimensionality of the dataset using the method of MacKay and Ghahramani, which makes use of the nearest neighbor distances.
  • kmin, kmedian, kmax: these are summary statistics of the k-occurrences.
    • max is of particular importance because that is the hubness.
    • why is there no mean? This is of no interest when it comes to k-occurrences. There are always n_neighbors * N edges in the nearest neighbor graph, so the mean number of k-occurrences will always be n_neighbors.
    • Instead, I have included the median which is most definitely of interest.
  • kskew is the skewness of the k-occurrences, which is used a lot to characterized hubness in datasets by the authors of the “Hubs in Space” paper.
  • k#0: is the number of items in the dataset with 0 k-occurrence: this is the count of anti-hubs in the dataset.
  • nkmax: a normalized version of kmax, using n_neighbors to normalize. If the number of edges in the graph were distributed “fairly” this value would be 1. While obviously the maximum possible value is controlled by n_items rather than n_neighbors, using n_neighbors gives easier-to-understand number for me it can be interpreted as the number of times more than the “fair share” of edges an item get. Roughly, a value > 10 seems to be when you might be concerned.
  • nkmedian: the normalized version kmedian, normalized by n_neighbors, just like nkmax.
  • k#0%: k#0 as a percentage of the dataset, e.g. this one is normalized by n_items. So this is saying what percentage of your dataset are anti-hubs.
  • smin, smedian, sskew, s#0, nsmedian, s#0%: these are analogous to the values which have a k-prefix instead of an s and these measure what I am calling the “s-occurrence” in analogy with “k-occurrence”. The s-occurrence counts the symmetric nearest neighbors for an item, i.e. the number of times an item is considered a k-nearest neighbor of its neighbors.

If you want to visualize the distribution of (normalized) k-occurrences and s-occurrences:

Code
hist_occurrences("s1k", 15)

Code
nbr_stats_summary(15, names=["iris", "s1k"])
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
iris 150 4 2 3.39 0 15.5 30 0.04 1 2.00 1.03 0.67 0 12.0 -0.68 1 0.80 0.67
s1k 1000 9 1 9.09 0 13.0 69 1.60 9 4.60 0.87 0.90 0 9.0 -0.17 10 0.60 1.00

Datasets

I chose datasets I was reasonably familiar with, a mixture of large, small, high-dimensional and low-dimensional, as well as synthetic and real data, including image data and scRNAseq data.

Code
datasets = [
    "avonet",
    "cifar10",
    "coil100",
    "coil20",
    "fashion",
    "frey",
    "gauss10D_10k",
    "gauss2D_10k",
    "gauss3D_10k",
    "gauss50D_10k",
    "gauss50D_1k",
    "gauss5D_10k",
    "iclr",
    "iris",
    "isofaces",
    "isoswiss",
    "kazer",
    "kuzushiji",
    "lamanno2020",
    "lamanno2020-pca50",
    "macosko2015",
    "macosko2015-pca50",
    "mammoth",
    "mnist",
    "ng20",
    "norb",
    "olivetti",
    "olivetti92x112",
    "onet271",
    "penguins",
    "randomwalks",
    "randomwalks-short",
    "s1k",
    "scurvehole",
    "spheres",
    "synthetic-hierarchical",
    "tasic2018",
    "tasic2018-pca50",
    "tomoradar",
]
len(datasets)
39

15 neighbors

So let’s look at the 15-neighbor result for some datasets. n_neighbors=15 is typical for UMAP results.

Code
hubness15 = nbr_stats_summary(n_neighbors=15, names=datasets)
hubness15
[16:18:08] INFO     Writing pkl format to                                             hubness.py:381
                    nn/randomwalks.15.euclidean.exact.faiss.stats.npy                               
           INFO     Writing pkl format to                                             hubness.py:381
                    nn/randomwalks-short.15.euclidean.exact.faiss.stats.npy                         
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
avonet 11009 11 1 4.71 0 15.0 44 0.33 39 2.93 1.00 0.35 0 10.0 -0.45 75 0.67 0.68
cifar10 60000 3072 1 34.81 0 2.0 2005 9.52 18689 133.67 0.13 31.15 0 0.0 3.07 36930 0.00 61.55
coil100 7200 49152 13 4.61 1 14.0 67 1.40 0 4.47 0.93 0.00 0 12.0 -0.73 2 0.80 0.03
coil20 1440 16384 3 4.77 2 14.0 52 1.08 0 3.47 0.93 0.00 2 12.0 -0.78 0 0.80 0.00
fashion 70000 784 1 17.98 0 10.0 267 2.72 6728 17.80 0.67 9.61 0 5.0 0.41 10296 0.33 14.71
frey 1965 560 1 6.91 0 14.0 45 0.66 6 3.00 0.93 0.31 0 10.0 -0.31 11 0.67 0.56
gauss10D_10k 10000 10 1 11.46 0 14.0 45 0.42 129 3.00 0.93 1.29 0 8.0 -0.06 166 0.53 1.66
gauss2D_10k 10000 2 1 2.43 1 15.0 24 -0.17 0 1.60 1.00 0.00 1 13.0 -1.31 0 0.87 0.00
gauss3D_10k 10000 3 1 3.68 0 15.0 26 -0.40 5 1.73 1.00 0.05 0 13.0 -1.22 6 0.87 0.06
gauss50D_10k 10000 50 1 37.71 0 6.0 566 5.75 1235 37.73 0.40 12.35 0 3.0 0.90 1568 0.20 15.68
gauss50D_1k 1000 50 1 32.41 0 8.0 345 5.55 67 23.00 0.53 6.70 0 5.0 0.46 79 0.33 7.90
gauss5D_10k 10000 5 1 6.16 0 15.0 30 -0.50 24 2.00 1.00 0.24 0 11.0 -0.72 28 0.73 0.28
iclr 14732 3000 1 69.60 0 5.0 9023 51.43 1764 601.53 0.33 11.97 0 3.0 0.93 2280 0.20 15.48
iris 150 4 2 3.39 0 15.5 30 0.04 1 2.00 1.03 0.67 0 12.0 -0.68 1 0.80 0.67
isofaces 698 4096 1 4.70 2 15.0 30 0.29 0 2.00 1.00 0.00 2 12.0 -0.69 0 0.80 0.00
isoswiss 20000 3 1 2.41 3 15.0 27 0.01 0 1.80 1.00 0.00 3 14.0 -1.09 0 0.93 0.00
kazer 59286 70 1 30.40 0 8.0 433 4.11 5343 28.87 0.53 9.01 0 3.0 0.95 10083 0.20 17.01
kuzushiji 70000 784 1 21.66 0 12.0 205 2.02 1558 13.67 0.80 2.23 0 6.0 0.22 3705 0.40 5.29
lamanno2020 3850 1999 1 51.26 0 9.0 169 2.28 387 11.27 0.60 10.05 0 4.0 0.59 631 0.27 16.39
lamanno2020-pca50 3850 50 1 12.28 0 14.0 57 0.80 14 3.80 0.93 0.36 0 10.0 -0.31 18 0.67 0.47
macosko2015 44808 3000 1 112.48 0 1.0 10542 36.61 20450 702.80 0.07 45.64 0 0.0 5.83 39658 0.00 88.51
macosko2015-pca50 44808 50 1 25.24 0 11.0 206 2.80 1076 13.73 0.73 2.40 0 6.0 0.27 1812 0.40 4.04
mammoth 50000 3 1 2.50 2 15.0 36 0.18 0 2.40 1.00 0.00 2 13.0 -1.03 0 0.87 0.00
mnist 70000 784 1 16.41 0 13.0 81 0.95 776 5.40 0.87 1.11 0 8.0 -0.05 1627 0.53 2.32
ng20 18846 3000 1 20.98 0 4.0 8351 33.46 2117 556.73 0.27 11.23 0 1.0 1.91 6338 0.07 33.63
norb 48600 9216 2 6.74 0 13.0 156 2.65 1 10.40 0.87 0.00 0 9.0 -0.03 16 0.60 0.03
olivetti 400 4096 1 9.53 1 11.0 95 1.85 0 6.33 0.73 0.00 1 8.0 0.09 0 0.53 0.00
olivetti92x112 400 10304 1 9.28 1 13.0 66 1.52 0 4.40 0.87 0.00 1 10.0 -0.29 0 0.67 0.00
onet271 873 82 1 12.17 0 13.0 64 1.13 19 4.27 0.87 2.18 0 9.0 -0.19 24 0.60 2.75
penguins 342 4 2 4.20 0 14.0 34 0.24 2 2.27 0.93 0.58 0 11.0 -0.65 2 0.73 0.58
randomwalks 70000 768 1 2.71 7 15.0 64 11.34 0 4.27 1.00 0.00 5 15.0 -3.04 0 1.00 0.00
randomwalks-short 70000 768 1 2.76 7 15.0 471 22.15 0 31.40 1.00 0.00 1 15.0 -2.65 0 1.00 0.00
s1k 1000 9 1 9.09 0 13.0 69 1.60 9 4.60 0.87 0.90 0 9.0 -0.17 10 0.60 1.00
scurvehole 9505 3 1 2.39 4 15.0 26 -0.01 0 1.73 1.00 0.00 4 14.0 -1.12 0 0.93 0.00
spheres 10000 101 1 75.36 0 4.5 127 0.85 4999 8.47 0.30 49.99 0 1.5 0.12 5000 0.10 50.00
synthetic-hierarchical 62500 50 125 29.80 0 8.0 260 3.13 4438 17.33 0.53 7.10 0 5.0 0.43 5141 0.33 8.23
tasic2018 23822 3000 5 59.12 0 7.0 667 4.77 4041 44.47 0.47 16.96 0 2.0 1.17 8476 0.13 35.58
tasic2018-pca50 23822 50 5 13.98 0 13.0 94 1.23 140 6.27 0.87 0.59 0 9.0 -0.24 215 0.60 0.90
tomoradar 120024 8192 2 2.89 0 15.0 40 -0.34 90 2.67 1.00 0.07 0 15.0 -4.83 94 1.00 0.08

A lot going on here. To get a grip, let’s look at what I consider low-dimensional datasets, those with less than 100 dimensions in the initial data:

Code
hubness15.loc[hubness15["n_dim"] < 100]
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
avonet 11009 11 1 4.71 0 15.0 44 0.33 39 2.93 1.00 0.35 0 10.0 -0.45 75 0.67 0.68
gauss10D_10k 10000 10 1 11.46 0 14.0 45 0.42 129 3.00 0.93 1.29 0 8.0 -0.06 166 0.53 1.66
gauss2D_10k 10000 2 1 2.43 1 15.0 24 -0.17 0 1.60 1.00 0.00 1 13.0 -1.31 0 0.87 0.00
gauss3D_10k 10000 3 1 3.68 0 15.0 26 -0.40 5 1.73 1.00 0.05 0 13.0 -1.22 6 0.87 0.06
gauss50D_10k 10000 50 1 37.71 0 6.0 566 5.75 1235 37.73 0.40 12.35 0 3.0 0.90 1568 0.20 15.68
gauss50D_1k 1000 50 1 32.41 0 8.0 345 5.55 67 23.00 0.53 6.70 0 5.0 0.46 79 0.33 7.90
gauss5D_10k 10000 5 1 6.16 0 15.0 30 -0.50 24 2.00 1.00 0.24 0 11.0 -0.72 28 0.73 0.28
iris 150 4 2 3.39 0 15.5 30 0.04 1 2.00 1.03 0.67 0 12.0 -0.68 1 0.80 0.67
isoswiss 20000 3 1 2.41 3 15.0 27 0.01 0 1.80 1.00 0.00 3 14.0 -1.09 0 0.93 0.00
kazer 59286 70 1 30.40 0 8.0 433 4.11 5343 28.87 0.53 9.01 0 3.0 0.95 10083 0.20 17.01
lamanno2020-pca50 3850 50 1 12.28 0 14.0 57 0.80 14 3.80 0.93 0.36 0 10.0 -0.31 18 0.67 0.47
macosko2015-pca50 44808 50 1 25.24 0 11.0 206 2.80 1076 13.73 0.73 2.40 0 6.0 0.27 1812 0.40 4.04
mammoth 50000 3 1 2.50 2 15.0 36 0.18 0 2.40 1.00 0.00 2 13.0 -1.03 0 0.87 0.00
onet271 873 82 1 12.17 0 13.0 64 1.13 19 4.27 0.87 2.18 0 9.0 -0.19 24 0.60 2.75
penguins 342 4 2 4.20 0 14.0 34 0.24 2 2.27 0.93 0.58 0 11.0 -0.65 2 0.73 0.58
s1k 1000 9 1 9.09 0 13.0 69 1.60 9 4.60 0.87 0.90 0 9.0 -0.17 10 0.60 1.00
scurvehole 9505 3 1 2.39 4 15.0 26 -0.01 0 1.73 1.00 0.00 4 14.0 -1.12 0 0.93 0.00
synthetic-hierarchical 62500 50 125 29.80 0 8.0 260 3.13 4438 17.33 0.53 7.10 0 5.0 0.43 5141 0.33 8.23
tasic2018-pca50 23822 50 5 13.98 0 13.0 94 1.23 140 6.27 0.87 0.59 0 9.0 -0.24 215 0.60 0.90

Most of these datasets are pretty well-behaved. You can see the slow onset of hubness with dimensionality by looking at the gauss datasets, although things are manageable at 10D. It takes the jump to 50D for things to get bad. Another synthetic dataset, synthetic-hierarchical, also shows hubness.

Of the “real” datasets that we might be concerned about, macosko2015-pca50, kazer are both scRNAseq datasets. However, lamanno2020-pca50 and tasic2018-pca50 are both scRNAseq datasets and aren’t so bad.

What about high-dimensional datasets? For this, I define high-dimensional as > 1000 dimensions.

Code
hubness15.loc[hubness15["n_dim"] >= 1000]
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
cifar10 60000 3072 1 34.81 0 2.0 2005 9.52 18689 133.67 0.13 31.15 0 0.0 3.07 36930 0.00 61.55
coil100 7200 49152 13 4.61 1 14.0 67 1.40 0 4.47 0.93 0.00 0 12.0 -0.73 2 0.80 0.03
coil20 1440 16384 3 4.77 2 14.0 52 1.08 0 3.47 0.93 0.00 2 12.0 -0.78 0 0.80 0.00
iclr 14732 3000 1 69.60 0 5.0 9023 51.43 1764 601.53 0.33 11.97 0 3.0 0.93 2280 0.20 15.48
isofaces 698 4096 1 4.70 2 15.0 30 0.29 0 2.00 1.00 0.00 2 12.0 -0.69 0 0.80 0.00
lamanno2020 3850 1999 1 51.26 0 9.0 169 2.28 387 11.27 0.60 10.05 0 4.0 0.59 631 0.27 16.39
macosko2015 44808 3000 1 112.48 0 1.0 10542 36.61 20450 702.80 0.07 45.64 0 0.0 5.83 39658 0.00 88.51
ng20 18846 3000 1 20.98 0 4.0 8351 33.46 2117 556.73 0.27 11.23 0 1.0 1.91 6338 0.07 33.63
norb 48600 9216 2 6.74 0 13.0 156 2.65 1 10.40 0.87 0.00 0 9.0 -0.03 16 0.60 0.03
olivetti 400 4096 1 9.53 1 11.0 95 1.85 0 6.33 0.73 0.00 1 8.0 0.09 0 0.53 0.00
olivetti92x112 400 10304 1 9.28 1 13.0 66 1.52 0 4.40 0.87 0.00 1 10.0 -0.29 0 0.67 0.00
tasic2018 23822 3000 5 59.12 0 7.0 667 4.77 4041 44.47 0.47 16.96 0 2.0 1.17 8476 0.13 35.58
tomoradar 120024 8192 2 2.89 0 15.0 40 -0.34 90 2.67 1.00 0.07 0 15.0 -4.83 94 1.00 0.08

Compared to the low-dimensional datasets, these datasets are much more prone to hubness. macosko2015 is particularly bad, but tasic2018 (also an scRNAseq dataset), ng20 (a document dataset) and cifar10 (an image dataset) are also bad. Also lamanno2020 (yep, another scRNAseq dataset) is a bit concerning because 10% of the data are anti-hubs. On other hand, coil20, coil100 and olivetti92x112, despite being very high-dimensional, don’t show that bad a behavior, so simply having a large number of columns isn’t enough to indicate hubness.

tomoradar is delightfully weird. 8,192 dimensions, but with an estimated intrinsic dimensionality of less than 3! Not a very hubby dataset either.

Let’s compare the hubness of the original macosko2015 dataset with its PCA-reduced form:

Code
hist_occurrences("macosko2015", 15)

This seems very diagnostic of a very hubby dataset.

Code
hist_occurrences("macosko2015-pca50", 15)

The effect of PCA seems more dramatic with the s-occurrences. Let’s also look at isofaces as a well-behaved dataset:

Code
hist_occurrences("isofaces", 15)

The k-occurrences are noticeably more symmetrically distributed and also over a much smaller range. The s-occurrences are now definitely right-skewed.

So what about tomoradar?

Code
hist_occurrences("tomoradar", 15)

This is a very not-hubby dataset. And yet I’ve not seen a good embedding of it with this number of neighbors. For example, here’s UMAP (I’m using PCA to initialize the embedding to avoid any potential issues with the default spectral initialization due to the data being split into two components at this scale):

Code
import drnb.embed.pipeline as pl

pl.standard_eval(
    pl.embedder("umap", params=dict(n_neighbors=15, init="pca")), dataset="tomoradar"
)
/home/james/dev/drnb3/.venv/lib/python3.12/site-packages/sklearn/utils/deprecation.py:151: FutureWarning: 'force_all_finite' was renamed to 'ensure_all_finite' in 1.6 and will be removed in 1.8.
  warnings.warn(
[16:19:47] INFO     Loading faiss with AVX2 support.                                   loader.py:110
           INFO     Successfully loaded faiss with AVX2 support.                       loader.py:112

[EvalResult(eval_type='RTE', label='rte-5-euclidean', value=np.float64(0.7676714657068586), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='RPC', label='rpc-5-euclidean', value=np.float32(0.6035691), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='NNP', label='nnp-15-noself-euclidean', value=np.float64(0.7640932924526207), info={'metric': 'euclidean', 'n_neighbors': 15}),
 EvalResult(eval_type='NNP', label='nnp-50-noself-euclidean', value=np.float64(0.43773695260947815), info={'metric': 'euclidean', 'n_neighbors': 50}),
 EvalResult(eval_type='NNP', label='nnp-150-noself-euclidean', value=np.float64(0.3231359283698816), info={'metric': 'euclidean', 'n_neighbors': 150})]

This looks like a plate of unappetizing spaghetti. What causes this sort of appearance? Looking at the data as a whole, tomoradar has a similar set of metrics as the randomwalks dataset, which consists of 10 random walks of length 7000 in 768 dimensions. Here’s a summary of the randomwalks dataset:

Code
hist_occurrences("randomwalks", 15)

Code
pl.standard_eval(
    pl.embedder("umap", params=dict(n_neighbors=15, init="pca")), dataset="randomwalks"
)
/home/james/dev/drnb3/.venv/lib/python3.12/site-packages/sklearn/utils/deprecation.py:151: FutureWarning: 'force_all_finite' was renamed to 'ensure_all_finite' in 1.6 and will be removed in 1.8.
  warnings.warn(

[EvalResult(eval_type='RTE', label='rte-5-euclidean', value=np.float64(0.6896342857142856), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='RPC', label='rpc-5-euclidean', value=np.float32(0.5945123), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='NNP', label='nnp-15-noself-euclidean', value=np.float64(0.8593200000000001), info={'metric': 'euclidean', 'n_neighbors': 15}),
 EvalResult(eval_type='NNP', label='nnp-50-noself-euclidean', value=np.float64(0.7614540000000001), info={'metric': 'euclidean', 'n_neighbors': 50}),
 EvalResult(eval_type='NNP', label='nnp-150-noself-euclidean', value=np.float64(0.5774100952380952), info={'metric': 'euclidean', 'n_neighbors': 150})]

Another plate of spaghetti. Surprisingly low intrinsic dimensionality and wiggly lines in the UMAP may therefore be a diagnostic that your data is dominated by random walks. How to fix this is another question and one I will defer to another time (because I have no idea).

50 neighbors

Code
hubness50 = nbr_stats_summary(n_neighbors=50, names=datasets)
hubness50
[16:20:06] INFO     Writing pkl format to                                             hubness.py:381
                    nn/randomwalks.50.euclidean.exact.faiss.stats.npy                               
[16:20:07] INFO     Writing pkl format to                                             hubness.py:381
                    nn/randomwalks-short.50.euclidean.exact.faiss.stats.npy                         
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
avonet 11009 11 1 4.69 0 49.0 125 0.22 9 2.50 0.98 0.08 0 34.0 -0.47 10 0.68 0.09
cifar10 60000 3072 1 28.32 0 8.0 4261 7.52 10114 85.22 0.16 16.86 0 1.0 2.94 24051 0.02 40.09
coil100 7200 49152 5 5.47 2 44.0 295 1.75 0 5.90 0.88 0.00 1 31.0 -0.16 0 0.62 0.00
coil20 1440 16384 2 4.99 4 44.0 158 0.90 0 3.16 0.88 0.00 3 35.0 -0.34 0 0.70 0.00
fashion 70000 784 1 14.86 0 37.0 759 2.34 3165 15.18 0.74 4.52 0 19.0 0.26 4722 0.38 6.75
frey 1965 560 1 6.45 0 46.0 176 0.88 1 3.52 0.92 0.05 0 32.0 -0.32 1 0.64 0.05
gauss10D_10k 10000 10 1 10.02 0 44.0 165 0.68 23 3.30 0.88 0.23 0 26.0 -0.07 25 0.52 0.25
gauss2D_10k 10000 2 1 2.18 3 50.0 69 -1.60 0 1.38 1.00 0.00 3 46.0 -2.27 0 0.92 0.00
gauss3D_10k 10000 3 1 3.29 1 51.0 73 -1.69 0 1.46 1.02 0.00 1 43.0 -1.51 0 0.86 0.00
gauss50D_10k 10000 50 1 31.29 0 22.0 1434 4.88 299 28.68 0.44 2.99 0 13.0 0.75 335 0.26 3.35
gauss50D_1k 1000 50 1 25.60 0 33.0 648 2.97 9 12.96 0.66 0.90 0 22.0 0.17 9 0.44 0.90
gauss5D_10k 10000 5 1 5.42 0 55.0 85 -0.87 5 1.70 1.10 0.05 0 36.0 -0.76 5 0.72 0.05
iclr 14732 3000 1 65.97 0 15.0 13904 27.40 476 278.08 0.30 3.23 0 10.0 1.10 607 0.20 4.12
iris 150 4 1 1.45 12 49.0 95 0.21 0 1.90 0.98 0.00 12 48.0 -1.46 0 0.96 0.00
isofaces 698 4096 1 5.21 7 48.0 113 0.46 0 2.26 0.96 0.00 4 37.0 -0.37 0 0.74 0.00
isoswiss 20000 3 1 2.13 21 50.0 78 -0.32 0 1.56 1.00 0.00 21 47.0 -1.96 0 0.94 0.00
kazer 59286 70 1 24.23 0 28.0 1324 3.72 1189 26.48 0.56 2.01 0 12.0 0.80 2334 0.24 3.94
kuzushiji 70000 784 1 18.53 0 41.0 851 3.03 229 17.02 0.82 0.33 0 21.0 0.23 793 0.42 1.13
lamanno2020 3850 1999 1 36.38 0 36.0 466 1.99 85 9.32 0.72 2.21 0 21.0 0.21 119 0.42 3.09
lamanno2020-pca50 3850 50 1 8.68 1 47.0 173 0.61 0 3.46 0.94 0.00 1 37.0 -0.55 0 0.74 0.00
macosko2015 44808 3000 1 89.98 0 4.0 18582 24.91 11609 371.64 0.08 25.91 0 0.0 5.00 35291 0.00 78.76
macosko2015-pca50 44808 50 1 19.87 0 38.0 647 2.75 77 12.94 0.76 0.17 0 23.0 0.16 171 0.46 0.38
mammoth 50000 3 1 2.22 11 50.0 101 0.24 0 2.02 1.00 0.00 11 45.0 -1.12 0 0.90 0.00
mnist 70000 784 1 13.86 0 45.0 226 0.83 73 4.52 0.90 0.10 0 28.0 -0.13 249 0.56 0.36
ng20 18846 3000 1 33.76 0 6.0 16017 18.92 1693 320.34 0.12 8.98 0 1.0 4.67 5639 0.02 29.92
norb 48600 9216 2 9.76 1 37.0 744 2.82 0 14.88 0.74 0.00 0 22.0 0.33 2 0.44 0.00
olivetti 400 4096 1 10.11 2 39.0 251 1.47 0 5.02 0.78 0.00 2 26.5 0.02 0 0.53 0.00
olivetti92x112 400 10304 1 10.98 4 42.0 154 0.79 0 3.08 0.84 0.00 4 32.0 -0.26 0 0.64 0.00
onet271 873 82 1 9.08 0 46.0 180 0.75 2 3.60 0.92 0.23 0 34.0 -0.55 2 0.68 0.23
penguins 342 4 1 2.95 2 49.5 101 0.25 0 2.02 0.99 0.00 2 41.0 -0.98 0 0.82 0.00
randomwalks 70000 768 1 2.28 24 50.0 228 26.36 0 4.56 1.00 0.00 14 49.0 -10.05 0 0.98 0.00
randomwalks-short 70000 768 1 2.46 22 42.0 2080 13.47 0 41.60 0.84 0.00 1 41.0 -1.04 0 0.82 0.00
s1k 1000 9 1 7.17 2 44.0 165 0.93 0 3.30 0.88 0.00 2 33.0 -0.42 0 0.66 0.00
scurvehole 9505 3 1 2.10 17 50.0 76 -0.32 0 1.52 1.00 0.00 17 47.0 -1.67 0 0.94 0.00
spheres 10000 101 1 58.30 0 22.5 329 0.56 4999 6.58 0.45 49.99 0 12.5 0.03 5000 0.25 50.00
synthetic-hierarchical 62500 50 125 22.74 0 34.0 415 1.94 512 8.30 0.68 0.82 0 24.0 0.11 530 0.48 0.85
tasic2018 23822 3000 1 39.78 0 32.0 1050 2.77 1208 21.00 0.64 5.07 0 12.0 0.63 3125 0.24 13.12
tasic2018-pca50 23822 50 2 9.80 0 46.0 222 0.93 16 4.44 0.92 0.07 0 34.0 -0.43 22 0.68 0.09
tomoradar 120024 8192 1 7.06 0 45.0 517 2.09 89 10.34 0.90 0.07 0 37.0 -0.35 94 0.74 0.08
Code
hubness50.loc[hubness50["n_dim"] < 100]
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
avonet 11009 11 1 4.69 0 49.0 125 0.22 9 2.50 0.98 0.08 0 34.0 -0.47 10 0.68 0.09
gauss10D_10k 10000 10 1 10.02 0 44.0 165 0.68 23 3.30 0.88 0.23 0 26.0 -0.07 25 0.52 0.25
gauss2D_10k 10000 2 1 2.18 3 50.0 69 -1.60 0 1.38 1.00 0.00 3 46.0 -2.27 0 0.92 0.00
gauss3D_10k 10000 3 1 3.29 1 51.0 73 -1.69 0 1.46 1.02 0.00 1 43.0 -1.51 0 0.86 0.00
gauss50D_10k 10000 50 1 31.29 0 22.0 1434 4.88 299 28.68 0.44 2.99 0 13.0 0.75 335 0.26 3.35
gauss50D_1k 1000 50 1 25.60 0 33.0 648 2.97 9 12.96 0.66 0.90 0 22.0 0.17 9 0.44 0.90
gauss5D_10k 10000 5 1 5.42 0 55.0 85 -0.87 5 1.70 1.10 0.05 0 36.0 -0.76 5 0.72 0.05
iris 150 4 1 1.45 12 49.0 95 0.21 0 1.90 0.98 0.00 12 48.0 -1.46 0 0.96 0.00
isoswiss 20000 3 1 2.13 21 50.0 78 -0.32 0 1.56 1.00 0.00 21 47.0 -1.96 0 0.94 0.00
kazer 59286 70 1 24.23 0 28.0 1324 3.72 1189 26.48 0.56 2.01 0 12.0 0.80 2334 0.24 3.94
lamanno2020-pca50 3850 50 1 8.68 1 47.0 173 0.61 0 3.46 0.94 0.00 1 37.0 -0.55 0 0.74 0.00
macosko2015-pca50 44808 50 1 19.87 0 38.0 647 2.75 77 12.94 0.76 0.17 0 23.0 0.16 171 0.46 0.38
mammoth 50000 3 1 2.22 11 50.0 101 0.24 0 2.02 1.00 0.00 11 45.0 -1.12 0 0.90 0.00
onet271 873 82 1 9.08 0 46.0 180 0.75 2 3.60 0.92 0.23 0 34.0 -0.55 2 0.68 0.23
penguins 342 4 1 2.95 2 49.5 101 0.25 0 2.02 0.99 0.00 2 41.0 -0.98 0 0.82 0.00
s1k 1000 9 1 7.17 2 44.0 165 0.93 0 3.30 0.88 0.00 2 33.0 -0.42 0 0.66 0.00
scurvehole 9505 3 1 2.10 17 50.0 76 -0.32 0 1.52 1.00 0.00 17 47.0 -1.67 0 0.94 0.00
synthetic-hierarchical 62500 50 125 22.74 0 34.0 415 1.94 512 8.30 0.68 0.82 0 24.0 0.11 530 0.48 0.85
tasic2018-pca50 23822 50 2 9.80 0 46.0 222 0.93 16 4.44 0.92 0.07 0 34.0 -0.43 22 0.68 0.09

Going to 50 neighbors doesn’t seem to change things much here. The same two datasets (kazer and macosko2015-pca50) are still the ones we are concerned about.

Code
hubness50.loc[hubness50["n_dim"] >= 1000]
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
cifar10 60000 3072 1 28.32 0 8.0 4261 7.52 10114 85.22 0.16 16.86 0 1.0 2.94 24051 0.02 40.09
coil100 7200 49152 5 5.47 2 44.0 295 1.75 0 5.90 0.88 0.00 1 31.0 -0.16 0 0.62 0.00
coil20 1440 16384 2 4.99 4 44.0 158 0.90 0 3.16 0.88 0.00 3 35.0 -0.34 0 0.70 0.00
iclr 14732 3000 1 65.97 0 15.0 13904 27.40 476 278.08 0.30 3.23 0 10.0 1.10 607 0.20 4.12
isofaces 698 4096 1 5.21 7 48.0 113 0.46 0 2.26 0.96 0.00 4 37.0 -0.37 0 0.74 0.00
lamanno2020 3850 1999 1 36.38 0 36.0 466 1.99 85 9.32 0.72 2.21 0 21.0 0.21 119 0.42 3.09
macosko2015 44808 3000 1 89.98 0 4.0 18582 24.91 11609 371.64 0.08 25.91 0 0.0 5.00 35291 0.00 78.76
ng20 18846 3000 1 33.76 0 6.0 16017 18.92 1693 320.34 0.12 8.98 0 1.0 4.67 5639 0.02 29.92
norb 48600 9216 2 9.76 1 37.0 744 2.82 0 14.88 0.74 0.00 0 22.0 0.33 2 0.44 0.00
olivetti 400 4096 1 10.11 2 39.0 251 1.47 0 5.02 0.78 0.00 2 26.5 0.02 0 0.53 0.00
olivetti92x112 400 10304 1 10.98 4 42.0 154 0.79 0 3.08 0.84 0.00 4 32.0 -0.26 0 0.64 0.00
tasic2018 23822 3000 1 39.78 0 32.0 1050 2.77 1208 21.00 0.64 5.07 0 12.0 0.63 3125 0.24 13.12
tomoradar 120024 8192 1 7.06 0 45.0 517 2.09 89 10.34 0.90 0.07 0 37.0 -0.35 94 0.74 0.08

With the high-dimensional datasets, increasing the number of neighbors seems to have more of an effect. The same datasets are still looking really hubby, but the extent of hubness is now more reduced. That said, it’s still not good.

But let’s see if cranking up the number of neighbors has diminishing returns or we were just not being ambitious enough.

150 neighbors

Code
hubness150 = nbr_stats_summary(n_neighbors=150, names=datasets)
hubness150
[16:20:29] INFO     Writing pkl format to                                             hubness.py:381
                    nn/randomwalks.150.euclidean.exact.faiss.stats.npy                              
[16:20:32] INFO     Writing pkl format to                                             hubness.py:381
                    nn/randomwalks-short.150.euclidean.exact.faiss.stats.npy                        
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
avonet 11009 11 1 4.53 0 146.0 375 0.25 1 2.50 0.97 0.01 0 98.0 -0.43 1 0.65 0.01
cifar10 60000 3072 1 24.04 0 29.0 7902 6.02 4623 52.68 0.19 7.71 0 5.0 2.50 12680 0.03 21.13
coil100 7200 49152 1 4.49 2 111.0 1119 2.32 0 7.46 0.74 0.00 2 80.0 0.01 0 0.53 0.00
coil20 1440 16384 1 4.27 4 113.0 584 1.35 0 3.89 0.75 0.00 4 82.0 0.14 0 0.55 0.00
fashion 70000 784 1 12.94 0 117.0 1772 1.93 1601 11.81 0.78 2.29 0 67.0 0.11 2262 0.45 3.23
frey 1965 560 1 6.39 2 138.0 434 0.64 0 2.89 0.92 0.00 2 89.0 -0.09 0 0.59 0.00
gauss10D_10k 10000 10 1 9.21 0 126.0 617 1.01 3 4.11 0.84 0.03 0 79.0 -0.05 3 0.53 0.03
gauss2D_10k 10000 2 1 2.09 5 154.0 185 -3.05 0 1.23 1.03 0.00 5 137.0 -2.14 0 0.91 0.00
gauss3D_10k 10000 3 1 3.15 2 162.0 203 -1.85 0 1.35 1.08 0.00 2 124.0 -1.30 0 0.83 0.00
gauss50D_10k 10000 50 1 27.60 0 75.0 3198 3.90 55 21.32 0.50 0.55 0 47.0 0.58 57 0.31 0.57
gauss50D_1k 1000 50 1 20.62 2 113.0 906 1.43 0 6.04 0.75 0.00 2 84.0 -0.11 0 0.56 0.00
gauss5D_10k 10000 5 1 5.13 0 161.0 251 -0.52 1 1.67 1.07 0.01 0 103.0 -0.60 1 0.69 0.01
iclr 16553 100 1 16.85 0 88.0 5085 7.71 25 33.90 0.59 0.15 0 42.0 0.80 39 0.28 0.24
iris 150 4 1 1.01 148 149.0 150 0.00 0 1.00 0.99 0.00 148 149.0 -12.25 0 0.99 0.00
isofaces 698 4096 1 4.75 24 157.0 284 -0.26 0 1.89 1.05 0.00 24 129.0 -1.04 0 0.86 0.00
isoswiss 20000 3 1 2.03 55 151.0 233 -0.75 0 1.55 1.01 0.00 55 145.0 -2.70 0 0.97 0.00
kazer 59286 70 1 20.21 0 93.0 3323 3.24 211 22.15 0.62 0.36 0 45.0 0.63 389 0.30 0.66
kuzushiji 70000 784 1 17.38 0 120.0 3056 4.22 31 20.37 0.80 0.04 0 61.0 0.30 154 0.41 0.22
lamanno2020 3850 1999 1 29.15 0 117.0 1180 2.05 15 7.87 0.78 0.39 0 80.0 -0.09 23 0.53 0.60
lamanno2020-pca50 3850 50 1 6.87 10 144.0 490 0.83 0 3.27 0.96 0.00 10 117.0 -0.81 0 0.78 0.00
macosko2015 44808 3000 1 76.47 0 15.0 26161 15.21 5476 174.41 0.10 12.22 0 0.0 3.94 30417 0.00 67.88
macosko2015-pca50 44808 50 1 16.19 0 121.0 1706 2.61 1 11.37 0.81 0.00 0 78.0 0.02 5 0.52 0.01
mammoth 50000 3 1 2.25 34 149.0 280 0.07 0 1.87 0.99 0.00 34 134.0 -1.17 0 0.89 0.00
mnist 70000 784 1 12.54 0 139.0 605 0.70 2 4.03 0.93 0.00 0 87.0 -0.18 42 0.58 0.06
ng20 18846 2500 1 24.05 0 124.0 1208 1.56 7 8.05 0.83 0.04 0 89.0 -0.24 9 0.59 0.05
norb 48600 9216 2 11.16 3 109.0 1740 2.46 0 11.60 0.73 0.00 2 61.0 0.32 0 0.41 0.00
olivetti 400 4096 1 7.93 6 128.5 399 0.81 0 2.66 0.86 0.00 6 101.0 -0.22 0 0.67 0.00
olivetti92x112 400 10304 1 8.82 15 141.5 381 0.51 0 2.54 0.94 0.00 15 115.5 -0.67 0 0.77 0.00
onet271 873 82 1 6.92 6 143.0 400 0.40 0 2.67 0.95 0.00 6 112.0 -0.73 0 0.75 0.00
penguins 342 4 1 1.80 37 133.0 320 0.77 0 2.13 0.89 0.00 37 125.0 -0.96 0 0.83 0.00
randomwalks 70000 768 1 2.12 72 150.0 728 18.51 0 4.85 1.00 0.00 39 148.0 -7.49 0 0.99 0.00
randomwalks-short 70000 768 1 3.82 44 75.0 11749 10.44 0 78.33 0.50 0.00 2 56.0 -0.61 0 0.37 0.00
s1k 1000 9 1 6.17 10 128.0 581 1.40 0 3.87 0.85 0.00 10 101.0 -0.34 0 0.67 0.00
scurvehole 9505 3 1 1.98 60 152.0 232 -0.55 0 1.55 1.01 0.00 60 144.0 -1.79 0 0.96 0.00
spheres 10000 101 1 44.70 0 76.5 723 0.38 4995 4.82 0.51 49.95 0 57.5 0.01 4996 0.38 49.96
synthetic-hierarchical 62500 50 125 17.11 0 131.0 496 0.76 15 3.31 0.87 0.02 0 100.0 -0.38 15 0.67 0.02
tasic2018 23822 3000 1 25.25 0 122.0 1550 1.65 264 10.33 0.81 1.11 0 66.0 0.15 854 0.44 3.58
tasic2018-pca50 23822 50 1 6.39 0 141.0 557 0.75 2 3.71 0.94 0.01 0 111.0 -0.62 4 0.74 0.02
tomoradar 120024 8192 1 13.32 0 136.0 1158 1.73 87 7.72 0.91 0.07 0 108.0 -0.50 94 0.72 0.08

Going to 150 neighbors continues the effect of going to 50 neighbors. It improves the hubness situation further but does not remove it. At this rate we would need a lot more neighbors to get rid of it for datasets like macosko2015, but we would be running into long runtimes with nearest neighbor searches and high memory usage. We may also be well outside the regime where the nearest neighbor graph is a good approximation to the true manifold.

Code
hist_occurrences("macosko2015", 150)

macosko2015 still not looking good. Meanwhile, what’s going on with tomoradar?

Code
hist_occurrences("tomoradar", 150)

Rather different distributions from the 15-neighbor case, looks pretty normal. Also there has been a leap in the estimated intrinsic dimensionality. Perhaps a larger number of neighbors would be better for this dataset?

Code
pl.standard_eval(
    pl.embedder("umap", params=dict(n_neighbors=150, init="pca")), dataset="tomoradar"
)
/home/james/dev/drnb3/.venv/lib/python3.12/site-packages/sklearn/utils/deprecation.py:151: FutureWarning: 'force_all_finite' was renamed to 'ensure_all_finite' in 1.6 and will be removed in 1.8.
  warnings.warn(

[EvalResult(eval_type='RTE', label='rte-5-euclidean', value=np.float64(0.7869009531427049), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='RPC', label='rpc-5-euclidean', value=np.float32(0.7017014), info={'metric': 'euclidean', 'ntpp': 5}),
 EvalResult(eval_type='NNP', label='nnp-15-noself-euclidean', value=np.float64(0.2714079406340954), info={'metric': 'euclidean', 'n_neighbors': 15}),
 EvalResult(eval_type='NNP', label='nnp-50-noself-euclidean', value=np.float64(0.3806590348596947), info={'metric': 'euclidean', 'n_neighbors': 50}),
 EvalResult(eval_type='NNP', label='nnp-150-noself-euclidean', value=np.float64(0.5060730631651448), info={'metric': 'euclidean', 'n_neighbors': 150})]

Doesn’t look fantastic, but it’s better than the 15-neighbor case.

NG20 Euclidean vs Cosine

The ng20 dataset is a document dataset. In its form here it’s a 3000-dimension PCA dense vector representation of the original sparse TDF-IF data. We would expect that the cosine metric would be a better choice for this dataset and with the Euclidean metric, is does seem like there is a lot of hubness going on with 15 nearest neighbors:

Code
nbr_stats_summary(15, names=["ng20", "iclr"], metric="euclidean")
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
ng20 18846 3000 1 20.98 0 4.0 8351 33.46 2117 556.73 0.27 11.23 0 1.0 1.91 6338 0.07 33.63
iclr 14732 3000 1 69.60 0 5.0 9023 51.43 1764 601.53 0.33 11.97 0 3.0 0.93 2280 0.20 15.48
Code
hist_occurrences("ng20", 15, log=True)

Code
hist_occurrences("iclr", 15, log=True)

Does switching to the cosine metric help:

Code
nbr_stats_summary(15, names=["ng20", "iclr"], metric="cosine")
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
ng20 18846 3000 1 -4.84 0 11.0 2184 37.73 105 145.60 0.73 0.56 0 8.0 0.09 455 0.53 2.41
iclr 14732 3000 1 -10.11 0 12.0 165 2.11 126 11.00 0.80 0.86 0 8.0 0.00 203 0.53 1.38
Code
hist_occurrences("ng20", 15, metric="cosine", log=True)

Code
hist_occurrences("iclr", 15, metric="cosine", log=True)

Although the skew is larger for cosine k-occurrrence, the other metrics (e.g. maximum, median and number of anti-hubs) all show an improvement. The s-occurrences is also obviously better.

Here are the results for 150 neighbors:

Code
nbr_stats_summary(150, names=["ng20", "iclr"], metric="euclidean")
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
ng20 18846 2500 1 24.05 0 124.0 1208 1.56 7 8.05 0.83 0.04 0 89.0 -0.24 9 0.59 0.05
iclr 16553 100 1 16.85 0 88.0 5085 7.71 25 33.90 0.59 0.15 0 42.0 0.80 39 0.28 0.24
Code
hist_occurrences("ng20", 150, log=True)

Code
hist_occurrences("iclr", 150, log=True)

Code
nbr_stats_summary(150, names=["ng20", "iclr"], metric="cosine")
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
ng20 18846 3000 1 -4.76 0 76.0 12882 12.54 2 85.88 0.51 0.01 0 36.0 1.15 18 0.24 0.10
iclr 14732 3000 1 -6.75 6 128.0 915 1.71 0 6.10 0.85 0.00 6 97.0 -0.28 0 0.65 0.00
Code
hist_occurrences("ng20", 150, metric="cosine", log=True)

Code
hist_occurrences("iclr", 150, metric="cosine", log=True)

Again the cosine metric seems to do better in terms of hubness.

Local Scaling

Schnitzer and co-workers note that selecting neighbors based on locally re-scaling the nearest neighbor distances (as introduced by Zelnik-Manor and Perona in the context of spectral clustering) can reduce hubness. As we have this implemented to work with PaCMAP, let’s see if it works.

The nbr_stats_summary function has a transform parameter which can be used to apply local scaling via transform="local". You get no choice on the size of the extended neighborhood to sample from, it’s always n_neighbors + 50, so be aware of that if you need to generate a larger neighborhood than usual. You can also use transform="random" to randomly sample neighbors from the extended neighborhood. This is more a way to compare the effect of local scaling rather than a practical way to reduce hubness.

We’ll start with the basic 15-neighbor case then apply local scaling straight afterwards:

Code
nbr_stats_summary(15, names=["macosko2015", "tasic2018", "lamanno2020", "ng20", "iclr"])
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
macosko2015 44808 3000 1 112.48 0 1.0 10542 36.61 20450 702.80 0.07 45.64 0 0.0 5.83 39658 0.00 88.51
tasic2018 23822 3000 5 59.12 0 7.0 667 4.77 4041 44.47 0.47 16.96 0 2.0 1.17 8476 0.13 35.58
lamanno2020 3850 1999 1 51.26 0 9.0 169 2.28 387 11.27 0.60 10.05 0 4.0 0.59 631 0.27 16.39
ng20 18846 3000 1 20.98 0 4.0 8351 33.46 2117 556.73 0.27 11.23 0 1.0 1.91 6338 0.07 33.63
iclr 14732 3000 1 69.60 0 5.0 9023 51.43 1764 601.53 0.33 11.97 0 3.0 0.93 2280 0.20 15.48
Code
nbr_stats_summary(
    15,
    names=["macosko2015", "tasic2018", "lamanno2020", "ng20", "iclr"],
    transform="local",
)
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
macosko2015 44808 3000 1 69.31 0 2.0 1057 6.77 13427 70.47 0.13 29.97 0 0.0 2.81 34217 0.00 76.36
tasic2018 23822 3000 6 28.50 0 13.0 187 1.55 1065 12.47 0.87 4.47 0 9.0 -0.22 2385 0.60 10.01
lamanno2020 3850 1999 1 25.74 0 14.0 55 0.66 66 3.67 0.93 1.71 0 11.0 -0.62 85 0.73 2.21
ng20 18846 2500 1 8.84 0 14.0 82 1.62 12 5.47 0.93 0.06 0 11.0 -0.59 15 0.73 0.08
iclr 16553 100 1 12.20 0 14.0 66 0.82 174 4.40 0.93 1.05 0 10.0 -0.40 281 0.67 1.70

By the measures of hubness here, this does seem to do a very good job of reducing hubness. As a comparison, what if we just randomly sampled 15 neighbors from the 65 extended neighborhood?

Code
nbr_stats_summary(
    15,
    names=["macosko2015", "tasic2018", "lamanno2020", "ng20", "iclr"],
    transform="random",
)
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
macosko2015 44808 3000 1 97.28 0 1.0 4721 22.38 17955 314.73 0.07 40.07 0 0.0 5.74 41345 0.00 92.27
tasic2018 23822 3000 1 40.67 0 10.0 267 2.48 2529 17.80 0.67 10.62 0 1.0 1.26 10400 0.07 43.66
lamanno2020 3850 1999 1 38.01 0 11.0 133 1.96 195 8.87 0.73 5.06 0 1.0 0.88 1143 0.07 29.69
ng20 18846 2500 1 20.83 0 14.0 82 0.82 233 5.47 0.93 1.24 0 2.0 0.56 2559 0.13 13.58
iclr 16553 100 1 21.18 0 8.0 560 7.92 1074 37.33 0.53 6.49 0 1.0 1.38 6850 0.07 41.38

Helps a bit, but not as much as the local scaling. So there really is some benefit to it.

Finally, let’s see what happens if we just use the full 65 neighbors.

Code
nbr_stats_summary(65, names=["macosko2015", "tasic2018", "lamanno2020", "ng20", "iclr"])
n_items n_dim n_comps dint kmin kmedian kmax kskew k#0 nkmax nkmedian k#0% smin smedian sskew s#0 nsmedian s#0%
macosko2015 44808 3000 1 86.11 0 5.0 20520 22.39 9940 315.69 0.08 22.18 0 0.0 4.75 34145 0.00 76.20
tasic2018 23822 3000 1 36.24 0 44.0 1113 2.45 884 17.12 0.68 3.71 0 19.0 0.51 2349 0.29 9.86
lamanno2020 3850 1999 1 34.16 0 48.0 595 1.98 61 9.15 0.74 1.58 0 30.0 0.12 83 0.46 2.16
ng20 18846 2500 1 17.21 0 61.0 372 0.75 9 5.72 0.94 0.05 0 46.0 -0.63 11 0.71 0.06
iclr 16553 100 1 18.70 0 37.0 2374 8.04 162 36.52 0.57 0.98 0 17.0 0.82 249 0.26 1.50

In some ways this is a tiny bit better than the local scaling case, and in other ways less good, and local scaling has the benefit of returning a smaller neighborhood for downstream processing. So if you can afford the extra compute, local scaling is a good choice.