class Datasets::Table
Attributes
dataset[R]
Public Class Methods
new(dataset)
click to toggle source
# File lib/datasets/table.rb, line 46 def initialize(dataset) @dataset = dataset @dictionaries = {} end
Public Instance Methods
[](name_or_index)
click to toggle source
# File lib/datasets/table.rb, line 86 def [](name_or_index) case name_or_index when Integer index = name_or_index columner_data.each_with_index do |(_name, values), i| return values if i == index end nil else name = name_or_index columner_data[normalize_name(name)] end end
column_names()
click to toggle source
# File lib/datasets/table.rb, line 63 def column_names columner_data.keys end
dictionary_encode(name)
click to toggle source
# File lib/datasets/table.rb, line 100 def dictionary_encode(name) @dictionaries[normalize_name(name)] ||= Dictionary.new(self[name]) end
each_column(&block)
click to toggle source
# File lib/datasets/table.rb, line 67 def each_column(&block) columner_data.each(&block) end
Also aliased as: each
each_record() { |record| ... }
click to toggle source
# File lib/datasets/table.rb, line 72 def each_record return to_enum(__method__) unless block_given? n_rows.times do |i| yield(Record.new(self, i)) end end
fetch_values(*keys) { |key| ... }
click to toggle source
# File lib/datasets/table.rb, line 109 def fetch_values(*keys) data = columner_data keys.collect do |key| if data.key?(key) data[key] else raise build_key_error(key) unless block_given? yield(key) end end end
find_record(row)
click to toggle source
# File lib/datasets/table.rb, line 79 def find_record(row) row += n_rows if row < 0 return nil if row < 0 return nil if row >= n_rows Record.new(self, row) end
label_encode(name)
click to toggle source
# File lib/datasets/table.rb, line 104 def label_encode(name) dictionary = dictionary_encode(name) dictionary.encode(self[name]) end
n_columns()
click to toggle source
# File lib/datasets/table.rb, line 51 def n_columns columner_data.size end
n_rows()
click to toggle source
# File lib/datasets/table.rb, line 57 def n_rows first_column = columner_data.first return 0 if first_column.nil? first_column[1].size end
to_h()
click to toggle source
# File lib/datasets/table.rb, line 121 def to_h columns = {} @dataset.each do |record| record.to_h.each do |name, value| values = (columns[name] ||= []) values << value end end columns end
Private Instance Methods
build_key_error(key)
click to toggle source
# File lib/datasets/table.rb, line 136 def build_key_error(key) KeyError.new("key not found: #{key.inspect}") end
columner_data()
click to toggle source
# File lib/datasets/table.rb, line 147 def columner_data @columns ||= to_h end
normalize_name(name)
click to toggle source
# File lib/datasets/table.rb, line 151 def normalize_name(name) name.to_sym end