Skip to content

Instantly share code, notes, and snippets.

@rakasaka
Created August 24, 2011 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakasaka/1169334 to your computer and use it in GitHub Desktop.
Save rakasaka/1169334 to your computer and use it in GitHub Desktop.
Clustering in Ruby using cluster
require 'clusterer'
include Clusterer
@idf = InverseDocumentFrequency.new()
a = Document.new("a lion is a wild feline animal", :idf => @idf).normalize!(@idf)
b = Document.new("a dog is a friendly animal", :idf => @idf).normalize!(@idf)
c = Document.new("a cat is a feline animal", :idf => @idf).normalize!(@idf)
clus = Algorithms.bisecting_kmeans([a,b,c], 2, :maximum_iterations => 5)
# Results
# [#<Clusterer::Cluster:0x00000101200240 @documents=[{"lion"=>0.5, "wild"=>0.5, "felin"=>0.5, "anim"=>0.5}, {"cat"=>0.6134772025315336, "felin"=>0.5584110143854988, "anim"=>0.5584110143854988}]>, #<Clusterer::Cluster:0x00000101200218 @documents=[{"dog"=>0.4949907352038194, "friendli"=>0.4949907352038194, "anim"=>0.7141206789645324}]>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment