Skip to main content

Implementation of K-means algorithm from scratch with adaptive distance metrics

Project description

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 → Uses Minkowski Distance with p = 3

Example Output

when k=3 Clustered Data

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.

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

simple_kmeans-0.1.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

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

simple_kmeans-0.1.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file simple_kmeans-0.1.1.tar.gz.

File metadata

  • Download URL: simple_kmeans-0.1.1.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.15

File hashes

Hashes for simple_kmeans-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9738f76ec80460515bf89444b96fda77206fb181bbf6560bec6399c47bd20235
MD5 d256c2a742630593dbc9580cb048a32b
BLAKE2b-256 bf318a3b406ffd3c12a8d0e7cb83f3dc26a756ca41214e575c9f6b4a03dd1ab8

See more details on using hashes here.

File details

Details for the file simple_kmeans-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: simple_kmeans-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.15

File hashes

Hashes for simple_kmeans-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 387c632375013e93adb6c3ba30d881c50585f11ecd975259a8f5a41d52b3d802
MD5 c26aec1a7300065012fa0d9c44054122
BLAKE2b-256 7063b37ec19133b43dcf54ee23e31e6a94c99a0ca12470a6ed1c53413eece1bd

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