Class: ML::Data::Generator2D

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

Overview

Generating sample points on 2D plane

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generator

generate_vector, #points

Constructor Details

#initialize(x_range = 100, y_range = 100, noise = 0, model = :random) ⇒ Generator2D

Initialize a generator

Parameters:

  • x_range (Integer) (defaults to: 100)

    x range

  • y_range (Integer) (defaults to: 100)

    y range

  • noise (Numeric) (defaults to: 0)

    the percentage of noise

  • model (Symbol) (defaults to: :random)

    the noise model, :random for flipping all the element in a probability, while :flip only flips a portion of elements randomly



91
92
93
94
95
96
# File 'lib/data/generator.rb', line 91

def initialize x_range = 100, y_range = 100, noise = 0, model = :random
  @x_range = x_range
  @y_range = y_range
  @noise = noise
  @model = model
end

Class Method Details

.point_from_line(coef, x) ⇒ Array

Generate point from line

Parameters:

  • coef (Array)
    a,b,c

    for ax+by+c=0

  • x (Number)

    x value

Returns:

  • (Array)

    point



79
80
81
# File 'lib/data/generator.rb', line 79

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

Parameters:

  • points (Integer)

    the number of points of each set

  • coef (Array) (defaults to: [-1.0, 1.0, 0.0])
    a,b,c

    for ax+by+c=0

Returns:

  • (Hash)

    key: points, value: supervised value



103
104
105
# File 'lib/data/generator.rb', line 103

def points_2d points, coef = [-1.0, 1.0, 0.0]
  points(points, coef)
end