Code Monkey home page Code Monkey logo

k-means's Introduction

KMeans

Attempting to build a fast, memory efficient K-Means program.

Install

gem sources -a http://rubygems.org
sudo gem install k_means

How To Use

require 'rubygems'
require 'k_means'

data = [[1,1], [1,2], [1,1], [1000, 1000], [500, 500]]
kmeans = KMeans.new(data, :centroids => 2)
kmeans.inspect  # Use kmeans.view to get hold of the un-inspected array
=> [[3, 4], [0, 1, 2]]

Custom Centroids

require 'rubygems'
require 'k_means'

# Your custom centroid needs to have #position and #reposition methods
class CustomCentroid
  attr_accessor :position
  def initialize(position); @position = position; end
  def reposition(nodes, centroid_positions); end
end

custom_centroids = []
2.times { custom_centroids << CustomCentroid.new([1,1]) }

data = [[1,1], [1,2], [1,1], [1000, 1000], [500, 500]]
kmeans = KMeans.new(data, :custom_centroids => custom_centroids)

Distance Measurements

KMeans 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 KMeans algorithm, just provide it as an option:

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

You get the idea…

Benchmarks

# 1000 records with 50 dimensions
data = Array.new(1000) {Array.new(50) {rand(10)}}
ai4r_data = Ai4r::Data::DataSet.new(:data_items=> data)

# Clustering can happen in magical ways
# so lets do it over multiple times
n = 5

Benchmark.bm do |x|
  x.report('KMeans') do
    n.times { KMeans.new(data) }
  end
  x.report("Ai4R") do
    n.times do
      b = Ai4r::Clusterers::KMeans.new
      b.build(ai4r_data, 4)
    end
  end
end
         user     system      total        real
KMeans 15.960000   0.030000  15.990000 ( 16.062639)
Ai4R   70.230000   0.180000  70.410000 ( 70.704843)

Thanks

Copyright © 2009 Red Davis. See LICENSE for details.

k-means's People

Contributors

reddavis avatar celumproject avatar hamzadom avatar vanekjar avatar

Watchers

James Cloos avatar  avatar

Forkers

hamzadom

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.