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 🎯
[](https://pypi.org/project/kmeans-from-scratch-laguchori/)
[](https://www.python.org/)
[](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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f425b8685dd5360c24ef8937ff2dccf355d2645856231bc5a979be430ff23595
|
|
| MD5 |
24634875d5f951b7de0fbca1846440ef
|
|
| BLAKE2b-256 |
1a02ecfe9e714ddbc2ab66a75121714f6c67b6a7b1ddecde82884f21bd908227
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kmeans_from_scratch-0.1.9.tar.gz -
Subject digest:
f425b8685dd5360c24ef8937ff2dccf355d2645856231bc5a979be430ff23595 - Sigstore transparency entry: 1059684568
- Sigstore integration time:
-
Permalink:
laguchoritarik/kmeans-from-scratch@2b667d63537fa28f4880764cd99c7064dee92203 -
Branch / Tag:
refs/tags/v0.1.10 - Owner: https://github.com/laguchoritarik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@2b667d63537fa28f4880764cd99c7064dee92203 -
Trigger Event:
push
-
Statement type:
File details
Details for the file kmeans_from_scratch-0.1.9-py3-none-any.whl.
File metadata
- Download URL: kmeans_from_scratch-0.1.9-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ef2a8e4a89391abde2ca096428d0a37cb974f2b7926e990fd9b2243e58f1eae
|
|
| MD5 |
4c1a4933e5d318797a86f63f66b5fc3d
|
|
| BLAKE2b-256 |
9086c63f4ad5463373c81edf083888d4f208af748d0c96b303205bcd614ca3eb
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kmeans_from_scratch-0.1.9-py3-none-any.whl -
Subject digest:
1ef2a8e4a89391abde2ca096428d0a37cb974f2b7926e990fd9b2243e58f1eae - Sigstore transparency entry: 1059684569
- Sigstore integration time:
-
Permalink:
laguchoritarik/kmeans-from-scratch@2b667d63537fa28f4880764cd99c7064dee92203 -
Branch / Tag:
refs/tags/v0.1.10 - Owner: https://github.com/laguchoritarik
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@2b667d63537fa28f4880764cd99c7064dee92203 -
Trigger Event:
push
-
Statement type: