Hartigan K-Means
Project description
Hartigan's K-Means
Scope
This project provides an efficient implementation of Hartigan’s method for k-means clustering (Hartigan 1975). It builds on the work of Slonim, Aharoni and Crammer (2013), which introduced a significant improvement to the algorithm computational complexity, and adds an additional optimization for inputs in sparse vector representation. The project is packaged as a python library with a cython-wrapped C++ extension for the partition optimization code. A pure python implementation is included as well.
Installation
pip install hartigan-kmeans
Usage
The main class in this library is HKmeans
, which implements the clustering interface of SciKit Learn, providing methods such as fit()
, fit_transform()
, fit_predict()
, etc.
The sample code below clusters the 18.8K documents of the 20-News-Groups dataset into 20 clusters:
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.datasets import fetch_20newsgroups
from sklearn import metrics
from hkmeans import HKmeans
# read the dataset
dataset = fetch_20newsgroups(subset='all', categories=None,
shuffle=True, random_state=256)
gold_labels = dataset.target
n_clusters = np.unique(gold_labels).shape[0]
# create count vectors using the 10K most frequent words
vectorizer = TfidfVectorizer(max_features=10000)
X = vectorizer.fit_transform(dataset.data)
# HKMeans initialization and clustering; parameters:
# perform 10 random initializations (n_init=10); the best one is returned.
# up to 15 optimization iterations in each initialization (max_iter=15)
# use all cores in the running machine for parallel execution (n_jobs=-1)
hkmeans = HKMeans(n_clusters=n_clusters, random_state=128, n_init=10,
n_jobs=-1, max_iter=15, verbose=True)
hkmeans.fit(X)
# report standard clustering metrics
print("Homogeneity: %0.3f" % metrics.homogeneity_score(gold_labels, hkmeans.labels_))
print("Completeness: %0.3f" % metrics.completeness_score(gold_labels, hkmeans.labels_))
print("V-measure: %0.3f" % metrics.v_measure_score(gold_labels, hkmeans.labels_))
print("Adjusted Rand-Index: %.3f" % metrics.adjusted_rand_score(gold_labels, hkmeans.labels_))
Expected result:
Homogeneity:
Completeness:
V-measure:
Adjusted Rand-Index:
See the Examples directory for more illustrations and a comparison against Lloyd's K-Means.
License
Copyright IBM Corporation 2022
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
If you would like to see the detailed LICENSE click here.
Authors
- Algorithm: Hartigan 1975
- Pseudo-code and optimization: Slonim, Aharoni and Crammer (2013)
- Programming, optimization and maintenance: Assaf Toledo
If you have any questions or issues you can create a new issue here.
References
- Hartigan, John A. Clustering algorithms. Wiley series in probability and mathematical statistics: Applied probability and statistics. John Wiley & Sons, Inc., 1975.
- Slonim, Noam, Ehud Aharoni, and Koby Crammer. "Hartigan's K-Means Versus Lloyd's K-Means—Is It Time for a Change?." Twenty-Third International Joint Conference on Artificial Intelligence. 2013.
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 hartigan_kmeans-0.0.5-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02f6746409fc0880fc4303fe2b1ec77c68df51386d5cfc11c9537eac417241e7 |
|
MD5 | d939179c154ad1ed4a262b82e787821d |
|
BLAKE2b-256 | b5aeb869e0bc97baad944bd4c721e0aa7f46df6b227e3beb0df30ba1994cd3b3 |
Hashes for hartigan_kmeans-0.0.5-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c32c3e954c2d9b9bfba998a4fb6c6181d5740b463e13272aea1679f0bc0597fd |
|
MD5 | 4ce3c5b3c4ecf0f8842822f831254074 |
|
BLAKE2b-256 | f26bfd9da4d5dde0341b142ace5a4ec2ff5e726a7ba97d28cb579e5150734127 |
Hashes for hartigan_kmeans-0.0.5-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd0d6c70f4afeddb48d5b81857e6b60146834610272face3baca11705dfedb4d |
|
MD5 | d6be048e4cadfa7c1d456466485f33ab |
|
BLAKE2b-256 | 70c6abbcb8b2acf58022a5e454e34a15de1cdba8d0a67994e79b44b51c815d5b |
Hashes for hartigan_kmeans-0.0.5-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a78d86bf530a10293131202b88b119de49ff6a66593eb40f79d3eb1e6119aa34 |
|
MD5 | 692f5f73f9f4f8a2c18a3c4bb672b3d7 |
|
BLAKE2b-256 | 820be85e3fdf78b59276123ee4cc782334a002556954fdca284514e61c57d637 |
Hashes for hartigan_kmeans-0.0.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7750fd985b968a91f847eaccf59c0eaff0fa7c8a7fce2345e646e2e669e66689 |
|
MD5 | d300c594148a9979108b4bdab887fda5 |
|
BLAKE2b-256 | fd911f14a4b8bd92d237706b56e52d021f1e7555d22c4a5308fd93b11ae56490 |
Hashes for hartigan_kmeans-0.0.5-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55eaa901c8032141081977b03178937743610e3a62f7f8944f554d732e0259f1 |
|
MD5 | e46b8785c3486bcdc2e111f581b10a1c |
|
BLAKE2b-256 | df925e7f9f26963c61925e78b8184ad14b44b821fe6f3a6c434fe7f7a7dc821f |
Hashes for hartigan_kmeans-0.0.5-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36dcafafa645aaab1af85e59f17682b23aa256b4b3dc42de149b6d8dc09f3547 |
|
MD5 | 1f700bdf0c973c8e15e7a076965542cf |
|
BLAKE2b-256 | fb4e164c4d3fcc7f974e994370992b4b95f6f10831b623961c261a65fa4fde8c |
Hashes for hartigan_kmeans-0.0.5-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0db8a9b38e5d59d16923be48093346c8ad9fe6a83396864ef8a9fe9cd2f20bdb |
|
MD5 | 075b118e474bd1ccb61e13406ca0fd0d |
|
BLAKE2b-256 | 5b735cb8a59f762bd8afbafc36f5c2268170b6a1d9730797da75e2ae7a96b184 |
Hashes for hartigan_kmeans-0.0.5-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88befce8e7eadcb5cac6dbc91c50ebfd0953acc75695ae45fb8a4be941526890 |
|
MD5 | 7f1b078150d7f0b1503463033b2dad3b |
|
BLAKE2b-256 | 5aa8f0a23a621ae7214043a79cc9b35c24e967dd16012ca0ba3e704da5b6c05b |
Hashes for hartigan_kmeans-0.0.5-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2414be1a12794b0ef721e9caaf06692658b32e36d3db267d146c6666b8017cb2 |
|
MD5 | d9349debb873f7f92d56e1e0d3741c9d |
|
BLAKE2b-256 | 526ee9ba649f05de94a4c771790fe8957e591c0320cd9cd2b3dbba196921bcb7 |
Hashes for hartigan_kmeans-0.0.5-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4910bec5251e4adbbb80646c3b0604873b37a4ae88afe635ab7fd0ab19b20c0a |
|
MD5 | 2958d6d535998327106641f0dc9393a7 |
|
BLAKE2b-256 | 9ce1f5ac17f2af8d21781e31363ad819ca0cf60589212748baf2dd464b21d0ec |
Hashes for hartigan_kmeans-0.0.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9723164df36521dce236500fbfcb8260b41ac91f1ce8dcb2bca9d2cd41c830f |
|
MD5 | c6c3a334683a945151d1aa4b56ccbddb |
|
BLAKE2b-256 | 683f12c6fb916ba616342dd33155e6c6bd359ac79e63c568f2fa1b42ec55259e |
Hashes for hartigan_kmeans-0.0.5-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f15dc8cd6d804d9c6e5fb6e128ecb494848bf4d48e85745435912d1812a2892 |
|
MD5 | 3b4289abd65ee255e527d711b8acc87f |
|
BLAKE2b-256 | bacee0a3e30b354a590600d9d02ce8d15dc8d42ef73a6ae402d7664f568a900b |
Hashes for hartigan_kmeans-0.0.5-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25248dc37e5132281d9ecdaa04f4114a19f857dbfebfcdba03a8969d48dfd2b1 |
|
MD5 | 9ede77750832a26dab5b415a97a966e9 |
|
BLAKE2b-256 | 994cc162abc7e8f2ad7a407f57923a23115463a4616d7f2e104862704661a71c |
Hashes for hartigan_kmeans-0.0.5-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9faa00920d2dd2992839f608522d7a5a30484822e119f0b769786532848ab0fd |
|
MD5 | 5041c52f08a2f9362b20a01c1477309c |
|
BLAKE2b-256 | 9cded2c7285065df9069506f3fb287feaff5718b955cb019bc2f87d0c024cb42 |
Hashes for hartigan_kmeans-0.0.5-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9817adca5f265a244fdefcf8046ae302ab8efa88d28b45db18fb5de93ba69760 |
|
MD5 | a7484e8419bb6e14fe3b10b777e9bb9f |
|
BLAKE2b-256 | 8cbc62c01a0e916a765d422f9af06896b3ee2d9d7f00a6aad73a5d468bb7f406 |
Hashes for hartigan_kmeans-0.0.5-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67b78359ae89b3b9132daebca9a0706965b81dbc590d292b2c5795ab70cda836 |
|
MD5 | 99654566bffe1f3e51419a41499603c2 |
|
BLAKE2b-256 | a36248d2e6273faf9d74537a23e7617ea8c2e167e90ba21d05e15818f982459b |
Hashes for hartigan_kmeans-0.0.5-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f20d8acfad7e69c912b292f63b9ccc6a0a97ba600fb63dad2eb661f4c2fd6c5e |
|
MD5 | 624512ab45202199bc948b5bcc594d05 |
|
BLAKE2b-256 | 5681f859b8c95e2918ee1b5ff3674e9f7417d03c5699236078762725745d64b2 |
Hashes for hartigan_kmeans-0.0.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 086fda722c9f278052029534fe423c1dc961ef4854225dac1a0324491e422c07 |
|
MD5 | 9ca3f5ca8f77973246b98b3294e56bf8 |
|
BLAKE2b-256 | ad8e279a38b0eb788717b116317bdacc1480a5c46ea3cfadb9084731fdd84b9d |
Hashes for hartigan_kmeans-0.0.5-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7610cb5fd2430a9503844977452f02b5a211efdf13c5dcdb34853b7723b9d210 |
|
MD5 | 57d13c1c13af8076f75156b8617ebe96 |
|
BLAKE2b-256 | 7cecb743e28ff95b0b7bcbabf4b1f32d031be74b9b20cf724644284533ee8d3a |
Hashes for hartigan_kmeans-0.0.5-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ae9de48b110e2ee28befaf4d2fad273c4522f50443e0bf00e654622ba2b6d66 |
|
MD5 | cc29dfbe017cd8347225780230330f9a |
|
BLAKE2b-256 | 05c6c8a8df135c9e03d1a667e03cd6e4d54dcf7887bc2ab89ebb0ef5321c0ea3 |
Hashes for hartigan_kmeans-0.0.5-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 634cfd16e2b45cf8a49ca94cdd692277d6289aba4b666e00ef300a43d3b7ad0a |
|
MD5 | 542f3dccb9faeca297b3e1620c9a2786 |
|
BLAKE2b-256 | 246d1936c69cc1e1350d708db0f255b0a1ad235030120fa60d7a3cf08ed5481a |
Hashes for hartigan_kmeans-0.0.5-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e68f31a61cc6cf8f0c92e934c5e153798aa950de68108cd425dad1786bdb2faa |
|
MD5 | 7869037d8457f53fa44425c5debc287e |
|
BLAKE2b-256 | 699b9f9c16d702e82eed8b142a0cf57a9e5a4236524c348410964e552f440520 |
Hashes for hartigan_kmeans-0.0.5-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b02a40646e9df6455d435a22bc4c72606a8686c2e006ae0b8f88e1b16b120e7 |
|
MD5 | 5762eb7654fed7952e5598f52247d81c |
|
BLAKE2b-256 | 2a2774bfa10b8ba1379c3f0b7b2770f278ec8cd036071cc9b2ab102944234640 |
Hashes for hartigan_kmeans-0.0.5-cp37-cp37m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 313ce1c05a1d28c2288e0373f5768fb5eda1481951f28252ff4782f6c5ffcf0e |
|
MD5 | 2c3e8c12206267553e602c7efff78fba |
|
BLAKE2b-256 | 3de82b2e888b583ab1da01700188ce62bc17244932eec046c20c146986baca05 |
Hashes for hartigan_kmeans-0.0.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a95243d9611035b29cf4957e29feb629e5abd7947ad5422c0cc79c7454a877e |
|
MD5 | 512d1cf98d994c14ac4c1387b6271934 |
|
BLAKE2b-256 | 35e0b99e512b103ed0b7d4e9e7ecfe32ab28e74243e4dcd8ece8c37e80e0ac8c |
Hashes for hartigan_kmeans-0.0.5-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93da15939df18201e59b8855ae1d4f09c8efa320bfd631b9a185d79fa5afbc7a |
|
MD5 | 464ec70ac6393e1090155f5c672b6bcf |
|
BLAKE2b-256 | 1ea8469be84226610576ee00a3c13d8fb7a210244b743a459444fd833fb334b8 |
Hashes for hartigan_kmeans-0.0.5-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6449d9ab706562fde59a2f082dd994c028098e903e154e3a1265b479ed3c447b |
|
MD5 | 35ba29b03c06f17618e5d2db33db3c65 |
|
BLAKE2b-256 | 633e708e3602fc9dfa081d306a21e0a308be21d999da64416a1491f908da9502 |
Hashes for hartigan_kmeans-0.0.5-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 590feac66054dfcf0a2ff2b6a2bf896993262db17dcc7649abebe6a9b2b27425 |
|
MD5 | 4899977534f577dcb61d32c20fa7978e |
|
BLAKE2b-256 | 99b3c7d1a5cfd8f503421691e8b2a44d1ff4b162057352ee06d7b38325308409 |
Hashes for hartigan_kmeans-0.0.5-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12f3a367de29e4cab041f1d891f4eabdfa4e1d44d5ed811a3b56d79a759ad09d |
|
MD5 | 218518540b859e5a1c2d035e80b9862c |
|
BLAKE2b-256 | a7e42b7e836d829f1cd13bec795f1fe89496e48e22d0708e108177439e75a901 |
Hashes for hartigan_kmeans-0.0.5-cp36-cp36m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8a0e662bbd7c32d638ab932f9e7afe08b10f1bb4195497d11e9369c345607f0 |
|
MD5 | 265413033f420556e182e7983e597a4e |
|
BLAKE2b-256 | e2bc2f445499e092a3f6c5f5c8728dddbf8665ad404c5f2a2716bd109162a48c |
Hashes for hartigan_kmeans-0.0.5-cp36-cp36m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 470283c13e2d97779f14440f55b3098f860cf3604f2febdd79f17df17acf40c2 |
|
MD5 | 4af4e9fac413a81f9f2303de8280b4e9 |
|
BLAKE2b-256 | 54102b02cc77dfa61af84a6f5c412041f01570c3078f803d3fab47d2d081aa34 |
Hashes for hartigan_kmeans-0.0.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f14bdfbf8b137687c0a4fdae784296d5e5d2ae02e647648aafa083aa7642b4d |
|
MD5 | 013474332d7de773fe648d2719f03314 |
|
BLAKE2b-256 | 369f05953ea99093d75a13f079ed0044d3b55027bff004b7e13b1eb057905739 |
Hashes for hartigan_kmeans-0.0.5-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfac348479dce723f03de7b558238d42d82d4b096dba4d2978108f6ea85d3555 |
|
MD5 | 17e76e926c3f9996d02378dbf8ee2207 |
|
BLAKE2b-256 | 61c1d6fc998dbfef4b8b6bbc0268dd796d9e5b3d4a40674cd0b4c8d52cb55dcc |
Hashes for hartigan_kmeans-0.0.5-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abc9391f80606432a7c17fef1b1dd0dc88a35607c8a7ecfed25913acf90239fc |
|
MD5 | 352304f5726ef743a819759426817d0f |
|
BLAKE2b-256 | 89150b4b56eaf78c606e234a037921640e06756992bba1498a3d9f4937cad074 |