Code
%load_ext lab_black
%load_ext autotime
import numpy as np
import pandas as pdtime: 245 ms (started: 2022-09-30 14:37:06 -07:00)
time: 245 ms (started: 2022-09-30 14:37:06 -07:00)
As mentioned in the hubness.ipynb notebook, the paper Hubs in Space: Popular Nearest Neighbors in High-Dimensional Data made the case that hubness (where an item shows up in the neighbor list of other items at much more than expected) can affect many aspects of machine learning including some related to dimensionality reduction, such as clustering. When they consider dimensionality reduction, they conclude:
If one still chooses to apply aggressive dimensionality reduction and let the resulting number of features fall below intrinsic dimensionality, it can be expected of […] hubness to be reduced or even lost. Whether these effects should be actively avoided or sought really depends on […] whether and to what degree the original pairwise distances represent valuable information
A dataset with a very high hubness is the macosko2015 scRNAseq dataset, which does not look very good under typical dimensionality reduction cases. After PCA to 50 dimensions, though, it does look a lot more clustered. And indeed the hubness is massively reduced after PCA. Tentatively, I would like to present this as evidence of hubness hurting dimensionality reduction. The difference seems to be due to the nearest neighbor graph that results after PCA, but not the weights of those edges.
Let’s look at macosko2015 without PCA being applied, using very default t-SNE settings. No legend shown because the identification of clusters isn’t important for what we are going to do.
time: 4.19 s (started: 2022-09-30 14:37:07 -07:00)
[14:37:13] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:186
[14:37:15] INFO Precomputed affinities provided. Ignoring perplexity-related parameters. tsne.py:1321

time: 1min 1s (started: 2022-09-30 14:37:11 -07:00)
This looks ok, but you can see a the large cyan cluster is swallowing a lot of the blue and green points. Now the version where the data was pre-processed by using the first 50 components:
[14:38:13] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:186
[14:38:14] INFO Precomputed affinities provided. Ignoring perplexity-related parameters. tsne.py:1321

time: 1min 1s (started: 2022-09-30 14:38:13 -07:00)
This just looks a lot better. I leave the question of whether it represents biological reality to one side, it’s just hard to argue that the non-PCA version shows better clustering. What makes the difference? I can think of a few possibilities:
macosko2015 and macosko2015-pca have very different hubness values and k-occurrence distributions.So we should try to rule out 2 or 3. Below I will show strong evidence that the source of the difference is the nearest neighbor graph (2), but that the edge weights (3) have basically no affect.
Effectively, the case for point 3 has been made already in Attraction-Repulsion Spectrum in Neighbor Embeddings. There (in figure 2 to be precise) the authors note that running t-SNE on MNIST with uniform affinities and 15 nearest neighbors gives results which look just like the usual perplexity-based result with perplexity = 30. I have no reason to doubt them, but let’s just check we can get that via the affordances provided by drnb.
[14:39:15] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:186
[14:39:18] INFO Precomputed affinities provided. Ignoring perplexity-related parameters. tsne.py:1321

time: 1min 37s (started: 2022-09-30 14:39:14 -07:00)
time: 1.58 ms (started: 2022-09-30 14:40:51 -07:00)
[14:40:52] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:1175

time: 52.6 s (started: 2022-09-30 14:40:51 -07:00)
Yeah, that’s pretty good. The 4/7/9 clusters at the top shift a bit, but that’s par for the course. So that’s the baseline sorted. At least in this case, affinity doesn’t seem to matter. Unfortunately, I don’t know a general rule of thumb for how to translate from perplexity to number of neighbors (divide by two?) so I will stick with perplexity=30, n_neighbors=15 for the rest of this discussion.
Let’s try using uniform affinities with the macosko2015 and macosko2015-pca50:
[14:41:45] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:1175

time: 26.3 s (started: 2022-09-30 14:41:44 -07:00)
[14:42:10] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:1175

time: 33.8 s (started: 2022-09-30 14:42:10 -07:00)
You will have to scroll up a bit, but yes they look like the previous results. Using uniform affinity does seem to give the same results as the perplexity based affinity. So that really looks like the difference between these two plots is down to PCA giving a different set of nearest neighbors and nothing to do with the distances per se.
To prove to myself that this is definitely correct, here I am going to manually build the t-SNE results in this notebook so I can see that no differences creep in elsewhere. Specifically I am going to:
macosko2015 datamacosko2015-pca50 distances being involved at any point.And here is some code to read some neighbors and then create the openTSNE.affinity objects:
import openTSNE.nearest_neighbors as tsnenn
import drnb.neighbors as nbnbrs
def get_nbrs(name, n_neighbors=15):
return nbnbrs.read_neighbors(
name,
n_neighbors=n_neighbors + 1,
exact=True,
)
def unif_aff(name, n_neighbors=15):
nbrs = get_nbrs(name, n_neighbors=n_neighbors)
knn = tsnenn.PrecomputedNeighbors(nbrs.idx[:, 1:], nbrs.dist[:, 1:])
return openTSNE.affinity.Uniform(knn_index=knn)
def perp_aff(name, perplexity=30):
nbrs = get_nbrs(name, n_neighbors=perplexity * 3)
knn = tsnenn.PrecomputedNeighbors(nbrs.idx[:, 1:], nbrs.dist[:, 1:])
return openTSNE.affinity.PerplexityBasedNN(knn_index=knn, perplexity=perplexity)time: 5.96 ms (started: 2022-09-30 14:42:48 -07:00)
I have abandoned the macosko2015 palette, and am using the ColorBrewer Set3 palette. I think it’s a bit easier to see the different categories. You may think otherwise. There just aren’t that many legible 12-color qualitative palettes, sorry.
this plucky little tsne object will be doing a lot of work for us.
This is just to show that all of the above gets us output that looks like what we had before.
[14:42:48] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:1175

