Skip to main content

Build Status License: MIT Supported Python version Stable Version

Fair K-Means

Fair K-Means produces a fair clustering assignment according to the fairness definition of Chierichetti et al. [1]. Each point has a binary color assigned to it. The goal is to assign the points to clusters such that the number of points with different colors in each cluster is the same. The algorithm also works with weights, so each point can participate with a different weight in the coloring.

The algorithm works as follows, assuming that the binary colors are red and blue:

  1. A matching between the red and blue points is computed such that the cost (the point distances) of the matching is minimized.
  2. The mean of each matched pair is computed.
  3. A K-Means++ clustering of all the means is computed, and the point pairs are assigned to the clusters of their means.

The matching between the red and blue points is computed using the Lemon C++ Library. The library is included in the package and does not need to be installed separately. Only the needed files were included, and a complete version of the library can be found here. A copyright notice is included here.

You can try Fair K-Means out on our Clustering Toolkit!

References

[1] Flavio Chierichetti, Ravi Kumar, Silvio Lattanzi, and Sergei Vassilvitskii, Fair clustering through fairlets, Proceedings of the 30th Annual Conference on Neural Information Processing Systems (NIPS), 2017, pp. 5036–5044.

Installation

pip install fair-kmeans

Example

from fair_kmeans import FairKMeans

example_data = [
    [1.0, 1.0, 1.0],
    [1.1, 1.1, 1.1],
    [1.2, 1.2, 1.2],
    [2.0, 2.0, 2.0],
    [2.1, 2.1, 2.1],
    [2.2, 2.2, 2.2],
]

example_colors = [1, 1, 1, 0, 0, 0]

km = FairKMeans(n_clusters=2, random_state=0)
km.fit(example_data, color=example_colors)
labels = km.labels_
centers = km.cluster_centers_

print(labels) # [1, 0, 0, 1, 0, 0]
print(centers) # [[1.65, 1.65, 1.65], [1.5, 1.5, 1.5]]

Example with Weights

from fair_kmeans import FairKMeans

example_data = [
    [1.0, 1.0, 1.0],
    [1.1, 1.1, 1.1],
    [1.2, 1.2, 1.2],
    [2.0, 2.0, 2.0],
    [2.1, 2.1, 2.1],
    [2.2, 2.2, 2.2],
]

example_colors = [1, 1, 1, 0, 0, 0]
example_weights = [2, 2, 1, 1, 1, 3]

km = FairKMeans(n_clusters=2, random_state=0)
km.fit(example_data, color=example_colors, sample_weight=example_weights)
labels = km.labels_
centers = km.cluster_centers_

print(labels) # [1, 1, 0, 1, 1, 0]
print(centers) # [[0.85, 0.85, 0.85], [1.28, 1.28, 1.28]]

Development

Install poetry

curl -sSL https://install.python-poetry.org | python3 -

Install clang

sudo apt-get install clang

Set clang variables

export CXX=/usr/bin/clang++
export CC=/usr/bin/clang

Install the package

poetry install

If the installation does not work and you do not see the C++ output, you can build the package to see the stack trace

poetry build

Run the tests

poetry run python -m unittest discover tests -v

Citation

If you use this code, please cite the following paper:

M. Schmidt, C. Schwiegelshohn, and C. Sohler, "Fair Coresets and Streaming Algorithms for Fair k-means," in Lecture notes in computer science, 2020, pp. 232–251. doi: 10.1007/978-3-030-39479-0_16.

Metadata

Release files for fair-kmeans 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fair-kmeans 0.1.4
File Size Uploaded Provenance Actions
fair_kmeans-0.1.4.tar.gz 79.1 kB Without provenance Details for fair_kmeans-0.1.4.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for fair-kmeans 0.1.4
File Actions
fair_kmeans-0.1.4-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.15 CPython 3.15 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for fair_kmeans-0.1.4-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
fair_kmeans-0.1.4-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details for fair_kmeans-0.1.4-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
fair_kmeans-0.1.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for fair_kmeans-0.1.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
fair_kmeans-0.1.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for fair_kmeans-0.1.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Total release size: 2.6 MB

Release files / fair_kmeans-0.1.4.tar.gz

Download URL fair_kmeans-0.1.4.tar.gz
Size 79.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e1d186111fe8a5d292246d18e61e414b88c0d3be8cfb75a7a7cf1b189236a3c9
BLAKE2b-256 checksum
How to use checksums
39b8535015eaa135feed6f2e861d6ca2f5c82461c2d6bc1e227eebe1711ea81b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / fair_kmeans-0.1.4-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL fair_kmeans-0.1.4-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 630.0 kB
Tags CPython 3.15 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
318a654763967dbed0b0e88c801d9dbe76b7a8ac3af2ffc63b9fe9ee96d5d20c
BLAKE2b-256 checksum
How to use checksums
156160df37eba6874f565e1c8dec19da53d67823462414888340ba8c5e9729f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / fair_kmeans-0.1.4-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL fair_kmeans-0.1.4-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 630.1 kB
Tags CPython 3.14 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
ce1fe6e820aedbe6eec9ab34c97eec2a90a30ca9524d9e7d6c73316db9d4bc79
BLAKE2b-256 checksum
How to use checksums
ff8eb7011089819df96055f96dc46690ae8875f38a4e03d2af2ed3fa2e37663b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / fair_kmeans-0.1.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL fair_kmeans-0.1.4-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 630.1 kB
Tags CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
cef2cab01c56fea7c94a0de70397f439f2d6b3e8993761309967910979c007ee
BLAKE2b-256 checksum
How to use checksums
c048f17225169c1230956070de75610f4ef025337a2e1dedd1ca1c76bd557525
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / fair_kmeans-0.1.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL fair_kmeans-0.1.4-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 630.2 kB
Tags CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
05f217cacb66617001a2b0759782f300de7f4b48ddd2deff7ff3b627891a304d
BLAKE2b-256 checksum
How to use checksums
cb9372b9506a5abc6ab7f1cc965bca12d4036ffe9424e668661ed0ea07311a89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for fair-kmeans 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page