Custom K-Means Clustering
This repository contains an implementation of the K-Means clustering algorithm from scratch, designed to work similarly to sklearn.cluster.KMeans. However, it uses a custom distance metric based on the dimensionality of the input data:
- 1D Data → Uses Manhattan Distance (L1 norm)
- 2D Data → Uses Euclidean Distance (L2 norm)
- 3D and above → Cosine Distance
Example Output
when k=3
Features
✅ Fully implemented from scratch ✅ Supports multiple distance metrics based on input dimensionality ✅ Works like Scikit-learn’s KMeans
Installation
You can install this package by cloning the repository and using pip:
pip install .
Or you can install it from pypi
pip install simple-kmeans
Usage
Here’s an example of how to use the custom K-Means implementation:
from simple_kmeans import KMeans
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data
X = np.array([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]])
# Initialize and fit the model
kmeans = KMeansCustom(k=2, max_iters=100)
kmeans.fit(X)
# Get cluster centers and labels
print("Cluster Centers:", kmeans.centroids)
print("Labels:", kmeans.labels)
# Plot results
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels, cmap='viridis')
plt.scatter(kmeans.centroids[:, 0], kmeans.centroids[:, 1], marker='x', c='red', s=200, label='Centroids')
plt.legend()
plt.title("Custom K-Means Clustering")
plt.show()
License
This project is open-source and available under the MIT License.
Contributions
Contributions, issues, and feature requests are welcome! Feel free to fork this repository and submit a pull request.
Metadata
Release files for simple-kmeans 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simple_kmeans-0.1.2.tar.gz | 3.1 kB | Without provenance | Details for simple_kmeans-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simple_kmeans-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for simple_kmeans-0.1.2-py3-none-any.whl |
Total release size: 6.4 kB
Release files / simple_kmeans-0.1.2.tar.gz
| Download URL | simple_kmeans-0.1.2.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
390f9a92936bb6ca2f335005e733633c58398a5dcd089beb3b1f7bd831f8dfc4
|
|
BLAKE2b-256 checksum How to use checksums |
186afe380a11c517a278ab597a2ff682da7fe594dab95c7a9174d530f15f4fe3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.15
|
Release files / simple_kmeans-0.1.2-py3-none-any.whl
| Download URL | simple_kmeans-0.1.2-py3-none-any.whl |
|---|---|
| Size | 3.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
05440aeeafcde6f9c80774843498c923351204a7034df32d081d83f0d9128fb6
|
|
BLAKE2b-256 checksum How to use checksums |
d5d71fb9e924a816f1f35471937eba702f8610e2ba294e9ff6d1cdd2572fc422
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.15
|
Security for simple-kmeans 0.1.2
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.