### 5 releases (3 breaking)

Uses old Rust 2015

0.8.1 | Jan 22, 2020 |
---|---|

0.8.0 | Jul 25, 2019 |

0.7.0 | Jun 11, 2019 |

0.5.0 | Nov 18, 2018 |

0.3.0 | Feb 13, 2018 |

#**474** in Science

**41** downloads per month

**MIT**license

27KB

513 lines

# RKM - Rust k-means

A simple Rust implementation of the k-means clustering algorithm based on a C++ implementation, dkm.

This implementation is generic, and will accept any type that satisfies the trait requirements. At a minimum, numeric floating point types built into rust should be supported. Uses rayon for parallelism to improve scalability at the cost of some performance on small data sets.

The

feature enables parallelism to speed up the algorithm in complex cases. The parallel algorithm may be slower than the non-parallel algorithm for small data sets, but is much faster for data sets with high dimensionality. Make sure you benchmark your use case with both configurations before deciding which to use.`parallel`

Known to compile against Rust stable 1.32.0.

### Usage

Calculate the k-means clusters for a set of data by calling

with your dataset in a 2D `rkm ::`kmeans_lloyd

`ndarray`

array and the number of clusters you would like to segment the data into. The return value will be a tuple containing the cluster means/centroids (as a 2D `ndarray`

) and a `Vec`

of indices that map each of the input data points to an element of the means array.See

for a simple usage example.`src /example.rs`

### TODOs

- Termination conditions (iterations and delta).

### Data

A small set of benchmarks for this library is included in

. The data sets are as follows:`src /bench.rs`

natural data taken from measurements of different iris plants. 150 points, 2 dimensions, 3 clusters. Source: UCI machine learning repository.`iris .data.csv`

synthetic data. 5000 points, 2 dimensions, 15 clusters. Source: P. Fränti and O. Virmajoki, "Iterative shrinking method for clustering problems", `s1 .data.csv`

*Pattern Recognition*, 39 (5), 761-765, May 2006.

synthetic data large set. 100000 points, 2 dimensions, 100 clusters. Source: Zhang et al., "BIRCH: A new data clustering algorithm and its applications", `birch3 .data.csv`

*Data Mining and Knowledge Discovery*, 1 (2), 141-182, 1997

synthetic data with high dimensionality. 1024 points, 128 dimensions, 16 clusters. Source: P. Fränti, O. Virmajoki and V. Hautamäki, "Fast agglomerative clustering using a k-nearest neighbor graph", `dim128 .data.csv`

*IEEE Trans. on Pattern Analysis and Machine Intelligence*, 28 (11), 1875-1881, November 2006

Compared to dkm this implementation is slower for the small iris and s1 data sets, but faster for the

and `dim128`

data sets.`birch3`

### Licensing

This code is licensed under the MIT license.

#### Dependencies

~4.5MB

~70K SLoC