Class: ML::Data::Generator
- Inherits:
-
Object
- Object
- ML::Data::Generator
- Defined in:
- lib/data/generator.rb
Overview
General generator for n-dimentional space
Direct Known Subclasses
Class Method Summary collapse
-
.generate_vector(dim, scale = 1) ⇒ Array
Generating a random vector.
Instance Method Summary collapse
-
#initialize(dim, scale = 1, noise = 0, model = :random) ⇒ Generator
constructor
Initial generator.
-
#points(points, coef) ⇒ Hash
Generate two groups of points.
Constructor Details
#initialize(dim, scale = 1, noise = 0, model = :random) ⇒ Generator
Initial generator
15 16 17 18 19 20 |
# File 'lib/data/generator.rb', line 15 def initialize dim, scale = 1, noise = 0, model = :random @dim = dim @scale = scale @noise = noise @model = model end |
Class Method Details
.generate_vector(dim, scale = 1) ⇒ Array
Generating a random vector
61 62 63 64 |
# File 'lib/data/generator.rb', line 61 def self.generate_vector dim, scale = 1 result = Array.new(dim) { (rand - 0.5) * 2 * scale } result << 1.0 end |
Instance Method Details
#points(points, coef) ⇒ Hash
Generate two groups of points
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/data/generator.rb', line 28 def points points, coef result = {} # for each group [1, -1].each do |grp| points.times do while true point = generate_vector prod = Matrix.column_vector(point).transpose * Matrix.column_vector(coef) if (prod[0,0] <=> 0) == grp result[point] = grp result[point] *= -1 if @model == :random and rand < @noise break end end end end if @model == :flip and @noise > 0 flipping = (points * @noise * 2).to_i order = (0...(points * 2)).to_a.shuffle for i in 0...flipping result[result.keys[order[i]]] *= -1 end end result end |