Class: ML::Data::Generator

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

Overview

General generator for n-dimentional space

Direct Known Subclasses

Generator2D

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dim, scale = 1, noise = 0, model = :random) ⇒ Generator

Initial generator

Parameters:

  • dim (Integer)

    dimension

  • scale (Numeric) (defaults to: 1)

    the magnitude of the vector

  • 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



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

Parameters:

  • dim (Integer)

    the dimension of the vector

  • scale (Integer) (defaults to: 1)

    the scale of each component (default [-1,1])

Returns:

  • (Array)

    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

Parameters:

  • points (Integer)

    the number of points of each set

  • coef (Array)

    array of the size of dimension to specify the hyper plane

Returns:

  • (Hash)

    key: points, value: supervised value



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