class TLearn::K_Means::Cluster
cluster cluster has id, vec, and v_list
Attributes
id[RW]
last_vec[RW]
v_list[RW]
vec[RW]
Public Class Methods
new(id, vec=nil, v_list=nil, dim=1)
click to toggle source
# File lib/t_learn/k_means.rb, line 70 def initialize(id, vec=nil, v_list=nil, dim=1) @id = id @v_list= v_list @vec = dim.times.map{0.0} if vec == nil @dim = dim calc_center() end
Public Instance Methods
add_v(v)
click to toggle source
# File lib/t_learn/k_means.rb, line 90 def add_v(v) @v_list.push(v) end
calc_center()
click to toggle source
# File lib/t_learn/k_means.rb, line 78 def calc_center() @last_vec = Marshal.load(Marshal.dump(@vec)) vec_sum = Array.new @v_list.each { |v| v.each_with_index { |v_x, i| vec_sum[i] ||= 0.0 vec_sum[i] += v_x } } vec_sum.each_with_index { |new_vec, i| @vec[i] = new_vec/@v_list.size.to_f } end
change_center?()
click to toggle source
# File lib/t_learn/k_means.rb, line 98 def change_center? @dim.times { |i| return true if @vec[i] != @last_vec[i] } return false end
format_hash()
click to toggle source
# File lib/t_learn/k_means.rb, line 105 def format_hash() cluster_hash = {} cluster_hash[:id] = @id cluster_hash[:vec] = @vec cluster_hash[:v_list] = @v_list return cluster_hash end
reset_v_list()
click to toggle source
# File lib/t_learn/k_means.rb, line 94 def reset_v_list() @v_list = [] end