Class: Rover::Vector
- Inherits:
-
Object
- Object
- Rover::Vector
- Defined in:
- lib/rover/vector.rb
Constant Summary collapse
- TYPE_CAST_MAPPING =
if a user never specifies types, the defaults are bool, float, int, and object keep these simple
we could create aliases for float64, int64, uint64 if so, type should still return the simple type
{ bool: Numo::Bit, float32: Numo::SFloat, float: Numo::DFloat, int8: Numo::Int8, int16: Numo::Int16, int32: Numo::Int32, int: Numo::Int64, object: Numo::RObject, uint8: Numo::UInt8, uint16: Numo::UInt16, uint32: Numo::UInt32, uint: Numo::UInt64 }
Instance Method Summary collapse
- #! ⇒ Object
- #-@ ⇒ Object
- #[](v) ⇒ Object
- #[]=(k, v) ⇒ Object
- #abs ⇒ Object
- #all?(&block) ⇒ Boolean
- #any?(&block) ⇒ Boolean
- #clamp(min, max) ⇒ Object
- #clamp!(min, max) ⇒ Object
- #crosstab(other) ⇒ Object
-
#diff ⇒ Object
keep same number of rows as original to make it easy to add to original data frame.
- #each(&block) ⇒ Object
- #each_with_index(&block) ⇒ Object
- #first(n = 1) ⇒ Object
- #head(n = 5) ⇒ Object
- #in?(values) ⇒ Boolean
-
#initialize(data, type: nil) ⇒ Vector
constructor
A new instance of Vector.
-
#inspect ⇒ Object
(also: #to_s)
TODO add type and size?.
- #last(n = 1) ⇒ Object
- #map(&block) ⇒ Object
- #max ⇒ Object
- #mean ⇒ Object
- #median ⇒ Object
- #min ⇒ Object
- #missing ⇒ Object
- #numeric? ⇒ Boolean
- #one_hot(drop: false, prefix: nil) ⇒ Object
- #percentile(q) ⇒ Object
- #size ⇒ Object (also: #length, #count)
- #sort ⇒ Object
-
#std ⇒ Object
uses Bessel’s correction for now since that’s all Numo supports.
- #sum ⇒ Object
- #tail(n = 5) ⇒ Object
- #take(n) ⇒ Object
- #tally ⇒ Object
- #to(type) ⇒ Object
- #to_a ⇒ Object
-
#to_html ⇒ Object
for IRuby.
- #to_numo ⇒ Object
- #type ⇒ Object
- #uniq ⇒ Object
-
#var ⇒ Object
uses Bessel’s correction for now since that’s all Numo supports.
- #zip(other, &block) ⇒ Object
Constructor Details
#initialize(data, type: nil) ⇒ Vector
Returns a new instance of Vector.
24 25 26 27 |
# File 'lib/rover/vector.rb', line 24 def initialize(data, type: nil) @data = cast_data(data, type: type) raise ArgumentError, "Bad size: #{@data.shape}" unless @data.ndim == 1 end |
Instance Method Details
#! ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/rover/vector.rb', line 142 def ! if @data.is_a?(Numo::Bit) Vector.new(@data.eq(0)) else raise "Not implemented yet" end end |
#-@ ⇒ Object
150 151 152 |
# File 'lib/rover/vector.rb', line 150 def -@ self * -1 end |
#[](v) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/rover/vector.rb', line 81 def [](v) if v.is_a?(Vector) Vector.new(v.to_numo.mask(@data)) else @data[v] end end |
#[]=(k, v) ⇒ Object
89 90 91 92 |
# File 'lib/rover/vector.rb', line 89 def []=(k, v) k = k.to_numo if k.is_a?(Vector) @data[k] = v end |
#abs ⇒ Object
182 183 184 |
# File 'lib/rover/vector.rb', line 182 def abs Vector.new(@data.abs) end |
#all?(&block) ⇒ Boolean
232 233 234 |
# File 'lib/rover/vector.rb', line 232 def all?(&block) to_a.all?(&block) end |
#any?(&block) ⇒ Boolean
236 237 238 |
# File 'lib/rover/vector.rb', line 236 def any?(&block) to_a.any?(&block) end |
#clamp(min, max) ⇒ Object
159 160 161 |
# File 'lib/rover/vector.rb', line 159 def clamp(min, max) dup.clamp!(min, max) end |
#clamp!(min, max) ⇒ Object
154 155 156 157 |
# File 'lib/rover/vector.rb', line 154 def clamp!(min, max) @data = @data.clip(min, max) self end |
#crosstab(other) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/rover/vector.rb', line 261 def crosstab(other) index = uniq.sort index_pos = index.to_a.map.with_index.to_h df = DataFrame.new({"_" => index}) other.uniq.sort.each do |k| df[k] = 0 end to_a.zip(other.to_a) do |v1, v2| df[v2][index_pos[v1]] += 1 end df end |
#diff ⇒ Object
keep same number of rows as original to make it easy to add to original data frame
76 77 78 79 |
# File 'lib/rover/vector.rb', line 76 def diff diff = @data.cast_to(Numo::DFloat).diff Vector.new(diff.insert(0, Float::NAN)) end |
#each(&block) ⇒ Object
186 187 188 |
# File 'lib/rover/vector.rb', line 186 def each(&block) @data.each(&block) end |
#each_with_index(&block) ⇒ Object
190 191 192 |
# File 'lib/rover/vector.rb', line 190 def each_with_index(&block) @data.each_with_index(&block) end |
#first(n = 1) ⇒ Object
244 245 246 247 248 249 250 |
# File 'lib/rover/vector.rb', line 244 def first(n = 1) if n >= size Vector.new(@data) else Vector.new(@data[0...n]) end end |
#head(n = 5) ⇒ Object
274 275 276 277 |
# File 'lib/rover/vector.rb', line 274 def head(n = 5) n += size if n < 0 first(n) end |
#in?(values) ⇒ Boolean
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rover/vector.rb', line 128 def in?(values) ret = Numo::Bit.new(size).fill(false) values.each do |v| comp = if v.is_a?(Numeric) || v.is_a?(Numo::NArray) @data.eq(v) else Numo::Bit.cast(@data.map { |d| d == v }) end ret |= comp end Vector.new(ret) end |
#inspect ⇒ Object Also known as: to_s
TODO add type and size?
299 300 301 302 303 |
# File 'lib/rover/vector.rb', line 299 def inspect elements = first(5).to_a.map(&:inspect) elements << "..." if size > 5 "#<Rover::Vector [#{elements.join(", ")}]>" end |
#last(n = 1) ⇒ Object
252 253 254 |
# File 'lib/rover/vector.rb', line 252 def last(n = 1) Vector.new(@data[-n..-1]) end |
#map(&block) ⇒ Object
163 164 165 166 167 |
# File 'lib/rover/vector.rb', line 163 def map(&block) mapped = @data.map(&block) mapped = mapped.to_a if mapped.is_a?(Numo::RObject) # re-evaluate cast Vector.new(mapped) end |
#max ⇒ Object
194 195 196 |
# File 'lib/rover/vector.rb', line 194 def max @data.max end |
#mean ⇒ Object
202 203 204 205 206 |
# File 'lib/rover/vector.rb', line 202 def mean # currently only floats have mean in Numo # https://github.com/ruby-numo/numo-narray/issues/79 @data.cast_to(Numo::DFloat).mean end |
#median ⇒ Object
208 209 210 211 212 |
# File 'lib/rover/vector.rb', line 208 def median # need to cast to get correct result # https://github.com/ruby-numo/numo-narray/issues/165 @data.cast_to(Numo::DFloat).median end |
#min ⇒ Object
198 199 200 |
# File 'lib/rover/vector.rb', line 198 def min @data.min end |
#missing ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rover/vector.rb', line 61 def missing bit = if @data.is_a?(Numo::RObject) Numo::Bit.cast(@data.map(&:nil?)) elsif @data.respond_to?(:isnan) @data.isnan else Numo::Bit.new(size).fill(0) end Vector.new(bit) end |
#numeric? ⇒ Boolean
47 48 49 |
# File 'lib/rover/vector.rb', line 47 def numeric? ![:object, :bool].include?(type) end |
#one_hot(drop: false, prefix: nil) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/rover/vector.rb', line 284 def one_hot(drop: false, prefix: nil) raise ArgumentError, "All elements must be strings" unless all? { |vi| vi.is_a?(String) } new_vectors = {} # maybe sort values first values = uniq.to_a values.shift if drop values.each do |v2| # TODO use types new_vectors["#{prefix}#{v2}"] = (self == v2).to_numo.cast_to(Numo::Int64) end DataFrame.new(new_vectors) end |
#percentile(q) ⇒ Object
214 215 216 |
# File 'lib/rover/vector.rb', line 214 def percentile(q) @data.percentile(q) end |
#size ⇒ Object Also known as: length, count
51 52 53 |
# File 'lib/rover/vector.rb', line 51 def size @data.size end |
#sort ⇒ Object
178 179 180 |
# File 'lib/rover/vector.rb', line 178 def sort Vector.new(@data.respond_to?(:sort) ? @data.sort : @data.to_a.sort) end |
#std ⇒ Object
uses Bessel’s correction for now since that’s all Numo supports
223 224 225 |
# File 'lib/rover/vector.rb', line 223 def std @data.cast_to(Numo::DFloat).stddev end |
#sum ⇒ Object
218 219 220 |
# File 'lib/rover/vector.rb', line 218 def sum @data.sum end |
#tail(n = 5) ⇒ Object
279 280 281 282 |
# File 'lib/rover/vector.rb', line 279 def tail(n = 5) n += size if n < 0 last(n) end |
#take(n) ⇒ Object
256 257 258 259 |
# File 'lib/rover/vector.rb', line 256 def take(n) raise ArgumentError, "attempt to take negative size" if n < 0 first(n) end |
#tally ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/rover/vector.rb', line 169 def tally result = Hash.new(0) @data.each do |v| result[v] += 1 end result.default = nil result end |
#to(type) ⇒ Object
33 34 35 |
# File 'lib/rover/vector.rb', line 33 def to(type) Vector.new(self, type: type) end |
#to_a ⇒ Object
41 42 43 44 45 |
# File 'lib/rover/vector.rb', line 41 def to_a a = @data.to_a a.map! { |v| !v.zero? } if @data.is_a?(Numo::Bit) a end |
#to_html ⇒ Object
for IRuby
307 308 309 310 |
# File 'lib/rover/vector.rb', line 307 def to_html require "iruby" IRuby::HTML.table(to_a) end |
#to_numo ⇒ Object
37 38 39 |
# File 'lib/rover/vector.rb', line 37 def to_numo @data end |
#type ⇒ Object
29 30 31 |
# File 'lib/rover/vector.rb', line 29 def type TYPE_CAST_MAPPING.find { |_, v| @data.is_a?(v) }[0] end |
#uniq ⇒ Object
57 58 59 |
# File 'lib/rover/vector.rb', line 57 def uniq Vector.new(to_a.uniq) end |
#var ⇒ Object
uses Bessel’s correction for now since that’s all Numo supports
228 229 230 |
# File 'lib/rover/vector.rb', line 228 def var @data.cast_to(Numo::DFloat).var end |
#zip(other, &block) ⇒ Object
240 241 242 |
# File 'lib/rover/vector.rb', line 240 def zip(other, &block) to_a.zip(other.to_a, &block) end |