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 module such as manhattan, minikowski, and cosine enable better results when working with high dimensional data.
Jupyter notebook comparison with sklearn:
Example:
pip install kmars
https://pypi.org/project/kmars/
import numpy as np
from kmars import KMeans, KMedians, KMedoids
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 in selected distance metric
- Frobenius (L2) norm convergence, and tolerance parameter
- Getter methods for many metrics after fitting for initial and final centroids
- 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
- Manhattan distance (L1 norm) SSE residuals as a common metric to compare different distance metrics of the same model
Available metrics after fitting model to data
- init_cluster_centers_: position of initial centroids
- init_labels_: closest initial centroid of each data point
- init_sse_: sum of squared errors of initial clusters in the chosen distance metric
- cluster_centers_: positions of final centroids
- labels_: closest final centroid of each data point
- sse_: sum of squared errors of final clusters in the chosen distance metric
- ssr_: sum of squared errors of final clusters in Manhattan distance (L1 norm)
- n_iter_converge_: number of iterations for convergence
- x_samples_: number of samples of data during fit
- x_features_: number of features in each datum during fit
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
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 kmars-0.2.16.tar.gz.
File metadata
- Download URL: kmars-0.2.16.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02b092194b6ddb18125d459f10c862a41768c1821363c858a618ff0de93b16f1
|
|
| MD5 |
88db185819cd74921f2fb90d28523c04
|
|
| BLAKE2b-256 |
f1d8b38b9883b769d9dfb8b904a9af3b9178ee4f4a038e5d6acfb184594c6d3f
|
File details
Details for the file kmars-0.2.16-py3-none-any.whl.
File metadata
- Download URL: kmars-0.2.16-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e11412736c5484a7c0a9239720ebf337baa052274866f049293d65075ddabc96
|
|
| MD5 |
cda4f036006f0c7f2e83bbf5d15bda38
|
|
| BLAKE2b-256 |
94f1d6c96c4795de2a5cf19658f58fddc90acca583c04e44d8dbde5b792c5a25
|