Skip to main content

Build Status License: MIT Supported Python version

fair k-center

This package computes an approximate $k$-center clustering under fairness constraints. In this fairness model every point has a color. The task of the clustering is to partition the points into clusters such that the color ratios of the data set are preserved in the clusters. For example if we have the two colors red and blue and the ratio of red to blue points is 1:2 in the dataset then we want that every cluster has a ratio of 1:2 of red and blue points.

A fair clustering can be computed for arbitrary many colors $col_1,\ldots, col_s$ as long as the underlying color ratio is of the form $1\colon t_2:\ldots\colon t_s$ with $t_i\in\mathbb N$. The computed $k$-center clustering will satisfy this color ratio in all clusters.

fair k-center with outliers

If the dataset has no underlying color ratio of this form, one can pass a desired color ratio of the form $1\colon t_2:\ldots\colon t_s$ with $t_i\in\mathbb N$ that should be achieved. If possible, the algortihm will achieve this color ratio in all clustres by declaring some points of color $col_2,\ldots, col_s$ as outliers if needed. Points declared as outliers are not part of any cluster. Futhermore the algorithm will not declare any points of color $col_1$ as outliers.

Parameters:

  • n_clusters: int Number of clusters k of the k-center solution
  • flets: bool, default=False If set to True the labeling of the fairlet assignment is stored
  • outlier: bool, default=True Should be set to False only when the data has 2 colors and the color ratio in the data is exactly 1:1. If set to False the clustering is computed with an algortihm that can't handle outliers. This may produce better results in the special case of 2 colors and color ratio 1:1.

Attributes:

  • labels_: ndarray of shape (n_samples,) dtype=int Labels between 0 and n_clusters-1 indicating which points belong to the same cluster and which center represents the cluster.
  • fairlets_: ndarray of shape (n_samples,) dtype=int Labels between 0 and n_samples-1 indicating which points belong to the same fairlet and which center represents the fairlet.
  • cluster_centers_: ndarray of shape (n_clusters, n_features) Coordinates of the cluster centers, center at position i represents all points with label i.
  • inertia_: float $k$-center cost (radius) of the clustering
  • inertia_fairlets_: float radius of the fairlets

fit(X, col, col_ratios=None, auto_ratios=False):

  • X: ndarray of shape (n_samples, n_features) Coordinates of points which are clustered
  • col: ndarray of shape (n_samples, ) Colors of points
  • col_ratios: ndarray of shape (n_colors, ), default=None
    Color ratios that should be satisfied in each cluster. If the numbers are not integers, they are converted to integers in the method. The colors are sorted inreasingly, then the i-th color ratio refers to the i-th smallest color. At least one of the ratios should be 1.
  • auto_ratios: bool, default=False If set to True and no col_ratios are given the ratios are picked automatically in the following way. First the color with the fewest number of points $m$ is determined. For every color we determine the number of points of this color $m'$ and set the respective ratio to $\lfloor\frac{m'}{m}\rfloor$. If neither auto_ratios is set to True nor col_ratios is passed, the color ratios are set to $1:1:...:1$.

Third-Party Libraries

This project includes the LEMON C++ graph library, which is licensed under the Boost Software License 1.0.
See fair_kcenter/cpp/lemon/LICENSE for full license text.

Usage

Before installing the package, make sure you have a C++ compiler and Python development headers installed.

To install Python development headers on Ubuntu / WSL, run:

sudo apt update
sudo apt install -y build-essential python3-dev

Install poetry

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

Install the package

poetry install

Run the program for fair_kcenter

poetry run python -m fair_kcenter --data-path your_data.csv --ratios-path your_ratios.csv --verbose --results-path your_result.json --k 3

Your data-path should be a file with a header containing the comma-separated data points and their color in the last column. Example:

0,1,2,3,4
5.1,3.5,1.4,0.2,0
4.9,3.0,1.4,0.2,1
4.7,3.2,1.3,0.2,1
4.6,3.1,1.5,0.2,0
5.0,3.6,1.4,0.2,2
...

Your ratios-path should be a file with a header containing comma-separated values. It should have the same length as the number of distinct colors. Example:

ratios
1
3
2
...

Example:

import numpy as np
import fair_kcenter as fk
from fair_kcenter.util import JsonEncoder

if __name__ == "__main__":

    X=np.array([[-1,0,0],[-2,0,0],[-3,0,0],[0,1,0],[0,2,0],[0,3,0],[0,4,0],[-4,0,0]])
    col=np.array([0,1,2,0,1,2,2,2])

    #Optional: if you want you can specify color ratios which should be achieved 
    color_ratios=np.array([1,1,2])

    n_clusters=2

    #Optional: if you want to know the fairlest labels
    flets=True

    kcenter = fk.Fairkcenter(
        n_clusters=n_clusters,
        flets=flets,
    )

    kcenter.fit(
        X=X,
        col=col,
        col_ratios=color_ratios,
    )

    print(kcenter.labels_)
    print(kcenter.cluster_centers_)

Metadata

Release files for fair-kcenter 0.1.0

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-kcenter 0.1.0
File Size Uploaded Provenance Actions
fair_kcenter-0.1.0.tar.gz 429.4 kB With provenance Details for fair_kcenter-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for fair-kcenter 0.1.0
File Interpreter ABI Platform Actions
fair_kcenter-0.1.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for fair_kcenter-0.1.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
fair_kcenter-0.1.0-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_kcenter-0.1.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
fair_kcenter-0.1.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details for fair_kcenter-0.1.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Total release size: 4.2 MB

Release files / fair_kcenter-0.1.0.tar.gz

Download URL fair_kcenter-0.1.0.tar.gz
Size 429.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9497d7c1e483f1bdc35cf055c079462c8c744f30f98bdc0ce5ab443bcc6fdc44
BLAKE2b-256 checksum
How to use checksums
c4ad82a078dc4fde017273f6913ccd8a2157ec69b44ffbd1a9441748999555c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / fair_kcenter-0.1.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL fair_kcenter-0.1.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 1.3 MB
Tags CPython 3.14 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
f0ddbd937abd8bc94f07d1739e7b339e2b2940d115ed6cb97db0371254554efa
BLAKE2b-256 checksum
How to use checksums
2c203ce374db800c17f52cff069322d5b74011bafb53717196671bc610ae98f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / fair_kcenter-0.1.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL fair_kcenter-0.1.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 1.3 MB
Tags CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
049edc53cea3bbf3401597d4540005ab0f2cf13f67e6c70b6dfd51f626d688e5
BLAKE2b-256 checksum
How to use checksums
728645b2ad14ebc9bbd0620b743a5dae3a7887c909cb131fd220279aa8f9710c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / fair_kcenter-0.1.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL fair_kcenter-0.1.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 1.3 MB
Tags CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
71c364e079e08ebe054e6549c20e00684653d80009060e2a635fd3dbdc703f2b
BLAKE2b-256 checksum
How to use checksums
c65f0a33f396940c980fb69c96231bb12c6194ade2ab7b0368fc6435aaa898a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Security for fair-kcenter 0.1.0

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.

Full provenance

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for fair_kcenter-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

4 release files Full provenance

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