KNRScore is a Python package for computing K-Nearest-Rank Similarity, a metric that quantifies local structural similarity between two maps or embeddings.
Project description
⭐️ Star this repo if you like it ⭐️
KNRscore - K-Nearest-Rank Similarity
Medium Blog
Also checkout The Similarity between t-SNE, UMAP, PCA, and Other Mappings to get a structured overview and usage of KNRscore.
Documentation pages
On the documentation pages you can find detailed information about the working of the KNRscore with examples.
Method
To compare the embedding of samples in two different maps, we propose a scale dependent similarity measure. For a pair of maps X and Y, we compare the sets of the, respectively, kx and ky nearest neighbours of each sample. We first define the variable rxij as the rank of the distance of sample j among all samples with respect to sample i, in map X. The nearest neighbor of sample i will have rank 1, the second nearest neighbor rank 2, etc. Analogously, ryij is the rank of sample j with respect to sample i in map Y. Now we define a score on the interval [0, 1], as (eq. 1)
Schematic overview
Schematic overview to systematically compare local and global differences between two sample projections. For illustration we compare two input maps (x and y) in which each map contains n samples (step 1). The second step is the ranking of samples based on Euclidean distance. The ranks of map x are subsequently compared to the ranks of map y for kx and ky nearest neighbours (step 3). The overlap between ranks (step 4), is subsequently summarized in Score: Sx,y(kx,ky).
Install KNRscore from PyPI
pip install KNRscore
Import KNRscore package
import KNRscore as knrs
Functions in KNRscore
import KNRscore as knrs
scores = knrs.compare(map1, map2)
fig = knrs.plot(scores)
fig = knrs.scatter(Xcoord,Ycoord)
Example
# Imort library
import KNRscore as knrs
# Load data
X, y = KNRscore.import_example()
# Compute embeddings
embed_pca = decomposition.TruncatedSVD(n_components=50).fit_transform(X)
embed_tsne = manifold.TSNE(n_components=2, init='pca').fit_transform(X)
# Compare PCA vs. tSNE
scores = knrs.compare(embed_pca, embed_tsne, n_steps=25)
# plot PCA vs. tSNE
fig, ax = knrs.plot(scores, xlabel='PCA', ylabel='tSNE')
fig, ax = knrs.scatter(embed_tsne[:, 0], embed_tsne[:, 1], labels=y, cmap='Set1', title='tSNE Scatter Plot')
fig, ax = knrs.scatter(embed_pca[:, 0], embed_pca[:, 1], labels=y, cmap='Set1', title='PCA Scatter Plot')
Examples figures
References
- Taskesen, E. et al. Pan-cancer subtyping in a 2D-map shows substructures that are driven by specific combinations of molecular characteristics. Sci. Rep. 6, 24949
- https://static-content.springer.com/esm/art%3A10.1038%2Fsrep24949/MediaObjects/41598_2016_BFsrep24949_MOESM12_ESM.pdf
- https://www.nature.com/articles/srep24949
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file knrscore-2.0.0.tar.gz.
File metadata
- Download URL: knrscore-2.0.0.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db11e18a4fd81be72e54fe3c5a8a93fa5fa23c36f6c825ed5c49d883d6401798
|
|
| MD5 |
0767dd6463ddf9f80c4ba2c0b7c78164
|
|
| BLAKE2b-256 |
db45a270dde38771ec2e0074ab29bafd8f5f575e23cc0035c3507756be21511f
|
File details
Details for the file knrscore-2.0.0-py3-none-any.whl.
File metadata
- Download URL: knrscore-2.0.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2bf899a4835d3f23a5c46fb931dd3527b65535ceefab180912ac16e38e7d62dc
|
|
| MD5 |
fff25ebd823358bc9c67e90835c89a3e
|
|
| BLAKE2b-256 |
12816d2c8f28790402d4041c4927c3696273329d67b01da8df3a5e1d054d19ce
|