Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A Ruby gem with lots of distance measures for your projects.

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 Rakefile
Octocat-spinner-32 measurable.gemspec


Build Status Code Climate

A gem to test what metric is best for certain kinds of datasets in machine learning.

Besides the Array class, I also want to support NVector (from NMatrix).

The distance measures will be created in Ruby first. If I see that it's really too slow, I'll write some methods in C (or Java, for JRuby).

This is a fork of the gem Distance Measure, which has a similar objective, but isn't actively maintained and doesn't support NMatrix. Thank you, @reddavis. :)


gem install measurable

I only tested it with 2.0.0 (yes, yes, travis, I'll do it eventually). I want to support JRuby as well.

Distance measures

I'm using the term "distance measure" without much concern for the strict mathematical definition of a metric. If the documentation for one of the methods isn't clear about it being or not a metric, please open an issue.

The following are the similarity measures supported at the moment:

These still need to be implemented:

  • Correlation distance
  • Chi-square distance
  • Kullback-Leibler divergence
  • Jensen-Shannon divergence
  • Mahalanobis distance
  • Squared Mahalanobis distance

I plan to update the specs to reflect that each method is (or isn't) a mathematical metric, but I want to finish implementing them first. Any help is appreciated! :)

How to use

The API I intend to support is something like this:

require "measurable"

u = NMatrix.ones([2, 1])
v = NMatrix.zeros([2, 1])
w = [1, 0]
x = [2, 2]

# Calculate the distance between two points in space.
Measurable.euclidean(u, v) # => 1.41421
Measurable.euclidean(w, v) # => 1.00000
Measurable.cosine([1, 2], [2, 3]) # => 0.00772

# Calculate the norm of a vector, i.e. its distance from the origin.
Measurable.euclidean_squared([3, 4]) # => 25


RDoc syntax is used to document the project. To build it locally, you'll need to install the Fivefish generator (gem install rdoc-generator-fivefish) and run the following command:

rake rdoc

If there's something wrong with an explanation or if there's information missing, please open an issue or send a pull request.


See LICENSE for details.

The original distance_measures gem is copyrighted by @reddavis.

Something went wrong with that request. Please try again.