Class: SOM
- Inherits:
-
Object
- Object
- SOM
- Defined in:
- lib/som.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#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.
-
#global_distance_error ⇒ Object
Taken from AI4R SOM library #107.
-
#initialize(training_data, options = {}) ⇒ SOM
constructor
A new instance of SOM.
-
#inspect ⇒ Object
Returns an array of buckets containing the index of the training data.
- #nodes ⇒ Object
- #train ⇒ Object
Constructor Details
#initialize(training_data, options = {}) ⇒ SOM
Returns a new instance of SOM.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/som.rb', line 17 def initialize(training_data, ={}) @training_data = training_data @dimensions = training_data[0].size @iteration_count = 1 # Options @number_of_nodes = [:nodes] || 5 @learning_rate = [:learning_rate] || 0.7 @radius = [:radius] || @number_of_nodes / 2 @max_iterations = [:max_iterations] || 100 @db_filepath = [:save_to] # TODO: Allow a lambda so we can use different neighborhood functions @neighborhood_function = 1 #options[:neighborhood_function] || 1 @verbose = [:verbose] create_nodes(training_data) end |
Class Method Details
.load(db_filepath) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/som.rb', line 6 def load(db_filepath) data = "" File.open(db_filepath) do |f| while line = f.gets data << line end end Marshal.load(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
59 60 61 62 |
# File 'lib/som.rb', line 59 def classify(data) closest_node = find_closest_node(data) [closest_node.id, closest_node.bucket] end |
#global_distance_error ⇒ Object
Taken from AI4R SOM library #107
65 66 67 68 69 |
# File 'lib/som.rb', line 65 def global_distance_error @training_data.inject(0) do |sum, n| sum + find_closest_node_with_distance(n)[1] end end |
#inspect ⇒ Object
Returns an array of buckets containing the index of the training data
50 51 52 |
# File 'lib/som.rb', line 50 def inspect nodes.map {|x| [x.id, x.bucket] } end |
#nodes ⇒ Object
36 37 38 |
# File 'lib/som.rb', line 36 def nodes @nodes ||= [] end |
#train ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/som.rb', line 40 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) save end |