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] ||= {})
dataset = filter(axis[:column] => axis[:range])
dataset.map {|row| row.each{|att,value| (result[att] ||= []) << value}}
result.each do |k,v|
first = v.first
if first.kind_of?(Numeric) then
v[0] = first.to_f 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
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
|