Class: Eps::DataFrame

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = []) ⇒ DataFrame

Returns a new instance of DataFrame.



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
# File 'lib/eps/data_frame.rb', line 6

def initialize(data = [])
  @columns = {}

  if data.is_a?(Eps::DataFrame)
    data.columns.each do |k, v|
      @columns[k] = v
    end
  elsif rover?(data) || daru?(data)
    data.to_h.each do |k, v|
      @columns[k.to_s] = v.to_a
    end
  elsif data.is_a?(Hash)
    data.each do |k, v|
      @columns[k.to_s] = v.to_a
    end
  else
    data = data.to_a if numo?(data)

    if data.any?
      row = data[0]

      if row.is_a?(Hash)
        row.keys.each do |k|
          @columns[k.to_s] = data.map { |r| r[k] }
        end
      elsif row.is_a?(Array)
        row.size.times do |i|
          @columns["x#{i}"] = data.map { |r| r[i] }
        end
      else
        @columns["x0"] = data
      end
    end
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



3
4
5
# File 'lib/eps/data_frame.rb', line 3

def columns
  @columns
end

#labelObject

Returns the value of attribute label.



4
5
6
# File 'lib/eps/data_frame.rb', line 4

def label
  @label
end

#weightObject

Returns the value of attribute weight.



4
5
6
# File 'lib/eps/data_frame.rb', line 4

def weight
  @weight
end

Instance Method Details

#==(other) ⇒ Object



132
133
134
# File 'lib/eps/data_frame.rb', line 132

def ==(other)
  columns.keys == other.columns.keys && columns == other.columns
end

#[](rows, cols = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/eps/data_frame.rb', line 70

def [](rows, cols = nil)
  if cols.nil?
    if rows.is_a?(String) || (rows.is_a?(Array) && rows.first.is_a?(String))
      cols = rows
      rows = 0..-1
    end
  end

  if rows.is_a?(Range)
    if rows.end.nil?
      rows = Range.new(rows.begin, size - 1)
    elsif rows.end < 0
      rows = Range.new(rows.begin, size + rows.end, rows.exclude_end?)
    else
      finish = rows.end
      finish -= 1 if rows.exclude_end?
      rows = Range.new(rows.begin, size - 1) if finish >= size - 1
    end
  elsif rows.is_a?(Integer)
    rows = [rows]
  end

  if cols
    if cols.is_a?(Range)
      c = columns.keys

      start_index = c.index(cols.begin)
      raise "Undefined column: #{cols.begin}" unless start_index

      end_index = c.index(cols.end)
      raise "Undefined column: #{cols.end}" unless end_index

      reverse = false
      if start_index > end_index
        reverse = true
        start_index, end_index = end_index, start_index
      end

      cols = c[Range.new(start_index, end_index, cols.exclude_end?)]
      cols.reverse! if reverse
    elsif !cols.is_a?(Array)
      singular = true
      cols = [cols]
    end
  else
    cols = columns.keys
  end

  df = Eps::DataFrame.new

  cols.each do |c|
    raise "Undefined column: #{c}" unless columns.include?(c)

    col = columns[c]
    df.columns[c] = rows.map { |i| col[i] }
  end
  df.label = rows.map { |i| label[i] } if label
  df.weight = rows.map { |i| weight[i] } if weight

  singular ? df.columns[cols[0]] : df
end

#any?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/eps/data_frame.rb', line 50

def any?
  @columns.any?
end

#dupObject



136
137
138
139
140
141
142
143
144
# File 'lib/eps/data_frame.rb', line 136

def dup
  df = Eps::DataFrame.new
  columns.each do |k, v|
    df.columns[k] = v
  end
  df.label = label
  df.weight = weight
  df
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/eps/data_frame.rb', line 42

def empty?
  size == 0
end

#mapObject



54
55
56
57
58
59
60
# File 'lib/eps/data_frame.rb', line 54

def map
  if @columns.any?
    size.times.map do |i|
      yield @columns.to_h { |k, v| [k, v[i]] }
    end
  end
end

#map_rowsObject



62
63
64
65
66
67
68
# File 'lib/eps/data_frame.rb', line 62

def map_rows
  if @columns.any?
    size.times.map do |i|
      yield @columns.map { |_, v| v[i] }
    end
  end
end

#sizeObject



46
47
48
# File 'lib/eps/data_frame.rb', line 46

def size
  @columns.any? ? columns.values.first.size : 0
end