Skip to main content

Hartigan K-Means

Project description

Hartigan's K-Means

Build and upload to PyPI

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hartigan-kmeans-0.0.5.tar.gz (136.7 kB view hashes)

Uploaded Source

Built Distributions

hartigan_kmeans-0.0.5-cp310-cp310-win_amd64.whl (189.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

hartigan_kmeans-0.0.5-cp310-cp310-win32.whl (180.6 kB view hashes)

Uploaded CPython 3.10 Windows x86

hartigan_kmeans-0.0.5-cp310-cp310-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

hartigan_kmeans-0.0.5-cp310-cp310-musllinux_1_1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

hartigan_kmeans-0.0.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (533.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

hartigan_kmeans-0.0.5-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (523.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

hartigan_kmeans-0.0.5-cp310-cp310-macosx_10_9_x86_64.whl (200.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

hartigan_kmeans-0.0.5-cp39-cp39-win_amd64.whl (189.1 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

hartigan_kmeans-0.0.5-cp39-cp39-win32.whl (180.6 kB view hashes)

Uploaded CPython 3.9 Windows x86

hartigan_kmeans-0.0.5-cp39-cp39-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

hartigan_kmeans-0.0.5-cp39-cp39-musllinux_1_1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

hartigan_kmeans-0.0.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (532.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

hartigan_kmeans-0.0.5-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (523.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

hartigan_kmeans-0.0.5-cp39-cp39-macosx_10_9_x86_64.whl (200.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

hartigan_kmeans-0.0.5-cp38-cp38-win_amd64.whl (188.9 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

hartigan_kmeans-0.0.5-cp38-cp38-win32.whl (180.5 kB view hashes)

Uploaded CPython 3.8 Windows x86

hartigan_kmeans-0.0.5-cp38-cp38-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

hartigan_kmeans-0.0.5-cp38-cp38-musllinux_1_1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

hartigan_kmeans-0.0.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (534.4 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

hartigan_kmeans-0.0.5-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (525.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

hartigan_kmeans-0.0.5-cp38-cp38-macosx_10_9_x86_64.whl (198.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

hartigan_kmeans-0.0.5-cp37-cp37m-win_amd64.whl (188.1 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

hartigan_kmeans-0.0.5-cp37-cp37m-win32.whl (179.4 kB view hashes)

Uploaded CPython 3.7m Windows x86

hartigan_kmeans-0.0.5-cp37-cp37m-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

hartigan_kmeans-0.0.5-cp37-cp37m-musllinux_1_1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

hartigan_kmeans-0.0.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (509.1 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

hartigan_kmeans-0.0.5-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (499.4 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

hartigan_kmeans-0.0.5-cp37-cp37m-macosx_10_9_x86_64.whl (198.3 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

hartigan_kmeans-0.0.5-cp36-cp36m-win_amd64.whl (197.8 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

hartigan_kmeans-0.0.5-cp36-cp36m-win32.whl (185.4 kB view hashes)

Uploaded CPython 3.6m Windows x86

hartigan_kmeans-0.0.5-cp36-cp36m-musllinux_1_1_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

hartigan_kmeans-0.0.5-cp36-cp36m-musllinux_1_1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

hartigan_kmeans-0.0.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (509.3 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

hartigan_kmeans-0.0.5-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (499.5 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

hartigan_kmeans-0.0.5-cp36-cp36m-macosx_10_9_x86_64.whl (198.3 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page