Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sungwoncho/k_means_ruby

K-means Algorithm in Ruby
https://github.com/sungwoncho/k_means_ruby

Last synced: 3 months ago
JSON representation

K-means Algorithm in Ruby

Awesome Lists containing this project

README

        

# k_means_ruby

[![Build Status](https://travis-ci.org/sungwoncho/k_means_ruby.svg?branch=master)](https://travis-ci.org/sungwoncho/k_means_ruby)
[![Coverage Status](https://coveralls.io/repos/sungwoncho/k_means_ruby/badge.svg?branch=master)](https://coveralls.io/r/sungwoncho/k_means_ruby?branch=master)

An implementation of k-means algorithm in Ruby.

## Definition

K-means algorithm is a clustering algorithm for a Euclidean space. It assumes that there are k clusters in the beginning.

* Choose k points that are likely to be in different clusters.
* Make those the points centroids of clusters.
* For the remaining points:
* Find the centroid to which the point is closest.
* Merge the point into that cluster.
* Recalculate the centroid of that cluster.

## Use

In your ruby console, require the main file:

`require './lib/k_means_ruby.rb'`

Define `Points` and `Clusters`:

```
p1 = KMeansRuby::Point.new(1,2)
p2 = KMeansRuby::Point.new(10,19)
...
c1 = KMeansRuby::Cluster.new(p1)
...
```

Instantiate the `Algorithm`:

`algorithm = KMeansRuby::Algorithm.new(points=[p1, p2], clusters=[c1])`

Run the algorithm by calling `algorithm.run_once` or `algorithm.repeat_for(n)`

## Contributing

### TODO:

* Allow users to run the algorithm until the clusters do not change anymore.