Module: DaruLite::Vector::Fetchable

Included in:
DaruLite::Vector
Defined in:
lib/daru_lite/vector/fetchable.rb

Instance Method Summary collapse

Instance Method Details

#[](*input_indexes) ⇒ Object

Get one or more elements with specified index or a range.

Usage

# For vectors employing single layer Index

v[:one, :two] # => DaruLite::Vector with indexes :one and :two
v[:one]       # => Single element
v[:one..:three] # => DaruLite::Vector with indexes :one, :two and :three

# For vectors employing hierarchial multi index


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/daru_lite/vector/fetchable.rb', line 15

def [](*input_indexes)
  # Get array of positions indexes
  positions = @index.pos(*input_indexes)

  # If one object is asked return it
  return @data[positions] if positions.is_a? Numeric

  # Form a new Vector using positional indexes
  DaruLite::Vector.new(
    positions.map { |loc| @data[loc] },
    name: @name,
    index: @index.subset(*input_indexes), dtype: @dtype
  )
end

#at(*positions) ⇒ object

Returns vector of values given positional values

Examples:

dv = DaruLite::Vector.new 'a'..'e'
dv.at 0, 1, 2
# => #<DaruLite::Vector(3)>
#   0   a
#   1   b
#   2   c

Parameters:

  • positions (Array<object>)

    positional values

Returns:

  • (object)

    vector



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/daru_lite/vector/fetchable.rb', line 40

def at(*positions)
  # to be used to form index
  original_positions = positions
  positions = coerce_positions(*positions)
  validate_positions(*positions)

  if positions.is_a? Integer
    @data[positions]
  else
    values = positions.map { |pos| @data[pos] }
    DaruLite::Vector.new values, index: @index.at(*original_positions), dtype: dtype
  end
end

#cut(partitions, opts = {}) ⇒ DaruLite::Vector

Partition a numeric variable into categories.

Examples:

heights = DaruLite::Vector.new [30, 35, 32, 50, 42, 51]
height_cat = heights.cut [30, 40, 50, 60], labels=['low', 'medium', 'high']
# => #<DaruLite::Vector(6)>
#       0    low
#       1    low
#       2    low
#       3   high
#       4 medium
#       5   high

Parameters:

  • partitions (Array<Numeric>)

    an array whose consecutive elements provide intervals for categories

  • opts (Hash) (defaults to: {})

    options to cut the partition

Options Hash (opts):

  • :close_at (:left, :right)

    specifies whether the interval closes at the right side of left side

  • :labels (Array)

    names of the categories

Returns:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/daru_lite/vector/fetchable.rb', line 129

def cut(partitions, opts = {})
  close_at = opts[:close_at] || :right
  labels = opts[:labels]
  partitions = partitions.to_a
  values = to_a.map { |val| cut_find_category partitions, val, close_at }
  cats = cut_categories(partitions, close_at)

  dv = DaruLite::Vector.new values,
                            index: @index,
                            type: :category,
                            categories: cats

  # Rename categories if new labels provided
  if labels
    dv.rename_categories cats.zip(labels).to_h
  else
    dv
  end
end

#get_sub_vector(keys, by_position: true) ⇒ DaruLite::Vector

Parameters:

  • keys (Array)

    can be positions (if by_position is true) or indexes (if by_position if false)

Returns:



100
101
102
103
104
105
106
107
108
109
# File 'lib/daru_lite/vector/fetchable.rb', line 100

def get_sub_vector(keys, by_position: true)
  return DaruLite::Vector.new([]) if keys == []

  keys = @index.pos(*keys) unless by_position

  sub_vect = at(*keys)
  sub_vect = DaruLite::Vector.new([sub_vect]) unless sub_vect.is_a?(DaruLite::Vector)

  sub_vect
end

#head(q = 10) ⇒ Object



54
55
56
# File 'lib/daru_lite/vector/fetchable.rb', line 54

def head(q = 10)
  self[0..(q - 1)]
end

#last(q = 1) ⇒ Object



63
64
65
66
# File 'lib/daru_lite/vector/fetchable.rb', line 63

def last(q = 1)
  # The Enumerable mixin dose not provide the last method.
  tail(q)
end

#positions(*values) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/daru_lite/vector/fetchable.rb', line 149

def positions(*values)
  case values
  when [nil]
    nil_positions
  when [Float::NAN]
    nan_positions
  when [nil, Float::NAN], [Float::NAN, nil]
    nil_positions + nan_positions
  else
    size.times.select { |i| include_with_nan? values, @data[i] }
  end
end

#split_by_separator(sep = ',') ⇒ Object

Returns a hash of Vectors, defined by the different values defined on the fields Example:

a=DaruLite::Vector.new(["a,b","c,d","a,b"])
a.split_by_separator
=>  {"a"=>#<DaruLite::Vector:0x7f2dbcc09d88
      @data=[1, 0, 1]>,
     "b"=>#<DaruLite::Vector:0x7f2dbcc09c48
      @data=[1, 1, 0]>,
    "c"=>#<DaruLite::Vector:0x7f2dbcc09b08
      @data=[0, 1, 1]>}


81
82
83
84
85
86
87
88
89
90
# File 'lib/daru_lite/vector/fetchable.rb', line 81

def split_by_separator(sep = ',')
  split_data = splitted sep
  split_data
    .flatten.uniq.compact.to_h do |key|
    [
      key,
      DaruLite::Vector.new(split_data.map { |v| split_value(key, v) })
    ]
  end
end

#split_by_separator_freq(sep = ',') ⇒ Object



92
93
94
95
96
# File 'lib/daru_lite/vector/fetchable.rb', line 92

def split_by_separator_freq(sep = ',')
  split_by_separator(sep).transform_values do |v|
    v.sum(&:to_i)
  end
end

#tail(q = 10) ⇒ Object



58
59
60
61
# File 'lib/daru_lite/vector/fetchable.rb', line 58

def tail(q = 10)
  start = [size - q, 0].max
  self[start..(size - 1)]
end