class ML::Data::Generator

General generator for n-dimentional space

Public Class Methods

generate_vector(dim, scale = 1) click to toggle source

Generating a random vector

@param [Integer] dim the dimension of the vector @param [Integer] scale the scale of each component (default [-1,1]) @return [Array] random vector

# 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
new(dim, scale = 1, noise = 0, model = :random) click to toggle source

Initial generator

@param [Integer] dim dimension @param [Numeric] scale the magnitude of the vector @param [Numeric] noise the percentage of noise @param [Symbol] model the noise model, :random for flipping

all the element in a probability, while :flip only flips a
portion of elements randomly
# 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

Public Instance Methods

points(points, coef) click to toggle source

Generate two groups of points

@param [Integer] points the number of points of each set @param [Array] coef array of the size of dimension to specify the

hyper plane

@return [Hash] key: points, value: supervised value

# 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

Protected Instance Methods

generate_vector() click to toggle source
# File lib/data/generator.rb, line 67
def generate_vector
  Generator.generate_vector @dim, @scale
end