Class: Datasets::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/datasets/table.rb

Defined Under Namespace

Classes: Record

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset) ⇒ Table

Returns a new instance of Table.



46
47
48
49
# File 'lib/datasets/table.rb', line 46

def initialize(dataset)
  @dataset = dataset
  @dictionaries = {}
end

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset.



45
46
47
# File 'lib/datasets/table.rb', line 45

def dataset
  @dataset
end

Instance Method Details

#[](name_or_index) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# 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_namesObject



63
64
65
# File 'lib/datasets/table.rb', line 63

def column_names
  columner_data.keys
end

#dictionary_encode(name) ⇒ Object



100
101
102
# File 'lib/datasets/table.rb', line 100

def dictionary_encode(name)
  @dictionaries[normalize_name(name)] ||= Dictionary.new(self[name])
end

#each_column(&block) ⇒ Object Also known as: each



67
68
69
# File 'lib/datasets/table.rb', line 67

def each_column(&block)
  columner_data.each(&block)
end

#each_recordObject



72
73
74
75
76
77
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



79
80
81
82
83
84
# 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) ⇒ Object



104
105
106
107
# File 'lib/datasets/table.rb', line 104

def label_encode(name)
  dictionary = dictionary_encode(name)
  dictionary.encode(self[name])
end

#n_columnsObject Also known as: size, length



51
52
53
# File 'lib/datasets/table.rb', line 51

def n_columns
  columner_data.size
end

#n_rowsObject



57
58
59
60
61
# 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_hObject



121
122
123
124
125
126
127
128
129
130
# 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