Class: Sequel::Dataset

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

Instance Method Summary collapse

Instance Method Details

#vectorize(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sequel_vectorized.rb', line 5

def vectorize options={}

  result = {}
  axis = (options[:axis] ||= {})

  # transform dataset to hash of arrays
  dataset = filter(axis[:column] => axis[:range])

  dataset.map {|row| row.each{|att,value| (result[att] ||= []) << value}}

  # transform numeric and boolean arrays to narrays
  result.each do |k,v|

    first = v.first

    if first.kind_of?(Numeric) then

      v[0] = first.to_f # so NArray is always of type float
      result[k] = NArray.to_na v

    elsif first == true || first == false then

      result[k] = NArray.float(v.size)
      result[k][] = v.map {|i| (i == true) ? 1 : 0 }

    end

  end

  # add dot notation
  columns.each do |col|
    result.instance_eval %Q{def #{col}; self[:#{col}]; end}
  end

  if (axis.empty? || result.empty?) then
    result
  else
    _process(result, axis)
  end

end