time: 22.6 s (started: 2022-09-30 14:42:48 -07:00)
[14:43:11] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:186
[14:43:12] INFO Precomputed affinities provided. Ignoring perplexity-related parameters. tsne.py:1321

time: 55.1 s (started: 2022-09-30 14:43:11 -07:00)
[14:44:06] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:1175

time: 33.6 s (started: 2022-09-30 14:44:06 -07:00)
[14:44:40] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:186
[14:44:41] INFO Precomputed affinities provided. Ignoring perplexity-related parameters. tsne.py:1321

time: 1min (started: 2022-09-30 14:44:39 -07:00)
Alright, that was a lot of code but at least we know it’s all working like it should be.
The main new thing we need to do here is:
pynndescent.from numba import jit, prange
from pynndescent.distances import euclidean
@jit(nopython=True, parallel=True)
def idx_to_dist(idx, data):
n_items = idx.shape[0]
n_neighbors = idx.shape[1]
dist = np.empty(dtype=np.float32, shape=idx.shape)
for i in prange(n_items):
for j in range(n_neighbors):
dist[i, j] = euclidean(data[i], data[idx[i, j]])
return disttime: 9.36 ms (started: 2022-09-30 14:45:40 -07:00)
The following functions are like the unif_aff and perp_aff functions above, but use the PCA data to get new nearest neighbor data.
def unif_aff_pca_donor(name, data, n_neighbors=15):
nbrs = get_nbrs(name, n_neighbors=n_neighbors)
nbrs_pca = get_nbrs(f"{name}-pca50", n_neighbors=n_neighbors)
nbr_dist = idx_to_dist(nbrs_pca.idx, data)
knn = tsnenn.PrecomputedNeighbors(nbrs_pca.idx[:, 1:], nbr_dist[:, 1:])
return openTSNE.affinity.Uniform(knn_index=knn)
def perp_aff_pca_donor(name, data, perplexity=30):
nbrs_orig = get_nbrs(name, n_neighbors=perplexity * 3)
nbrs_pca = get_nbrs(f"{name}-pca50", n_neighbors=perplexity * 3)
nbr_dist = idx_to_dist(nbrs_pca.idx, data)
knn = tsnenn.PrecomputedNeighbors(nbrs_pca.idx[:, 1:], nbr_dist[:, 1:])
return openTSNE.affinity.PerplexityBasedNN(knn_index=knn, perplexity=perplexity)time: 4.94 ms (started: 2022-09-30 14:48:51 -07:00)
We are now ready to see this all in action.
[14:49:06] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:1175

time: 32.7 s (started: 2022-09-30 14:49:05 -07:00)
[14:49:48] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:186
[14:49:49] INFO Precomputed affinities provided. Ignoring perplexity-related parameters. tsne.py:1321

time: 58.3 s (started: 2022-09-30 14:49:44 -07:00)
So there it is. We can get results in the non-PCA macosko2015 dataset that look as good as the PCA version. The only thing we need from the PCA results is the choice of the nearest neighbor indices. The distances are irrelevant.
So what remains to understand is why in this case the PCA neighbor indices are better. Is it because the hubness is drastically reduced? Is it because of the way PCA with 50 dimensions works on the distances in the original ambient case and the reduction of hubness is just a side effect?
time: 1.85 ms (started: 2022-09-30 14:50:57 -07:00)
[14:51:01] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:186
[14:51:05] INFO Precomputed affinities provided. Ignoring perplexity-related parameters. tsne.py:1321

time: 2min 7s (started: 2022-09-30 14:50:58 -07:00)
[14:53:06] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:186
[14:53:10] INFO Precomputed affinities provided. Ignoring perplexity-related parameters. tsne.py:1321

time: 2min 4s (started: 2022-09-30 14:53:06 -07:00)
[14:55:10] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:1175

time: 9.77 s (started: 2022-09-30 14:55:10 -07:00)
[14:55:20] INFO KNN index provided. Ignoring KNN-related parameters. affinity.py:1175

time: 9.64 s (started: 2022-09-30 14:55:20 -07:00)
The answer would seem to be “no”.