From 81a637ae52d2a1d0bc751b44c193765cdc1091f1 Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Sun, 16 Feb 2014 00:12:32 +0100 Subject: nlp_ruby 0.3 --- kmeans | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'kmeans') diff --git a/kmeans b/kmeans index 5c49d9a..02c9c42 100755 --- a/kmeans +++ b/kmeans @@ -8,7 +8,8 @@ def read_data fn data = {} ReadFile.new(fn).readlines_strip.map{ |i| a = i.split ' ', 2 - data[a.first] = read_feature_string a.last + v = SparseVector.from_kv a.last + data[a.first] = v } return data end @@ -22,7 +23,7 @@ def rand_means_init data, k prng = Random.new a = [] 0.upto(k-1) do - a << mean_sparse_vector(data.values.sample k, random:prng) + a << SparseVector.mean(data.values.sample k, random:prng) end return a end @@ -51,7 +52,7 @@ end def update assignment, data new_centroids = [] assignment.each_pair { |centroid_index,a| - new_centroids << mean_sparse_vector(assignment[centroid_index].map{ |i| data[i] }) + new_centroids << SparseVector.mean(assignment[centroid_index].map{ |i| data[i] }) } return new_centroids end -- cgit v1.2.3