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
- #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
- #reject(&block) ⇒ Object
- #select(&block) ⇒ 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 |
#all?(&block) ⇒ Boolean
245 246 247 |
# File 'lib/rover/vector.rb', line 245 def all?(&block) to_a.all?(&block) end |
#any?(&block) ⇒ Boolean
249 250 251 |
# File 'lib/rover/vector.rb', line 249 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
278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/rover/vector.rb', line 278 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
199 200 201 |
# File 'lib/rover/vector.rb', line 199 def each(&block) @data.each(&block) end |
#each_with_index(&block) ⇒ Object
203 204 205 |
# File 'lib/rover/vector.rb', line 203 def each_with_index(&block) @data.each_with_index(&block) end |
#first(n = 1) ⇒ Object
257 258 259 260 261 262 263 |
# File 'lib/rover/vector.rb', line 257 def first(n = 1) if n >= size Vector.new(@data) else Vector.new(@data[0...n]) end end |
#head(n = 5) ⇒ Object
291 292 293 294 |
# File 'lib/rover/vector.rb', line 291 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?
316 317 318 319 320 |
# File 'lib/rover/vector.rb', line 316 def inspect elements = first(5).to_a.map(&:inspect) elements << "..." if size > 5 "#<Rover::Vector [#{elements.join(", ")}]>" end |
#last(n = 1) ⇒ Object
265 266 267 268 269 270 271 |
# File 'lib/rover/vector.rb', line 265 def last(n = 1) if n >= size Vector.new(@data) else Vector.new(@data[-n..-1]) end end |
#map(&block) ⇒ Object
163 164 165 166 167 |
# File 'lib/rover/vector.rb', line 163 def map(&block) # convert to Ruby first to cast properly # https://github.com/ruby-numo/numo-narray/issues/181 Vector.new(@data.to_a.map(&block)) end |
#map!(&block) ⇒ Object
169 170 171 172 |
# File 'lib/rover/vector.rb', line 169 def map!(&block) @data = cast_data(@data.to_a.map(&block)) self end |
#max ⇒ Object
207 208 209 |
# File 'lib/rover/vector.rb', line 207 def max @data.max end |
#mean ⇒ Object
215 216 217 218 219 |
# File 'lib/rover/vector.rb', line 215 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
221 222 223 224 225 |
# File 'lib/rover/vector.rb', line 221 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
211 212 213 |
# File 'lib/rover/vector.rb', line 211 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
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/rover/vector.rb', line 301 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
227 228 229 |
# File 'lib/rover/vector.rb', line 227 def percentile(q) @data.percentile(q) end |
#reject(&block) ⇒ Object
178 179 180 |
# File 'lib/rover/vector.rb', line 178 def reject(&block) Vector.new(@data.to_a.reject(&block)) end |
#select(&block) ⇒ Object
174 175 176 |
# File 'lib/rover/vector.rb', line 174 def select(&block) Vector.new(@data.to_a.select(&block)) 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
191 192 193 |
# File 'lib/rover/vector.rb', line 191 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
236 237 238 |
# File 'lib/rover/vector.rb', line 236 def std @data.cast_to(Numo::DFloat).stddev end |
#sum ⇒ Object
231 232 233 |
# File 'lib/rover/vector.rb', line 231 def sum @data.sum end |
#tail(n = 5) ⇒ Object
296 297 298 299 |
# File 'lib/rover/vector.rb', line 296 def tail(n = 5) n += size if n < 0 last(n) end |
#take(n) ⇒ Object
273 274 275 276 |
# File 'lib/rover/vector.rb', line 273 def take(n) raise ArgumentError, "attempt to take negative size" if n < 0 first(n) end |
#tally ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/rover/vector.rb', line 182 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
324 325 326 327 328 329 330 331 332 |
# File 'lib/rover/vector.rb', line 324 def to_html require "iruby" if size > 7 # pass 8 rows so maxrows is applied IRuby::HTML.table(first(4).to_a + last(4).to_a, maxrows: 7) else IRuby::HTML.table(to_a) end 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 |
#var ⇒ Object
uses Bessel’s correction for now since that’s all Numo supports
241 242 243 |
# File 'lib/rover/vector.rb', line 241 def var @data.cast_to(Numo::DFloat).var end |
#zip(other, &block) ⇒ Object
253 254 255 |
# File 'lib/rover/vector.rb', line 253 def zip(other, &block) to_a.zip(other.to_a, &block) end |