Skip to main content

Fair K-Means produces a fair clustering assignment according to the fairness definition of Chierichetti et al. Each point has a binary color, and the goal is to assign the points to clusters such that the number of points with different colors in each cluster is the same and the cost of the clusters is minimized.

Project description

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.

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:

Melanie Schmidt, Chris Schwiegelshohn and Christian Sohler.
"Fair Coresets and Streaming Algorithms for Fair k-Means Clustering" (2020).
Approximation and Online Algorithms. WAOA 2019.
Lecture Notes in Computer Science, vol 11926. Springer.

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

fair_kmeans-0.1.1.tar.gz (80.0 kB view details)

Uploaded Source

Built Distribution

fair_kmeans-0.1.1-cp310-cp310-manylinux_2_35_x86_64.whl (618.1 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

File details

Details for the file fair_kmeans-0.1.1.tar.gz.

File metadata

  • Download URL: fair_kmeans-0.1.1.tar.gz
  • Upload date:
  • Size: 80.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.14 Linux/6.5.0-1023-azure

File hashes

Hashes for fair_kmeans-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f404b8ebeb641e47e8523c7599a543048972398ba86c0b986a9c650560fc2414
MD5 c1f63e1b6cff823a9bf816ffd133b0d2
BLAKE2b-256 cbfea4274328ac625423cf2c2a88adea0731990b97c319d8b39ed5b22341eabb

See more details on using hashes here.

File details

Details for the file fair_kmeans-0.1.1-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for fair_kmeans-0.1.1-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 4ac932e955793bd767c73499732c78be5e7ec4b260dc0aaa894f48c2bb74a1eb
MD5 ae70546fa704790a2551dab261523a9f
BLAKE2b-256 e103ae3d4671de029be2e65f349980b448e98b81d8eaa8af25a520d4b2aede5d

See more details on using hashes here.

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