Skip to main content

A from-scratch implementation of K-Means clustering algorithm

Project description

K-Means From Scratch

Une implémentation de l'algorithme K-Means en Python avec NumPy uniquement.

Installation

pip install kmeans-from-scratch

# K-Means From Scratch 🎯

[![PyPI version](https://badge.fury.io/py/kmeans-from-scratch-laguchori.svg)](https://pypi.org/project/kmeans-from-scratch-laguchori/)
[![Python](https://img.shields.io/badge/python-3.8%2B-blue.svg)](https://www.python.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> Une implémentation **from scratch** de l'algorithme K-Means en Python, utilisant uniquement **NumPy**. Parfaite pour apprendre le Machine Learning et les Design Patterns.

---

## ✨ Features

- ✅ **100% NumPy** : Aucune dépendance lourde (pas de scikit-learn, pandas, etc.)
- ✅ **API compatible scikit-learn** : `fit()`, `predict()`, `transform()`, `score()`
- ✅ **Deux stratégies d'initialisation** : `random` et `k-means++`
-  **Vectorisé** : Performance optimisée grâce au broadcasting NumPy
-  **Design Patterns** : Pattern Strategy pour l'initialisation, code modulaire et testable
- ✅ **Type hints** : Annotations de type pour une meilleure expérience IDE
- ✅ **Tests inclus** : Structure prête pour pytest

---

# Cloner le repo
git clone https://github.com/laguchoritarik/kmeans-from-scratch.git
cd kmeans-from-scratch

# Créer un environnement virtuel
python -m venv venv
source venv/bin/activate  # Linux/Mac
# ou
venv\Scripts\activate  # Windows

# Installer en mode développement
pip install -e .[dev]

# Exemple
import numpy as np
from kmeans_lib import KMeans

# Générer des données d'exemple
```python
np.random.seed(42)
X = np.vstack([
    np.random.randn(50, 2) + [0, 0],   # Cluster 1
    np.random.randn(50, 2) + [5, 5]    # Cluster 2
])

# Créer et entraîner le modèle
kmeans = KMeans(n_clusters=2, init='k-means++', max_iter=100)
kmeans.fit(X)

# Prédire les clusters
labels = kmeans.predict(X)
print(f"Labels: {labels[:10]}")  # Premier 10 prédictions

# Accéder aux centroïdes
print(f"Centroïdes:\n{kmeans.cluster_centers_}")

# Calculer l'inertie (qualité du clustering)
print(f"Inertie: {kmeans.inertia_:.2f}")
```python

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

kmeans_from_scratch-0.1.9.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

kmeans_from_scratch-0.1.9-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file kmeans_from_scratch-0.1.9.tar.gz.

File metadata

  • Download URL: kmeans_from_scratch-0.1.9.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kmeans_from_scratch-0.1.9.tar.gz
Algorithm Hash digest
SHA256 f425b8685dd5360c24ef8937ff2dccf355d2645856231bc5a979be430ff23595
MD5 24634875d5f951b7de0fbca1846440ef
BLAKE2b-256 1a02ecfe9e714ddbc2ab66a75121714f6c67b6a7b1ddecde82884f21bd908227

See more details on using hashes here.

Provenance

The following attestation bundles were made for kmeans_from_scratch-0.1.9.tar.gz:

Publisher: pypi-publish.yml on laguchoritarik/kmeans-from-scratch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kmeans_from_scratch-0.1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for kmeans_from_scratch-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 1ef2a8e4a89391abde2ca096428d0a37cb974f2b7926e990fd9b2243e58f1eae
MD5 4c1a4933e5d318797a86f63f66b5fc3d
BLAKE2b-256 9086c63f4ad5463373c81edf083888d4f208af748d0c96b303205bcd614ca3eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for kmeans_from_scratch-0.1.9-py3-none-any.whl:

Publisher: pypi-publish.yml on laguchoritarik/kmeans-from-scratch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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