Permalink
Browse files

38 Add demo2

  • Loading branch information...
1 parent ccf5d71 commit 37d438cbcee824dc6636de8e44109720675bf02b @id774 committed Oct 29, 2013
Showing with 75 additions and 0 deletions.
  1. +37 −0 README.md
  2. +38 −0 demo2.rb
View
@@ -43,6 +43,43 @@ kmeans.make_cluster
p kmeans.cluster.values #=> [["test01", "test04"], ["test02"], ["test03", "test05", "test06"]]
```
+Hierarchical clustering
+
+``` html
+require 'kmeans/pair'
+require 'kmeans/pearson'
+require 'kmeans/hcluster'
+require 'kmeans/dendrogram'
+
+hash = {
+ "test01"=> {"hoge"=>0, "fuga"=>1, "piyo"=>0 },
+ "test02"=> {"hoge"=>2, "fuga"=>1, "piyo"=>3 },
+ "test03"=> {"hoge"=>3, "fuga"=>0, "piyo"=>1 },
+ "test04"=> {"hoge"=>0, "fuga"=>2, "piyo"=>0 },
+ "test05"=> {"hoge"=>4, "fuga"=>2, "piyo"=>3 },
+ "test06"=> {"hoge"=>5, "fuga"=>8, "piyo"=>2 },
+ "test07"=> {"hoge"=>2, "fuga"=>6, "piyo"=>1 },
+ "test08"=> {"hoge"=>3, "fuga"=>4, "piyo"=>1 },
+ "test09"=> {"hoge"=>2, "fuga"=>2, "piyo"=>1 },
+ "test10"=> {"hoge"=>1, "fuga"=>2, "piyo"=>1 }
+}
+
+# Hierarchical clustering
+hcluster = Kmeans::HCluster.new
+
+keys = hash.keys
+values = hash.values.map {|x| x.values }
+clust = hcluster.hcluster(values)
+p hcluster.printclust(clust, keys) #=> "-\n -\n -\n test07\n -\n test01\n test04\n -\n test08\n -\n test06\n -\n test09\n test10\n -\n test02\n -\n test03\n test05\n"
+
+# Draw Dendrogram (Using with RMagick)
+dendrogram = Kmeans::Dendrogram.new(
+ :imagefile => 'demo.png'
+)
+
+dendrogram.drawdendrogram(clust, keys) # Create demo.png on your directory
+```
+
For more info, see spec files.
View
@@ -0,0 +1,38 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+
+$:.unshift File.join(File.dirname(__FILE__))
+
+require 'lib/kmeans/pair'
+require 'lib/kmeans/pearson'
+require 'lib/kmeans/hcluster'
+require 'lib/kmeans/dendrogram'
+
+hash = {
+ "test01"=> {"hoge"=>0, "fuga"=>1, "piyo"=>0 },
+ "test02"=> {"hoge"=>2, "fuga"=>1, "piyo"=>3 },
+ "test03"=> {"hoge"=>3, "fuga"=>0, "piyo"=>1 },
+ "test04"=> {"hoge"=>0, "fuga"=>2, "piyo"=>0 },
+ "test05"=> {"hoge"=>4, "fuga"=>2, "piyo"=>3 },
+ "test06"=> {"hoge"=>5, "fuga"=>8, "piyo"=>2 },
+ "test07"=> {"hoge"=>2, "fuga"=>6, "piyo"=>1 },
+ "test08"=> {"hoge"=>3, "fuga"=>4, "piyo"=>1 },
+ "test09"=> {"hoge"=>2, "fuga"=>2, "piyo"=>1 },
+ "test10"=> {"hoge"=>1, "fuga"=>2, "piyo"=>1 }
+}
+
+# Hierarchical clustering
+hcluster = Kmeans::HCluster.new
+
+keys = hash.keys
+values = hash.values.map {|x| x.values }
+clust = hcluster.hcluster(values)
+p hcluster.printclust(clust, keys) #=> "-\n -\n -\n test07\n -\n test01\n test04\n -\n test08\n -\n test06\n -\n test09\n test10\n -\n test02\n -\n test03\n test05\n"
+
+# Draw Dendrogram (Using with RMagick)
+dendrogram = Kmeans::Dendrogram.new(
+ :imagefile => 'demo.png'
+)
+
+dendrogram.drawdendrogram(clust, keys)
+

0 comments on commit 37d438c

Please sign in to comment.