Class: ML::Data::Generator2D

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

Overview

Generating sample points on 2D plane

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x_range = 100, y_range = 100) ⇒ Generator2D

Initialize a generator



20
21
22
23
# File 'lib/data/generator.rb', line 20

def initialize x_range = 100, y_range = 100
  @x_range = x_range
  @y_range = y_range
end

Class Method Details

.point_from_line(coef, x) ⇒ Array

Generate point from line



12
13
14
# File 'lib/data/generator.rb', line 12

def self.point_from_line coef, x
  [x, (-coef[2]-(coef[0] * x))/coef[1]]
end

Instance Method Details

#points_2d(points, coef = [-1.0, 1.0, 0.0]) ⇒ Hash

Generate two groups of points on 2d plain



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/data/generator.rb', line 30

def points_2d points, coef = [-1.0, 1.0, 0.0]
  result = {}
  # for each group
  [1, -1].each do |grp|
    points.times do
      while true
        point = generate_point
        prod = Matrix.column_vector(point).transpose * Matrix.column_vector(coef)
        if (prod[0,0] <=> 0) == grp
          result[point] = grp
          break
        end
      end
    end
  end
  result
end