Class: OutlierTree::Dataset

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Dataset

Returns a new instance of Dataset.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/outliertree/dataset.rb', line 5

def initialize(data)
  @data = data

  if defined?(Rover::DataFrame) && data.is_a?(Rover::DataFrame)
    @vectors = data.vectors
    @numeric_columns, @categorical_columns = data.keys.partition { |k, v| ![:object, :bool].include?(data[k].type) }
  else
    @vectors = {}
    raise ArgumentError, "Array elements must be hashes" unless data.all? { |d| d.is_a?(Hash) }
    keys = data.flat_map(&:keys).uniq
    keys.each do |k|
      @vectors[k] = []
    end
    data.each do |d|
      keys.each do |k|
        @vectors[k] << d[k]
      end
    end
    @numeric_columns, @categorical_columns = keys.partition { |k| @vectors[k].all? { |v| v.nil? || v.is_a?(Numeric) } }
  end
end

Instance Attribute Details

#categorical_columnsObject (readonly)

Returns the value of attribute categorical_columns.



3
4
5
# File 'lib/outliertree/dataset.rb', line 3

def categorical_columns
  @categorical_columns
end

#numeric_columnsObject (readonly)

Returns the value of attribute numeric_columns.



3
4
5
# File 'lib/outliertree/dataset.rb', line 3

def numeric_columns
  @numeric_columns
end

Instance Method Details

#[](k) ⇒ Object



27
28
29
# File 'lib/outliertree/dataset.rb', line 27

def [](k)
  @vectors[k]
end

#sizeObject



31
32
33
# File 'lib/outliertree/dataset.rb', line 31

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