Skip to main content

A Fair K-Means clustering algorithm with fairness constraints.

Project description

Fair K-Means Clustering

Ensuring demographic fairness in K-Means clustering with AI-driven adjustments.

📌 Overview

Fair K-Means is an enhanced clustering algorithm that incorporates fairness constraints to ensure proportional representation of protected demographic groups in cluster assignments. By leveraging external fairness distributions (via OpenAI API), this approach ensures balanced and unbiased clustering while maintaining clustering quality.

🚀 Installation

Install the package from PyPI:

pip install fair-kmeans-uchicago

🛠 Usage

Import and apply Fair K-Means Clustering to your dataset:

from fair_kmeans import fair_kmeans

# Apply Fair K-Means on dataset
df, fairness_metrics = fair_kmeans(data, n_clusters=3, protected_col="gender")

# View fairness results
print(fairness_metrics)

🔹 How It Works

1️⃣ Standard K-Means Clustering

  • Cluster Assignment: Assigns data points to the nearest centroid.
  • Centroid Update: Updates centroids by averaging assigned points.
  • Convergence: Stops iterating when centroids stabilize.

2️⃣ Fairness Constraints

  • Ensures the distribution of protected attributes (e.g., race, gender) in each cluster aligns with an expected fairness distribution.
  • Retrieves fairness constraints dynamically via OpenAI API.

3️⃣ Fairness-Aware Adjustments

  • Identifies clusters where demographic representation deviates from expected proportions.
  • Reassigns individuals from overrepresented to underrepresented clusters.
  • Maintains clustering quality using Silhouette Score as a regulatory measure.

📊 Evaluation Metrics

✔ Fairness Score Computation

Fairness is measured as the absolute deviation between actual and expected demographic proportions:

Fairness Score = | Actual Proportion - Expected Proportion |

A lower fairness score indicates a better-aligned demographic distribution.

✔ Silhouette Score

  • Measures how well-separated and cohesive clusters are.
  • Used to prevent significant loss of clustering quality during fairness adjustments.

🔹 Key Features

Bias Mitigation: Ensures fair representation across all clusters.
Data-Driven Fairness: Uses external fairness benchmarks instead of assuming equal distribution.
Scalability: Can handle large datasets efficiently.
Interpretability: Outputs fairness scores alongside clustering results.
Regulatory Compliance: Supports fair AI governance (GDPR, Equal Credit Opportunity Act).


📌 Limitations

Computational Overhead: Fairness adjustments increase processing time.
Dependency on Fairness Data: External fairness constraints (via OpenAI API) may introduce bias.
Potential Impact on Clustering Quality: Enforcing fairness may slightly reduce clustering compactness.


💡 Applications

  • 📊 Customer Segmentation – Fair clustering in marketing & consumer analytics.
  • 📑 Hiring & Recruitment – Ensures diverse and unbiased hiring pools.
  • 🏦 Fair Credit Scoring – Reduces demographic bias in financial decision-making.
  • 🎓 University Admissions – Promotes equitable student representation.
  • 🚔 Predictive Policing – Ensures fairness in criminal justice analytics.

📄 Citation & References

If you use this package in your research, please cite it as:

@article{fairkmeans2024,
  title={Fair K-Means Clustering},
  author={ASR, AV, RV},
  journal={PyPI},
  year={2024}
}

For further details, refer to:

  • Ghadiri et al., 2020 - "Socially Fair K-Means Clustering"
  • Bateni et al., 2024 - "Individually Fair K-Means Clustering"

📬 Contact & Support

For questions, issues, or contributions, visit our GitHub repository:
🔗 GitHub Repository\


Fair K-Means: Where AI Meets Ethical Clustering! 🚀

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_uchicago-0.1.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fair_kmeans_uchicago-0.1.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file fair_kmeans_uchicago-0.1.3.tar.gz.

File metadata

  • Download URL: fair_kmeans_uchicago-0.1.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.1

File hashes

Hashes for fair_kmeans_uchicago-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ac20670066f767a64223d40b8d4a98ec05877958aab8c36705d1080adee410b9
MD5 fcf409997aaf80332834531af6948b31
BLAKE2b-256 bfc6c206999cfe2a190bb8d14bf2183e50fbe8ca86085294060d6db263acf1af

See more details on using hashes here.

File details

Details for the file fair_kmeans_uchicago-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fair_kmeans_uchicago-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 27171b67940b21b2b7be105c914f25d04544adfb4c6dc90c7b0cfada7ca934d3
MD5 1a6f1b96ce38dc8ea592a411435e5b36
BLAKE2b-256 655a1ef79ce5cfdcbf1887946fa96daa5358745ffd3d6b41ae1cbcaff6173927

See more details on using hashes here.

Supported by

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