Class: TLearn::K_Means

Inherits:
Object
  • Object
show all
Defined in:
lib/t_learn/k_means.rb

Defined Under Namespace

Classes: Cluster

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#c_listObject

Returns the value of attribute c_list.



7
8
9
# File 'lib/t_learn/k_means.rb', line 7

def c_list
  @c_list
end

#data_listObject

Returns the value of attribute data_list.



7
8
9
# File 'lib/t_learn/k_means.rb', line 7

def data_list
  @data_list
end

#kObject

Returns the value of attribute k.



7
8
9
# File 'lib/t_learn/k_means.rb', line 7

def k
  @k
end

Instance Method Details

#calc_dist(v, cluster) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/t_learn/k_means.rb', line 48

def calc_dist(v, cluster)
  dist_sum = 0.0
  v.each_with_index { |v_x, i|
    dist_sum += (cluster.vec[i] - v_x).abs
  } 
  return dist_sum/v.size
end

#change_clusters_center?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/t_learn/k_means.rb', line 56

def change_clusters_center?()
  @cluster_list.each {|c|
    return true if(c.change_center?) 
  } 
  return false
end

#fit(data_list, k) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/t_learn/k_means.rb', line 18

def fit(data_list, k)
  init(data_list, k)
  history = []
  loop {
    @cluster_list.each{|c| c.reset_v_list()}
    @data_list.each {|d|
      min_dist = 100000
      min_cluster_id = -1
      @cluster_list.each {|c|
        dist = calc_dist(d, c)
        if dist < min_dist 
          min_cluster_id = c.id 
          min_dist = dist
        end
      }
      @cluster_list[min_cluster_id].add_v(d)
    }

    history.push(format_for_log())
    @cluster_list.each{|c| c.calc_center()}
    break if !change_clusters_center?
  }

  return {:result => format_for_log(), :history => history}
end

#format_for_logObject



44
45
46
# File 'lib/t_learn/k_means.rb', line 44

def format_for_log()
  result = @cluster_list.map {|c| c.format_hash()}
end

#init(data_list, k = 2) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/t_learn/k_means.rb', line 9

def init(data_list, k=2)
  @data_list = data_list
  sliced_data_list = @data_list.each_slice(k).to_a
  @dim = data_list[0].size
  @k = k 
  @cluster_list = @k.times.map {|n| Cluster.new(n, nil,sliced_data_list[n] , @dim)}
end