Class: SOM

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

Instance Method Summary collapse

Constructor Details

#initialize(training_data, options = {}) ⇒ SOM

Returns a new instance of SOM.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/som.rb', line 5

def initialize(training_data, options={})
  @training_data = training_data
  @dimensions = training_data[0].size
  @iteration_count = 1
  
  # Options
  @number_of_nodes = options[:nodes] || 5
  @learning_rate = options[:learning_rate] || 0.5
  @radius = options[:radius] || @number_of_nodes / 2
  @max_iterations = options[:max_iterations] || 100
  
  # TODO: Allow a lambda so we can use different neighborhood functions
  @neighborhood_function = 1 #options[:neighborhood_function] || 1
  @verbose = options[:verbose]
  
  create_nodes(training_data)
end

Instance Method Details

#classify(data) ⇒ Object

Return training data from the node that is closest to input data You are returned an array that look like:

node_id, [training_data_index_1, training_data_index_2…]

The index is the original index of that that was pumped into the SOM during the training process



45
46
47
48
# File 'lib/som.rb', line 45

def classify(data)
  closest_node = find_closest_node(data)
  [closest_node.id, closest_node.bucket]
end

#global_distance_errorObject

Taken from AI4R SOM library #107



51
52
53
54
55
# File 'lib/som.rb', line 51

def global_distance_error
  @training_data.inject(0) do |sum, n|
    sum + find_closest_node_with_distance(n)[1]
  end
end

#inspectObject

Returns an array of buckets containing the index of the training data



36
37
38
# File 'lib/som.rb', line 36

def inspect
  nodes.map {|x| [x.id, x.bucket] }
end

#nodesObject



23
24
25
# File 'lib/som.rb', line 23

def nodes
  @nodes ||= []
end

#trainObject



27
28
29
30
31
32
33
# File 'lib/som.rb', line 27

def train
  while train_it!(@training_data)
  end
  # Place the data in the nodes buckets so we can see how
  # The data has been clustered
  place_data_into_buckets(@training_data)
end