Skip to main content

A library for K-clustering algorithms with multiple distance metrics and kmeans++ initialisation

Project description

Introduction

This is an implementation of K-means/medians/medoids with various distance metrics (Euclidean, Manhattan, Cosine...) built over Numpy.

I created this library for my own use, because the KMeans class of the Scikit-learn library lacks options for customising the variant of the K-algorithm and options for using distance metrics besides Euclidean distance.

This library was developed with the intention to replicate the functionality and parameter convention used in Scikit-learn as close as possible so that end users, such as myself, will have little difficulty writing code for K-algorithm clustering machine learning tasks with KMars. The additional distance metrics not present in sklearn's clustering such as manhattan, minikowski, and cosine enable better results when working with high dimensional data.

Jupyter notebook comparison with sklearn https://github.com/jerrold110/Library-Kmars/blob/main/notebooks/Comparison%20of%20Sklearn%20and%20Kmars.ipynb

Example:

import numpy as np
from kmars import KMeans

X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])

km = KMeans(4, dist='euclidean', init='kmeans++')
KMeans.fit(X)
labels = km.labels_
cetroids = km.cluster_centers_
print(help(KMeans))

Features:

  • Algorithms: KMeans, KMedians, KMedoids
  • Distance metrics: 'euclidean','manhattan','minikowski','cosine','hamming'
  • K-means++ centroid initialisation with seed search
  • Frobenius (L2) norm convergence, and tolerance parameter
  • Getter methods for positions, error_scores, closest centroid for for initial centroids and final centroids, and more
  • Selection of Sum-Square-Error or Sum-Error metric for KMedoids cluster centroid update and overall fit score
  • Data type changes to float64 during distance calculation to avoid numerical overflow

Distance metrics

The distance metric selected at initialisation is the same metric used for:

  • Centroid initialisation with kmeans++
  • Sum squares error metrics in distance metric at initialisation
  • Kmedoids centroid selection and all-cluster-centroid-update-approval The field that never changes how it is calculated:
  • Manhattan distance (L1 norm) for SSE residuals (to compare different metrics of the same algo)

Future features

  • Data validation to take in pandas dataframes
  • More algorithms, algorithm upgrades (FastPAM for Kmedoids instead of Naive)
  • More distance metrics (eg: improved sqrt cosine)
  • Heuristic centroid initialisation: picks the n_clusters points with the smallest sum distance to every other point

Issues

  • Currently only accepts 2 dimensional numpy arrays as input

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

kmars-0.2.9.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

kmars-0.2.9-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file kmars-0.2.9.tar.gz.

File metadata

  • Download URL: kmars-0.2.9.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for kmars-0.2.9.tar.gz
Algorithm Hash digest
SHA256 8a739b9122fee03e9b2343dced6d59af0547d50c5906c4800560686ed1038d0b
MD5 168da3f164f6053fa93b9559353525c6
BLAKE2b-256 020d5919d505f31faaddc239f8523173a80793b4a7a17adcb3fdbe0803f435b5

See more details on using hashes here.

File details

Details for the file kmars-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: kmars-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for kmars-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 c69c3d248f73c2e35d5a1d9e869d48cbe2d28f4214364409b7ce6553612ffc7d
MD5 a9dea29b7a847575ac8fb98b8eec8de2
BLAKE2b-256 99174c74b6cc81ae3d9c77a916f8c2b972c100a91e3ffbd55efecea767fb88ef

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