Project

knn

0.02
No commit activity in last 3 years
No release in over 3 years
Simple K Nearest Neighbour algorithm
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.2.9

Runtime

 Project Readme

K Nearest Neighbours¶ ↑

Simple KNN Ruby implementation

Install¶ ↑

gem sources -a -http://gemcutter.org
sudo gem install naive_bayes

How To Use¶ ↑

require 'rubygems'
require 'knn'

data = Array.new(100000) { Array.new(4) { rand } }

knn = KNN.new(data)

knn.nearest_neighbours([1,2,3,4], 4)  # ([data], k's)
  #=> [[4837, 7.43033158269445, [0.966558570073977, 0.903158898673566, 0.954567901514261, 0.988114355901207]], ...

# Data is returned in the format
# [data index, distance to the input, [data points]]

# So if we called queried the data array for 4837...
data[4837]
  #=> [0.966558570073977, 0.903158898673566, 0.954567901514261, 0.988114355901207]

Distance Measurements¶ ↑

KNN uses the Distance Measures Gem (github.com/reddavis/Distance-Measures) so we get quite a range of distance measurements.

The measurements currently available are:

euclidean_distance

cosine_similarity

jaccard_index

jaccard_distance

binary_jaccard_index

binary_jaccard_distance

tanimoto_coefficient

To specify a particular one to use in the KNN algorithm, just provide it as an option:

KNN.new(@data, :distance_measure => :jaccard_index)
KNN.new(@data, :distance_measure => :cosine_similarity)
KNN.new(@data, :distance_measure => :tanimoto_coefficient)

Copyright © 2009 Red Davis. See LICENSE for details.