Class: ML::Data::Generator

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

Overview

General generator for n-dimentional space

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dim) ⇒ Generator

Initial generator

Parameters:

  • dim (Integer)

    dimension



59
60
61
# File 'lib/data/generator.rb', line 59

def initialize dim
  @dim = dim
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

Returns:

  • (Array)

    random vector



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

def self.generate_vector dim, scale = 1
  result = Array.new(dim) { (rand - 0.5) * 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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/data/generator.rb', line 68

def points points, coef
  result = {}
  # for each group
  [1, -1].each do |grp|
    points.times do
      while true
        point = Generator.generate_vector(@dim, 100)
        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