Clustering based on density with variable density clusters
Project description
HDBSCAN
HDBSCAN - Hierarchical Density-Based Spatial Clustering of Applications with Noise. Performs DBSCAN over varying epsilon values and integrates the result to find a clustering that gives the best stability over epsilon. This allows HDBSCAN to find clusters of varying densities (unlike DBSCAN), and be more robust to parameter selection.
In practice this means that HDBSCAN returns a good clustering straight away with little or no parameter tuning – and the primary parameter, minimum cluster size, is intuitive and easy to select.
HDBSCAN is ideal for exploratory data analysis; it’s a fast and robust algorithm that you can trust to return meaningful clusters (if there are any).
Based on the papers:
McInnes L, Healy J. Accelerated Hierarchical Density Based Clustering In: 2017 IEEE International Conference on Data Mining Workshops (ICDMW), IEEE, pp 33-42. 2017 [pdf]
R. Campello, D. Moulavi, and J. Sander, Density-Based Clustering Based on Hierarchical Density Estimates In: Advances in Knowledge Discovery and Data Mining, Springer, pp 160-172. 2013
Documentation, including tutorials, are available on ReadTheDocs at http://hdbscan.readthedocs.io/en/latest/ .
Notebooks comparing HDBSCAN to other clustering algorithms, explaining how HDBSCAN works and comparing performance with other python clustering implementations are available.
How to use HDBSCAN
The hdbscan package inherits from sklearn classes, and thus drops in neatly next to other sklearn clusterers with an identical calling API. Similarly it supports input in a variety of formats: an array (or pandas dataframe, or sparse matrix) of shape (num_samples x num_features); an array (or sparse matrix) giving a distance matrix between samples.
import hdbscan
from sklearn.datasets import make_blobs
data, _ = make_blobs(1000)
clusterer = hdbscan.HDBSCAN(min_cluster_size=10)
cluster_labels = clusterer.fit_predict(data)
Performance
Significant effort has been put into making the hdbscan implementation as fast as possible. It is orders of magnitude faster than the reference implementation in Java, and is currently faster than highly optimized single linkage implementations in C and C++. version 0.7 performance can be seen in this notebook . In particular performance on low dimensional data is better than sklearn’s DBSCAN , and via support for caching with joblib, re-clustering with different parameters can be almost free.
Additional functionality
The hdbscan package comes equipped with visualization tools to help you understand your clustering results. After fitting data the clusterer object has attributes for:
The condensed cluster hierarchy
The robust single linkage cluster hierarchy
The reachability distance minimal spanning tree
All of which come equipped with methods for plotting and converting to Pandas or NetworkX for further analysis. See the notebook on how HDBSCAN works for examples and further details.
The clusterer objects also have an attribute providing cluster membership strengths, resulting in optional soft clustering (and no further compute expense). Finally each cluster also receives a persistence score giving the stability of the cluster over the range of distance scales present in the data. This provides a measure of the relative strength of clusters.
Outlier Detection
The HDBSCAN clusterer objects also support the GLOSH outlier detection algorithm. After fitting the clusterer to data the outlier scores can be accessed via the outlier_scores_ attribute. The result is a vector of score values, one for each data point that was fit. Higher scores represent more outlier like objects. Selecting outliers via upper quantiles is often a good approach.
- Based on the paper:
R.J.G.B. Campello, D. Moulavi, A. Zimek and J. Sander Hierarchical Density Estimates for Data Clustering, Visualization, and Outlier Detection, ACM Trans. on Knowledge Discovery from Data, Vol 10, 1 (July 2015), 1-51.
Robust single linkage
The hdbscan package also provides support for the robust single linkage clustering algorithm of Chaudhuri and Dasgupta. As with the HDBSCAN implementation this is a high performance version of the algorithm outperforming scipy’s standard single linkage implementation. The robust single linkage hierarchy is available as an attribute of the robust single linkage clusterer, again with the ability to plot or export the hierarchy, and to extract flat clusterings at a given cut level and gamma value.
Example usage:
import hdbscan
from sklearn.datasets import make_blobs
data, _ = make_blobs(1000)
clusterer = hdbscan.RobustSingleLinkage(cut=0.125, k=7)
cluster_labels = clusterer.fit_predict(data)
hierarchy = clusterer.cluster_hierarchy_
alt_labels = hierarchy.get_clusters(0.100, 5)
hierarchy.plot()
- Based on the paper:
K. Chaudhuri and S. Dasgupta. “Rates of convergence for the cluster tree.” In Advances in Neural Information Processing Systems, 2010.
Branch detection
The hdbscan package supports a branch-detection post-processing step by Bot et al.. Cluster shapes, such as branching structures, can reveal interesting patterns that are not expressed in density-based cluster hierarchies. The BranchDetector class mimics the HDBSCAN API and can be used to detect branching hierarchies in clusters. It provides condensed branch hierarchies, branch persistences, and branch memberships and supports joblib’s caching functionality. A notebook demonstrating the BranchDetector is available.
Example usage:
import hdbscan
from sklearn.datasets import make_blobs
data, _ = make_blobs(1000)
clusterer = hdbscan.HDBSCAN(branch_detection_data=True).fit(data)
branch_detector = hdbscan.BranchDetector().fit(clusterer)
branch_detector.cluster_approximation_graph_.plot(edge_width=0.1)
- Based on the paper:
D. M. Bot, J. Peeters, J. Liesenborgs and J. Aerts “FLASC: A Flare-Sensitive Clustering Algorithm: Extending HDBSCAN* for Detecting Branches in Clusters” Arxiv 2311.15887, 2023.
Installing
Easiest install, if you have Anaconda (thanks to conda-forge which is awesome!):
conda install -c conda-forge hdbscan
PyPI install, presuming you have an up to date pip:
pip install hdbscan
Binary wheels for a number of platforms are available thanks to the work of Ryan Helinski <rlhelinski@gmail.com>.
If pip is having difficulties pulling the dependencies then we’d suggest to first upgrade pip to at least version 10 and try again:
pip install --upgrade pip
pip install hdbscan
Otherwise install the dependencies manually using anaconda followed by pulling hdbscan from pip:
conda install cython
conda install numpy scipy
conda install scikit-learn
pip install hdbscan
For a manual install of the latest code directly from GitHub:
pip install --upgrade git+https://github.com/scikit-learn-contrib/hdbscan.git#egg=hdbscan
Alternatively download the package, install requirements, and manually run the installer:
wget https://github.com/scikit-learn-contrib/hdbscan/archive/master.zip
unzip master.zip
rm master.zip
cd hdbscan-master
pip install -r requirements.txt
python setup.py install
Running the Tests
The package tests can be run after installation using the command:
nosetests -s hdbscan
or, if nose is installed but nosetests is not in your PATH variable:
python -m nose -s hdbscan
If one or more of the tests fail, please report a bug at https://github.com/scikit-learn-contrib/hdbscan/issues/new
Python Version
The hdbscan library supports both Python 2 and Python 3. However we recommend Python 3 as the better option if it is available to you.
Help and Support
For simple issues you can consult the FAQ in the documentation. If your issue is not suitably resolved there, please check the issues on github. Finally, if no solution is available there feel free to open an issue ; the authors will attempt to respond in a reasonably timely fashion.
Contributing
We welcome contributions in any form! Assistance with documentation, particularly expanding tutorials, is always welcome. To contribute please fork the project make your changes and submit a pull request. We will do our best to work through any issues with you and get your code merged into the main branch.
Citing
If you have used this codebase in a scientific publication and wish to cite it, please use the Journal of Open Source Software article.
L. McInnes, J. Healy, S. Astels, hdbscan: Hierarchical density based clustering In: Journal of Open Source Software, The Open Journal, volume 2, number 11. 2017
@article{mcinnes2017hdbscan,
title={hdbscan: Hierarchical density based clustering},
author={McInnes, Leland and Healy, John and Astels, Steve},
journal={The Journal of Open Source Software},
volume={2},
number={11},
pages={205},
year={2017}
}
To reference the high performance algorithm developed in this library please cite our paper in ICDMW 2017 proceedings.
McInnes L, Healy J. Accelerated Hierarchical Density Based Clustering In: 2017 IEEE International Conference on Data Mining Workshops (ICDMW), IEEE, pp 33-42. 2017
@inproceedings{mcinnes2017accelerated,
title={Accelerated Hierarchical Density Based Clustering},
author={McInnes, Leland and Healy, John},
booktitle={Data Mining Workshops (ICDMW), 2017 IEEE International Conference on},
pages={33--42},
year={2017},
organization={IEEE}
}
If you used the branch-detection functionality in this codebase in a scientific publication and which to cite it, please use the Arxiv preprint:
D. M. Bot, J. Peeters, J. Liesenborgs and J. Aerts “FLASC: A Flare-Sensitive Clustering Algorithm: Extending HDBSCAN* for Detecting Branches in Clusters” Arxiv 2311.15887, 2023.
@misc{bot2023flasc,
title={FLASC: A Flare-Sensitive Clustering Algorithm: Extending HDBSCAN* for Detecting Branches in Clusters},
author={D. M. Bot and J. Peeters and J. Liesenborgs and J. Aerts},
year={2023},
eprint={2311.15887},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2311.15887},
}
Licensing
The hdbscan package is 3-clause BSD licensed. Enjoy.
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 Distributions
Hashes for hdbscan-0.8.39-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36afdbdef30ad4cbccc730ca0f0d76837b533746db24c17cd661a8f01ff9c849 |
|
MD5 | 97a50ee23e64ec98bba053e0d005abea |
|
BLAKE2b-256 | 66f75be1c9351a70166054910163e990f737dbe376d422b5811b45e958b1c680 |
Hashes for hdbscan-0.8.39-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7809a21842f9524a5160176181079380ba47f54f438ec6c48101801e0a84712 |
|
MD5 | cb2e684efb86b1ca2e32ef58c0cf6698 |
|
BLAKE2b-256 | 9bb2736f502dc447e52aa51d3e8991d7d342ce3a4f4d09226015eb14e4a1dcad |
Hashes for hdbscan-0.8.39-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1ce6cfe384b637b9451d003d0ddad75cfb4851ef743f29d794249721c2f1158 |
|
MD5 | a384186449e346d812d6333c41aa7ae9 |
|
BLAKE2b-256 | 15aed74752edea5ca8fbbe0058411b092e2c872bf74cb4a94f35ba05946ff464 |
Hashes for hdbscan-0.8.39-cp311-cp311-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68880e33f8dc2a12b07f267dcb657932737e6863f9be8c13299c9ad7c50b9c41 |
|
MD5 | a5e721793d46cdcf1da85b1f0d064ca5 |
|
BLAKE2b-256 | f9425a092661e98682fd2dd7b7afde1e5264f56b0882e0893334bf5f3eb01f32 |
Hashes for hdbscan-0.8.39-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2e6bfb9e03b3689d93fd966d1037781339da5352653d8cf109ad2c9229be9f2 |
|
MD5 | 778b048026b97b4d84a70944464146c9 |
|
BLAKE2b-256 | 372cc8807834821e4537c6a0a54eb927729e5b1a1054d1940af862991b1bcb2c |
Hashes for hdbscan-0.8.39-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17092f3dc50e4da19010b61b7ecf87aca2980e76d83a4826614bba2b5c45f89e |
|
MD5 | 36dae74da46e83e608225332a5b45a3e |
|
BLAKE2b-256 | 53f019fccde88302e2ba8bbf66b89b09d5fe0d94bc1c6760b3b9731d2c0573e5 |
Hashes for hdbscan-0.8.39-cp310-cp310-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ea1f9867f54501f99f8323df1086881a13e5ad135216e23cf8cb170f3f159a9 |
|
MD5 | 8b65e8e9ae068223cba065d3ffa61235 |
|
BLAKE2b-256 | b903096b083b106559277998fe4f0e6bb9db4c6323d91122e34c73cfc0009e1e |
Hashes for hdbscan-0.8.39-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f782c749e1040f737e94c8c2f711135146b2d00753858acd13452dafb6add0b6 |
|
MD5 | 17e60ac5a683d1177ecf29f1ae72ed66 |
|
BLAKE2b-256 | 0a494881cc990ec74a691d1617eb5d1f41359489d9e4ee6bc7992162e30c2ce3 |
Hashes for hdbscan-0.8.39-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ec50875227d31995de80a2bdee98e541177856862897cb60357410c3f6a9ed9 |
|
MD5 | 7997c674ae93c489b15820f2a8de1d05 |
|
BLAKE2b-256 | 2fbe4be54b641246faf8fa28e9c37739ab3947c29f6bd81c95e93d7d0bb179d9 |
Hashes for hdbscan-0.8.39-cp39-cp39-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 127cd877bf0a816d3ea75478d9774f4205fdfe60d84bc7639be501f071d4c558 |
|
MD5 | bd93d3fba52e25ab5b69dfadebafaef0 |
|
BLAKE2b-256 | 3835d070cda55331dbd52b8bdd471f3e88530a591387f1930a17ae653dc735ce |
Hashes for hdbscan-0.8.39-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdef592010599503d6197e8420e6ad4bccfdaaf49f46722da34a1b62026455db |
|
MD5 | 07c65f502c823b528e1a5e262abb7144 |
|
BLAKE2b-256 | e99aead67b029b3bca1732cc3a75ee17471a7130b9f7a28ccc5ccc93b810da7e |
Hashes for hdbscan-0.8.39-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7815a842008fcf224807f6cc79c48cda4f85f31a9a9918bb2cf5f8ecdb7bfe5b |
|
MD5 | bc0b02b2c77ac203bf2c98753fa37220 |
|
BLAKE2b-256 | 1eec0ca014e87131af4cf7293f3915f131764ec6ededf49a90ef304cb78ed0ac |
Hashes for hdbscan-0.8.39-cp38-cp38-macosx_12_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34595a7e77799dbc768f6e35691657d3c375a4f262d74515f46c7cc15294ea60 |
|
MD5 | 963231b47b7bc826db9706a310334edf |
|
BLAKE2b-256 | 4b03b6898c54b57f313db1b60be066bfd7b93235853f9d0aeba203c30ad2126e |