Class: DaruLite::Vector
- Extended by:
- Gem::Deprecate
- Includes:
- Maths::Arithmetic::Vector, Maths::Statistics::Vector, Enumerable
- Defined in:
- lib/daru_lite/vector.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- DEFAULT_SORTER =
lambda { |(lv, li), (rv, ri)| if lv.nil? && rv.nil? li <=> ri elsif lv.nil? -1 elsif rv.nil? 1 else lv <=> rv end }
- DATE_REGEXP =
/^(\d{2}-\d{2}-\d{4}|\d{4}-\d{2}-\d{2})$/.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Store vector data in an array.
-
#dtype ⇒ Object
readonly
The underlying dtype of the Vector.
-
#index ⇒ Object
The row index.
-
#labels ⇒ Object
Store a hash of labels for values.
-
#missing_positions ⇒ Object
readonly
An Array or the positions in the vector that are being treated as ‘missing’.
-
#name ⇒ Object
readonly
The name of the DaruLite::Vector.
-
#nm_dtype ⇒ Object
readonly
Returns the value of attribute nm_dtype.
Class Method Summary collapse
-
.[](*indexes) ⇒ Object
Create a vector using (almost) any object * Array: flattened * Range: transformed using to_a * DaruLite::Vector * Numeric and string values.
-
._load(data) ⇒ Object
:nodoc:.
- .coerce(data, options = {}) ⇒ Object
-
.new_with_size(n, opts = {}, &block) ⇒ Object
Create a new vector by specifying the size and an optional value and block to generate values.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two vectors are equal if they have the exact same index values corresponding with the exact same elements.
-
#[](*input_indexes) ⇒ Object
Get one or more elements with specified index or a range.
-
#[]=(*indexes, val) ⇒ Object
Just like in Hashes, you can specify the index label of the DaruLite::Vector and assign an element an that place in the DaruLite::Vector.
-
#_dump ⇒ Object
:nodoc:.
- #all?(&block) ⇒ Boolean
- #any?(&block) ⇒ Boolean
- #apply_method(method, keys: nil, by_position: true) ⇒ Object (also: #apply_method_on_sub_vector)
-
#apply_where(bool_array, &block) ⇒ DaruLite::Vector
Return a new vector based on the contents of a boolean array and &block.
-
#at(*positions) ⇒ object
Returns vector of values given positional values.
-
#bootstrap(estimators, nr, s = nil) ⇒ Object
Bootstrap Generate
nr
resamples (with replacement) of sizes
from vector, computing each estimate fromestimators
over each resample. -
#cast(opts = {}) ⇒ Object
Cast a vector to a new data type.
-
#category? ⇒ true, false
Tells if vector is categorical or not.
-
#clone_structure ⇒ Object
Copies the structure of the vector (i.e the index, size, etc.) and fills all all values with nils.
-
#concat(element, index) ⇒ Object
(also: #push, #<<)
Append an element to the vector by specifying the element and index.
-
#count_values(*values) ⇒ Integer
Count the number of values specified.
-
#cut(partitions, opts = {}) ⇒ DaruLite::Vector
Partition a numeric variable into categories.
-
#daru_lite_vector ⇒ Object
(also: #dv)
:nocov:.
-
#db_type ⇒ Object
Returns the database type for the vector, according to its content.
-
#delete(element) ⇒ Object
Delete an element by value.
-
#delete_at(index) ⇒ Object
Delete element by index.
-
#delete_if ⇒ Object
Delete an element if block returns true.
- #detach_index ⇒ Object
-
#dup ⇒ DaruLite::Vector
Duplicated a vector.
- #each(&block) ⇒ Object
- #each_index(&block) ⇒ Object
- #each_with_index(&block) ⇒ Object
- #empty? ⇒ Boolean
- #get_sub_vector(keys, by_position: true) ⇒ DaruLite::Vector
- #group_by(*args) ⇒ Object
-
#has_index?(index) ⇒ Boolean
Returns true if an index exists.
-
#has_missing_data? ⇒ Boolean
(also: #flawed?)
Reports whether missing data is present in the Vector.
- #head(q = 10) ⇒ Object
-
#in(other) ⇒ Object
Comparator for checking if any of the elements in other exist in self.
-
#include_values?(*values) ⇒ true, false
Check if any one of mentioned values occur in the vector.
-
#index_of(element) ⇒ Object
Get index of element.
-
#indexes(*values) ⇒ Array
Return indexes of values specified.
-
#initialize(source, opts = {}) ⇒ Vector
constructor
Create a Vector object.
-
#inspect(spacing = 20, threshold = 15) ⇒ Object
Over rides original inspect for pretty printing in irb.
-
#is_values(*values) ⇒ DaruLite::Vector
Return vector of booleans with value at ith position is either true or false depending upon whether value at position i is equal to any of the values passed in the argument or not.
-
#jackknife(estimators, k = 1) ⇒ Object
Jacknife Returns a dataset with jacknife delete-
k
estimators
estimators
could be: a) Hash with variable names as keys and lambdas as values a.jacknife(:log_s2=>lambda {|v| Math.log(v.variance)}) b) Array with method names to jacknife a.jacknife([:mean, :sd]) c) A single method to jacknife a.jacknife(:mean)k
represent the block size for block jacknife. -
#keep_if ⇒ Object
Keep an element if block returns true.
-
#lag(k = 1) ⇒ DaruLite::Vector
Lags the series by ‘k` periods.
- #last(q = 1) ⇒ Object
- #map!(&block) ⇒ Object
-
#match(regexp) ⇒ Array
Returns an array of either none or integer values, indicating the
regexp
matching with the given array. - #method_missing(name, *args, &block) ⇒ Object
-
#n_valid ⇒ Object
number of non-missing elements.
- #numeric? ⇒ Boolean
-
#numeric_summary ⇒ String
Displays summary for an numeric type Vector.
- #object? ⇒ Boolean
-
#object_summary ⇒ String
Displays summary for an object type Vector.
-
#only_missing(as_a = :vector) ⇒ Object
Returns a Vector containing only missing data (preserves indexes).
-
#only_numerics ⇒ Object
Returns a Vector with only numerical data.
-
#only_valid(as_a = :vector, _duplicate = true) ⇒ Object
Creates a new vector consisting only of non-nil data.
- #positions(*values) ⇒ Object
-
#recode(dt = nil, &block) ⇒ Object
Like map, but returns a DaruLite::Vector with the returned values.
-
#recode!(dt = nil, &block) ⇒ Object
Destructive version of recode!.
-
#reindex(new_index) ⇒ Object
Create a new vector with a different index, and preserve the indexing of current elements.
-
#reindex!(new_index) ⇒ DaruLite::Vector
Sets new index for vector.
-
#reject_values(*values) ⇒ DaruLite::Vector
Return a vector with specified values removed.
-
#rename(new_name) ⇒ Object
(also: #name=)
Give the vector a new name.
-
#reorder(order) ⇒ Object
Non-destructive version of #reorder!.
-
#reorder!(order) ⇒ Object
Reorder the vector with given positions.
-
#replace_nils(replacement) ⇒ Object
Non-destructive version of #replace_nils!.
-
#replace_nils!(replacement) ⇒ Object
Replace all nils in the vector with the value passed as an argument.
-
#replace_values(old_values, new_value) ⇒ DaruLite::Vector
Replaces specified values with a new value.
- #reset_index! ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#rolling_fillna(direction = :forward) ⇒ Object
Non-destructive version of rolling_fillna!.
-
#rolling_fillna!(direction = :forward) ⇒ Object
Rolling fillna replace all Float::NAN and NIL values with the preceeding or following value.
-
#save(filename) ⇒ Object
Save the vector to a file.
-
#set_at(positions, val) ⇒ Object
Change value at given positions.
- #size ⇒ Object
-
#sort(opts = {}, &block) ⇒ Object
Sorts a vector according to its values.
-
#sort_by_index(opts = {}) ⇒ Vector
Sorts the vector according to it’s`Index` values.
-
#sorted_data(&block) ⇒ Object
Just sort the data and get an Array in return using Enumerable#sort.
-
#split_by_separator(sep = ',') ⇒ Object
Returns a hash of Vectors, defined by the different values defined on the fields Example:.
- #split_by_separator_freq(sep = ',') ⇒ Object
-
#splitted(sep = ',') ⇒ Object
Return an Array with the data splitted by a separator.
-
#summary(indent_level = 0) ⇒ String
Create a summary of the Vector.
- #tail(q = 10) ⇒ Object
-
#to_a ⇒ Object
Return an array.
-
#to_category(opts = {}) ⇒ DaruLite::Vector
Converts a non category type vector to category type vector.
-
#to_df ⇒ DaruLite::DataFrame
The vector as a single-vector dataframe.
-
#to_h ⇒ Object
Convert to hash (explicit).
-
#to_html(threshold = 30) ⇒ Object
Convert to html for iruby.
- #to_html_tbody(threshold = 30) ⇒ Object
- #to_html_thead ⇒ Object
-
#to_json ⇒ Object
Convert the hash from to_h to json.
-
#to_matrix(axis = :horizontal) ⇒ Object
Convert Vector to a horizontal or vertical Ruby Matrix.
- #to_s ⇒ Object
-
#type ⇒ Object
The type of data contained in the vector.
-
#uniq ⇒ Object
Keep only unique elements of the vector alongwith their indexes.
-
#verify ⇒ Object
Reports all values that doesn’t comply with a condition.
-
#where(bool_array) ⇒ Object
Return a new vector based on the contents of a boolean array.
Methods included from Maths::Statistics::Vector
#acf, #acvf, #average_deviation_population, #box_cox_transformation, #center, #coefficient_of_variation, #count, #covariance_population, #covariance_sample, #cumsum, #describe, #dichotomize, #diff, #ema, #emsd, #emv, #factors, #frequencies, #index_of_max, #index_of_max_by, #index_of_min, #index_of_min_by, #kurtosis, #macd, #max, #max_by, #max_index, #mean, #median, #median_absolute_deviation, #min, #min_by, #mode, #percent_change, #percentile, #product, #proportion, #proportions, #range, #ranked, #rolling, #rolling_count, #rolling_max, #rolling_mean, #rolling_median, #rolling_min, #rolling_std, #rolling_sum, #rolling_variance, #sample_with_replacement, #sample_without_replacement, #skew, #standard_deviation_population, #standard_deviation_sample, #standard_error, #standardize, #sum, #sum_of_squared_deviation, #sum_of_squares, #value_counts, #variance_population, #variance_sample, #vector_centered_compute, #vector_percentile, #vector_standardized_compute
Methods included from Maths::Arithmetic::Vector
#%, #*, #**, #+, #-, #/, #abs, #add, #exp, #round, #sqrt
Constructor Details
#initialize(source, opts = {}) ⇒ Vector
Create a Vector object.
Arguments
Hash. If Array, a numeric index will be created if not supplied in the options. Specifying more index elements than actual values in source will insert nil into the surplus index elements. When a Hash is specified, the keys of the Hash are taken as the index elements and the corresponding values as the values that populate the vector.
Options
-
:name
- Name of the vector -
:index
- Index of the vector -
:dtype
- The underlying data type. Can be :array.
Default :array.
-
:missing_values
- An Array of the values that are to be treated as ‘missing’.
nil is the default missing value.
Usage
vecarr = DaruLite::Vector.new [1,2,3,4], index: [:a, :e, :i, :o]
vechsh = DaruLite::Vector.new({a: 1, e: 2, i: 3, o: 4})
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/daru_lite/vector.rb', line 177 def initialize(source, opts = {}) if opts[:type] == :category # Initialize category type vector extend DaruLite::Category initialize_category source, opts else # Initialize non-category type vector initialize_vector source, opts end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 |
# File 'lib/daru_lite/vector.rb', line 1383 def method_missing(name, *args, &block) # FIXME: it is shamefully fragile. Should be either made stronger # (string/symbol dychotomy, informative errors) or removed totally. - zverok if name =~ /(.+)=/ self[Regexp.last_match(1).to_sym] = args[0] elsif has_index?(name) self[name] else super end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Store vector data in an array
148 149 150 |
# File 'lib/daru_lite/vector.rb', line 148 def data @data end |
#dtype ⇒ Object (readonly)
The underlying dtype of the Vector. Can be :array.
138 139 140 |
# File 'lib/daru_lite/vector.rb', line 138 def dtype @dtype end |
#index ⇒ Object
The row index. Can be either DaruLite::Index or DaruLite::MultiIndex.
136 137 138 |
# File 'lib/daru_lite/vector.rb', line 136 def index @index end |
#labels ⇒ Object
Store a hash of labels for values. Supplementary only. Recommend using index for proper usage.
146 147 148 |
# File 'lib/daru_lite/vector.rb', line 146 def labels @labels end |
#missing_positions ⇒ Object (readonly)
An Array or the positions in the vector that are being treated as ‘missing’.
141 142 143 |
# File 'lib/daru_lite/vector.rb', line 141 def missing_positions @missing_positions end |
#name ⇒ Object (readonly)
The name of the DaruLite::Vector. String.
134 135 136 |
# File 'lib/daru_lite/vector.rb', line 134 def name @name end |
#nm_dtype ⇒ Object (readonly)
Returns the value of attribute nm_dtype.
139 140 141 |
# File 'lib/daru_lite/vector.rb', line 139 def nm_dtype @nm_dtype end |
Class Method Details
.[](*indexes) ⇒ Object
Create a vector using (almost) any object
-
Array: flattened
-
Range: transformed using to_a
-
DaruLite::Vector
-
Numeric and string values
Description
The ‘Vector.[]` class method creates a vector from almost any object that has a `#to_a` method defined on it. It is similar to R’s ‘c` method.
Usage
a = DaruLite::Vector[1,2,3,4,6..10]
#=>
# <DaruLite::Vector:99448510 @name = nil @size = 9 >
# nil
# 0 1
# 1 2
# 2 3
# 3 4
# 4 6
# 5 7
# 6 8
# 7 9
# 8 10
62 63 64 65 66 67 |
# File 'lib/daru_lite/vector.rb', line 62 def [](*indexes) values = indexes.map do |a| a.respond_to?(:to_a) ? a.to_a : a end.flatten DaruLite::Vector.new(values) end |
._load(data) ⇒ Object
:nodoc:
69 70 71 72 73 74 75 |
# File 'lib/daru_lite/vector.rb', line 69 def _load(data) # :nodoc: h = Marshal.load(data) DaruLite::Vector.new(h[:data], index: h[:index], name: h[:name], dtype: h[:dtype], missing_values: h[:missing_values]) end |
.coerce(data, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/daru_lite/vector.rb', line 77 def coerce(data, = {}) case data when DaruLite::Vector data when Array, Hash new(data, ) else raise ArgumentError, "Can't coerce #{data.class} to #{self}" end end |
.new_with_size(n, opts = {}, &block) ⇒ Object
Create a new vector by specifying the size and an optional value and block to generate values.
Description
The new_with_size class method lets you create a DaruLite::Vector by specifying the size as the argument. The optional block, if supplied, is run once for populating each element in the Vector.
The result of each run of the block is the value that is ultimately assigned to that position in the Vector.
Options
:value All the rest like .new
29 30 31 32 33 |
# File 'lib/daru_lite/vector.rb', line 29 def new_with_size(n, opts = {}, &block) value = opts.delete :value block ||= ->(_) { value } DaruLite::Vector.new Array.new(n, &block), opts end |
Instance Method Details
#==(other) ⇒ Object
Two vectors are equal if they have the exact same index values corresponding with the exact same elements. Name is ignored.
282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/daru_lite/vector.rb', line 282 def ==(other) case other when DaruLite::Vector @index == other.index && size == other.size && each_with_index.with_index.all? do |(e, index), position| e == other.at(position) && index == other.index.to_a[position] end else super end end |
#[](*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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/daru_lite/vector.rb', line 199 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 |
#[]=(*indexes, val) ⇒ Object
270 271 272 273 274 275 276 277 278 |
# File 'lib/daru_lite/vector.rb', line 270 def []=(*indexes, val) cast(dtype: :array) if val.nil? && dtype != :array guard_type_check(val) modify_vector(indexes, val) update_position_cache end |
#_dump ⇒ Object
:nodoc:
1353 1354 1355 1356 1357 1358 1359 1360 |
# File 'lib/daru_lite/vector.rb', line 1353 def _dump(*) # :nodoc: Marshal.dump( data: @data.to_a, dtype: @dtype, name: @name, index: @index ) end |
#all?(&block) ⇒ Boolean
583 584 585 |
# File 'lib/daru_lite/vector.rb', line 583 def all?(&block) @data.data.all?(&block) end |
#any?(&block) ⇒ Boolean
579 580 581 |
# File 'lib/daru_lite/vector.rb', line 579 def any?(&block) @data.data.any?(&block) end |
#apply_method(method, keys: nil, by_position: true) ⇒ Object Also known as: apply_method_on_sub_vector
122 123 124 125 126 127 128 129 130 |
# File 'lib/daru_lite/vector.rb', line 122 def apply_method(method, keys: nil, by_position: true) vect = keys ? get_sub_vector(keys, by_position: by_position) : self case method when Symbol then vect.send(method) when Proc then method.call(vect) else raise end end |
#apply_where(bool_array, &block) ⇒ DaruLite::Vector
Return a new vector based on the contents of a boolean array and &block.
427 428 429 |
# File 'lib/daru_lite/vector.rb', line 427 def apply_where(bool_array, &block) DaruLite::Core::Query.vector_apply_where self, bool_array, &block end |
#at(*positions) ⇒ object
Returns vector of values given positional values
224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/daru_lite/vector.rb', line 224 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 |
#bootstrap(estimators, nr, s = nil) ⇒ Object
Bootstrap
Generate nr
resamples (with replacement) of size s
from vector, computing each estimate from estimators
over each resample. estimators
could be a) Hash with variable names as keys and lambdas as values
a.bootstrap(:log_s2=>lambda {|v| Math.log(v.variance)},1000)
b) Array with names of method to bootstrap
a.bootstrap([:mean, :sd],1000)
c) A single method to bootstrap
a.jacknife(:mean, 1000)
If s is nil, is set to vector size by default.
Returns a DataFrame where each vector is a vector of length nr
containing the computed resample estimates.
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 |
# File 'lib/daru_lite/vector.rb', line 1136 def bootstrap(estimators, nr, s = nil) s ||= size h_est, es, bss = prepare_bootstrap(estimators) nr.times do bs = sample_with_replacement(s) es.each do |estimator| bss[estimator].push(h_est[estimator].call(bs)) end end es.each do |est| bss[est] = DaruLite::Vector.new bss[est] end DaruLite::DataFrame.new bss end |
#cast(opts = {}) ⇒ Object
Cast a vector to a new data type.
Options
-
:dtype
- :array for Ruby Array..
514 515 516 517 518 519 |
# File 'lib/daru_lite/vector.rb', line 514 def cast(opts = {}) dt = opts[:dtype] raise ArgumentError, "Unsupported dtype #{opts[:dtype]}" unless dt == :array @data = cast_vector_to dt unless @dtype == dt end |
#category? ⇒ true, false
Tells if vector is categorical or not.
559 560 561 |
# File 'lib/daru_lite/vector.rb', line 559 def category? type == :category end |
#clone_structure ⇒ Object
Copies the structure of the vector (i.e the index, size, etc.) and fills all all values with nils.
1340 1341 1342 |
# File 'lib/daru_lite/vector.rb', line 1340 def clone_structure DaruLite::Vector.new(([nil] * size), name: @name, index: @index.dup) end |
#concat(element, index) ⇒ Object Also known as: push, <<
Append an element to the vector by specifying the element and index
498 499 500 501 502 503 504 505 |
# File 'lib/daru_lite/vector.rb', line 498 def concat(element, index) raise IndexError, 'Expected new unique index' if @index.include? index @index |= [index] @data[@index[index]] = element update_position_cache end |
#count_values(*values) ⇒ Integer
Count the number of values specified
873 874 875 |
# File 'lib/daru_lite/vector.rb', line 873 def count_values(*values) positions(*values).size end |
#cut(partitions, opts = {}) ⇒ DaruLite::Vector
Partition a numeric variable into categories.
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 |
# File 'lib/daru_lite/vector.rb', line 1417 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 |
#daru_lite_vector ⇒ Object Also known as: dv
:nocov:
1363 1364 1365 |
# File 'lib/daru_lite/vector.rb', line 1363 def daru_lite_vector(*) self end |
#db_type ⇒ Object
Returns the database type for the vector, according to its content
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 |
# File 'lib/daru_lite/vector.rb', line 1325 def db_type # first, detect any character not number if @data.any? { |v| v.to_s =~ DATE_REGEXP } 'DATE' elsif @data.any? { |v| v.to_s =~ /[^0-9e.-]/ } 'VARCHAR (255)' elsif @data.any? { |v| v.to_s.include?('.') } 'DOUBLE' else 'INTEGER' end end |
#delete(element) ⇒ Object
Delete an element by value
522 523 524 |
# File 'lib/daru_lite/vector.rb', line 522 def delete(element) delete_at index_of(element) end |
#delete_at(index) ⇒ Object
Delete element by index
527 528 529 530 531 532 |
# File 'lib/daru_lite/vector.rb', line 527 def delete_at(index) @data.delete_at @index[index] @index = DaruLite::Index.new(@index.to_a - [index]) update_position_cache end |
#delete_if ⇒ Object
Delete an element if block returns true. Destructive.
676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'lib/daru_lite/vector.rb', line 676 def delete_if return to_enum(:delete_if) unless block_given? keep_e, keep_i = each_with_index.reject { |n, _i| yield(n) }.transpose @data = cast_vector_to @dtype, keep_e @index = DaruLite::Index.new(keep_i) update_position_cache self end |
#detach_index ⇒ Object
848 849 850 851 852 853 |
# File 'lib/daru_lite/vector.rb', line 848 def detach_index DaruLite::DataFrame.new( index: @index.to_a, values: @data.to_a ) end |
#dup ⇒ DaruLite::Vector
Duplicated a vector
1117 1118 1119 |
# File 'lib/daru_lite/vector.rb', line 1117 def dup DaruLite::Vector.new @data.dup, name: @name, index: @index.dup end |
#each(&block) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/daru_lite/vector.rb', line 93 def each(&block) return to_enum(:each) unless block @data.each(&block) self end |
#each_index(&block) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/daru_lite/vector.rb', line 100 def each_index(&block) return to_enum(:each_index) unless block @index.each(&block) self end |
#each_with_index(&block) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/daru_lite/vector.rb', line 107 def each_with_index(&block) return to_enum(:each_with_index) unless block @data.to_a.zip(@index.to_a).each(&block) self end |
#empty? ⇒ Boolean
445 446 447 |
# File 'lib/daru_lite/vector.rb', line 445 def empty? @index.empty? end |
#get_sub_vector(keys, by_position: true) ⇒ DaruLite::Vector
884 885 886 887 888 889 890 891 892 893 |
# File 'lib/daru_lite/vector.rb', line 884 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 |
#group_by(*args) ⇒ Object
1450 1451 1452 |
# File 'lib/daru_lite/vector.rb', line 1450 def group_by(*args) to_df.group_by(*args) end |
#has_index?(index) ⇒ Boolean
Returns true if an index exists
878 879 880 |
# File 'lib/daru_lite/vector.rb', line 878 def has_index?(index) @index.include? index end |
#has_missing_data? ⇒ Boolean Also known as: flawed?
Reports whether missing data is present in the Vector.
458 459 460 |
# File 'lib/daru_lite/vector.rb', line 458 def has_missing_data? !indexes(*DaruLite::MISSING_VALUES).empty? end |
#head(q = 10) ⇒ Object
431 432 433 |
# File 'lib/daru_lite/vector.rb', line 431 def head(q = 10) self[0..(q - 1)] end |
#in(other) ⇒ Object
Comparator for checking if any of the elements in other exist in self.
361 362 363 364 365 366 367 368 |
# File 'lib/daru_lite/vector.rb', line 361 def in(other) other = other.zip(Array.new(other.size, 0)).to_h DaruLite::Core::Query::BoolArray.new( @data.each_with_object([]) do |d, memo| memo << (other.key?(d)) end ) end |
#include_values?(*values) ⇒ true, false
Check if any one of mentioned values occur in the vector
473 474 475 |
# File 'lib/daru_lite/vector.rb', line 473 def include_values?(*values) values.any? { |v| include_with_nan? @data, v } end |
#index_of(element) ⇒ Object
Get index of element
564 565 566 567 568 569 |
# File 'lib/daru_lite/vector.rb', line 564 def index_of(element) case dtype when :array then @index.key(@data.index { |x| x.eql? element }) else @index.key @data.index(element) end end |
#indexes(*values) ⇒ Array
Return indexes of values specified
1271 1272 1273 |
# File 'lib/daru_lite/vector.rb', line 1271 def indexes(*values) index.to_a.values_at(*positions(*values)) end |
#inspect(spacing = 20, threshold = 15) ⇒ Object
Over rides original inspect for pretty printing in irb
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
# File 'lib/daru_lite/vector.rb', line 1030 def inspect(spacing = 20, threshold = 15) row_headers = index.is_a?(MultiIndex) ? index.sparse_tuples : index.to_a "#<#{self.class}(#{size})#{':category' if category?}>\n" + Formatters::Table.format( to_a.lazy.map { |v| [v] }, headers: @name && [@name], row_headers: row_headers, threshold: threshold, spacing: spacing ) end |
#is_values(*values) ⇒ DaruLite::Vector
Do not use it to check for Float::NAN as Float::NAN == Float::NAN is false
Return vector of booleans with value at ith position is either true or false depending upon whether value at position i is equal to any of the values passed in the argument or not
493 494 495 |
# File 'lib/daru_lite/vector.rb', line 493 def is_values(*values) DaruLite::Vector.new values.map { |v| eq(v) }.inject(:|) end |
#jackknife(estimators, k = 1) ⇒ Object
Jacknife
Returns a dataset with jacknife delete-k
estimators
estimators
could be: a) Hash with variable names as keys and lambdas as values
a.jacknife(:log_s2=>lambda {|v| Math.log(v.variance)})
b) Array with method names to jacknife
a.jacknife([:mean, :sd])
c) A single method to jacknife
a.jacknife(:mean)
k
represent the block size for block jacknife. By default is set to 1, for classic delete-one jacknife.
Returns a dataset where each vector is an vector of length cases
/k
containing the computed jacknife estimates.
Reference:
-
Sawyer, S. (2005). Resampling Data: Using a Statistical Jacknife.
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 |
# File 'lib/daru_lite/vector.rb', line 1171 def jackknife(estimators, k = 1) # rubocop:disable Metrics/MethodLength raise "n should be divisible by k:#{k}" unless (size % k).zero? nb = (size / k).to_i h_est, es, ps = prepare_bootstrap(estimators) est_n = es.to_h { |v| [v, h_est[v].call(self)] } nb.times do |i| other = @data.dup other.slice!(i * k, k) other = DaruLite::Vector.new other es.each do |estimator| # Add pseudovalue ps[estimator].push( (nb * est_n[estimator]) - ((nb - 1) * h_est[estimator].call(other)) ) end end es.each do |est| ps[est] = DaruLite::Vector.new ps[est] end DaruLite::DataFrame.new ps end |
#keep_if ⇒ Object
Keep an element if block returns true. Destructive.
690 691 692 693 694 |
# File 'lib/daru_lite/vector.rb', line 690 def keep_if return to_enum(:keep_if) unless block_given? delete_if { |val| !yield(val) } end |
#lag(k = 1) ⇒ DaruLite::Vector
Lags the series by ‘k` periods.
Lags the series by ‘k` periods, “shifting” data and inserting `nil`s from beginning or end of a vector, while preserving original vector’s size.
‘k` can be positive or negative integer. If `k` is positive, `nil`s are inserted at the beginning of the vector, otherwise they are inserted at the end.
836 837 838 839 840 841 842 843 844 845 846 |
# File 'lib/daru_lite/vector.rb', line 836 def lag(k = 1) case k when 0 then dup when 1...size copy(([nil] * k) + data.to_a) when -size..-1 copy(data.to_a[k.abs...size]) else copy([]) end end |
#last(q = 1) ⇒ Object
440 441 442 443 |
# File 'lib/daru_lite/vector.rb', line 440 def last(q = 1) # The Enumerable mixin dose not provide the last method. tail(q) end |
#map!(&block) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/daru_lite/vector.rb', line 115 def map!(&block) return to_enum(:map!) unless block @data.map!(&block) self end |
#match(regexp) ⇒ Array
Returns an array of either none or integer values, indicating the regexp
matching with the given array.
1210 1211 1212 |
# File 'lib/daru_lite/vector.rb', line 1210 def match(regexp) @data.map { |value| !!(value =~ regexp) } end |
#n_valid ⇒ Object
number of non-missing elements
861 862 863 |
# File 'lib/daru_lite/vector.rb', line 861 def n_valid size - indexes(*DaruLite::MISSING_VALUES).size end |
#numeric? ⇒ Boolean
449 450 451 |
# File 'lib/daru_lite/vector.rb', line 449 def numeric? type == :numeric end |
#numeric_summary ⇒ String
Displays summary for an numeric type Vector
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
# File 'lib/daru_lite/vector.rb', line 1014 def numeric_summary summary = "\n median: #{median}" + format("\n mean: %0.4f", mean) if sd summary << (format("\n std.dev.: %0.4f", sd) + format("\n std.err.: %0.4f", se)) end if count_values(*DaruLite::MISSING_VALUES).zero? summary << (format("\n skew: %0.4f", skew) + format("\n kurtosis: %0.4f", kurtosis)) end summary end |
#object? ⇒ Boolean
453 454 455 |
# File 'lib/daru_lite/vector.rb', line 453 def object? type == :object end |
#object_summary ⇒ String
Displays summary for an object type Vector
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
# File 'lib/daru_lite/vector.rb', line 999 def object_summary nval = count_values(*DaruLite::MISSING_VALUES) summary = "\n factors: #{factors.to_a.join(',')}" \ "\n mode: #{mode.to_a.join(',')}" \ "\n Distribution\n" data = frequencies.sort.each_with_index.map do |v, k| [k, v, format('%0.2f%%', ((nval.zero? ? 1 : v.quo(nval)) * 100))] end summary + Formatters::Table.format(data) end |
#only_missing(as_a = :vector) ⇒ Object
Returns a Vector containing only missing data (preserves indexes).
1301 1302 1303 1304 1305 1306 1307 1308 |
# File 'lib/daru_lite/vector.rb', line 1301 def only_missing(as_a = :vector) case as_a when :vector self[*indexes(*DaruLite::MISSING_VALUES)] when :array self[*indexes(*DaruLite::MISSING_VALUES)].to_a end end |
#only_numerics ⇒ Object
Returns a Vector with only numerical data. Missing data is included but non-Numeric objects are excluded. Preserves index.
1313 1314 1315 1316 1317 1318 1319 1320 |
# File 'lib/daru_lite/vector.rb', line 1313 def only_numerics numeric_indexes = each_with_index .select { |v, _i| v.is_a?(Numeric) || v.nil? } .map(&:last) self[*numeric_indexes] end |
#only_valid(as_a = :vector, _duplicate = true) ⇒ Object
Creates a new vector consisting only of non-nil data
Arguments
as an Array. Otherwise will return a DaruLite::Vector.
vector, setting this to false will return the same vector. Otherwise, a duplicate will be returned irrespective of presence of missing data.
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 |
# File 'lib/daru_lite/vector.rb', line 1226 def only_valid(as_a = :vector, _duplicate = true) # FIXME: Now duplicate is just ignored. # There are no spec that fail on this case, so I'll leave it # this way for now - zverok, 2016-05-07 new_index = @index.to_a - indexes(*DaruLite::MISSING_VALUES) new_vector = new_index.map { |idx| self[idx] } if as_a == :vector DaruLite::Vector.new new_vector, index: new_index, name: @name, dtype: dtype else new_vector end end |
#positions(*values) ⇒ Object
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 |
# File 'lib/daru_lite/vector.rb', line 1437 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 |
#recode(dt = nil, &block) ⇒ Object
Like map, but returns a DaruLite::Vector with the returned values.
660 661 662 663 664 |
# File 'lib/daru_lite/vector.rb', line 660 def recode(dt = nil, &block) return to_enum(:recode, dt) unless block dup.recode! dt, &block end |
#recode!(dt = nil, &block) ⇒ Object
Destructive version of recode!
667 668 669 670 671 672 673 |
# File 'lib/daru_lite/vector.rb', line 667 def recode!(dt = nil, &block) return to_enum(:recode!, dt) unless block @data.map!(&block).data @data = cast_vector_to(dt || @dtype) self end |
#reindex(new_index) ⇒ Object
Create a new vector with a different index, and preserve the indexing of current elements.
1091 1092 1093 |
# File 'lib/daru_lite/vector.rb', line 1091 def reindex(new_index) dup.reindex!(new_index) end |
#reindex!(new_index) ⇒ DaruLite::Vector
Unlike #reorder! which takes positions as input it takes index as an input to reorder the vector
Sets new index for vector. Preserves index->value correspondence. Sets nil for new index keys absent from original index.
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 |
# File 'lib/daru_lite/vector.rb', line 1049 def reindex!(new_index) values = [] each_with_index do |val, i| values[new_index[i]] = val if new_index.include?(i) end values.fill(nil, values.size, new_index.size - values.size) @data = cast_vector_to @dtype, values @index = new_index update_position_cache self end |
#reject_values(*values) ⇒ DaruLite::Vector
Return a vector with specified values removed
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 |
# File 'lib/daru_lite/vector.rb', line 1251 def reject_values(*values) resultant_pos = size.times.to_a - positions(*values) dv = at(*resultant_pos) # Handle the case when number of positions is 1 # and hence #at doesn't return a vector if dv.is_a?(DaruLite::Vector) dv else pos = resultant_pos.first at(pos..pos) end end |
#rename(new_name) ⇒ Object Also known as: name=
Give the vector a new name
1108 1109 1110 1111 |
# File 'lib/daru_lite/vector.rb', line 1108 def rename(new_name) @name = new_name self end |
#reorder(order) ⇒ Object
Non-destructive version of #reorder!
1085 1086 1087 |
# File 'lib/daru_lite/vector.rb', line 1085 def reorder(order) dup.reorder! order end |
#reorder!(order) ⇒ Object
Unlike #reindex! which takes index as input, it takes positions as an input to reorder the vector
Reorder the vector with given positions
1076 1077 1078 1079 1080 1081 1082 |
# File 'lib/daru_lite/vector.rb', line 1076 def reorder!(order) @index = @index.reorder order data_array = order.map { |i| @data[i] } @data = cast_vector_to @dtype, data_array, @nm_dtype update_position_cache self end |
#replace_nils(replacement) ⇒ Object
Non-destructive version of #replace_nils!
856 857 858 |
# File 'lib/daru_lite/vector.rb', line 856 def replace_nils(replacement) dup.replace_nils!(replacement) end |
#replace_nils!(replacement) ⇒ Object
Replace all nils in the vector with the value passed as an argument. Destructive. See #replace_nils for non-destructive version
Arguments
-
replacement
- The value which should replace all nils
763 764 765 766 767 768 769 |
# File 'lib/daru_lite/vector.rb', line 763 def replace_nils!(replacement) indexes(*DaruLite::MISSING_VALUES).each do |idx| self[idx] = replacement end self end |
#replace_values(old_values, new_value) ⇒ DaruLite::Vector
It performs the replace in place.
Replaces specified values with a new value
1292 1293 1294 1295 1296 1297 1298 |
# File 'lib/daru_lite/vector.rb', line 1292 def replace_values(old_values, new_value) old_values = [old_values] unless old_values.is_a? Array size.times do |pos| set_at([pos], new_value) if include_with_nan? old_values, at(pos) end self end |
#reset_index! ⇒ Object
752 753 754 755 |
# File 'lib/daru_lite/vector.rb', line 752 def reset_index! @index = DaruLite::Index.new(Array.new(size) { |i| i }) self end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
1395 1396 1397 |
# File 'lib/daru_lite/vector.rb', line 1395 def respond_to_missing?(name, include_private = false) name.to_s.end_with?('=') || has_index?(name) || super end |
#rolling_fillna(direction = :forward) ⇒ Object
Non-destructive version of rolling_fillna!
805 806 807 |
# File 'lib/daru_lite/vector.rb', line 805 def rolling_fillna(direction = :forward) dup.rolling_fillna!(direction) end |
#rolling_fillna!(direction = :forward) ⇒ Object
Rolling fillna replace all Float::NAN and NIL values with the preceeding or following value
791 792 793 794 795 796 797 798 799 800 801 802 |
# File 'lib/daru_lite/vector.rb', line 791 def rolling_fillna!(direction = :forward) enum = direction == :forward ? index : index.reverse_each last_valid_value = 0 enum.each do |idx| if valid_value?(self[idx]) last_valid_value = self[idx] else self[idx] = last_valid_value end end self end |
#save(filename) ⇒ Object
Save the vector to a file
Arguments
-
filename - Path of file where the vector is to be saved
1349 1350 1351 |
# File 'lib/daru_lite/vector.rb', line 1349 def save(filename) DaruLite::IO.save self, filename end |
#set_at(positions, val) ⇒ Object
Change value at given positions
251 252 253 254 255 |
# File 'lib/daru_lite/vector.rb', line 251 def set_at(positions, val) validate_positions(*positions) positions.map { |pos| @data[pos] = val } update_position_cache end |
#size ⇒ Object
89 90 91 |
# File 'lib/daru_lite/vector.rb', line 89 def size @data.size end |
#sort(opts = {}, &block) ⇒ Object
Sorts a vector according to its values. If a block is specified, the contents will be evaluated and data will be swapped whenever the block evaluates to true. Defaults to ascending order sorting. Any missing values will be put at the end of the vector. Preserves indexing. Default sort algorithm is quick sort.
Options
-
:ascending
- if false, will sort in descending order. Defaults to true. -
:type
- Specify the sorting algorithm. Only supports quick_sort for now.
Usage
v = DaruLite::Vector.new ["My first guitar", "jazz", "guitar"]
# Say you want to sort these strings by length.
v.sort(ascending: false) { |a,b| a.length <=> b.length }
603 604 605 606 607 608 609 610 611 612 |
# File 'lib/daru_lite/vector.rb', line 603 def sort(opts = {}, &block) opts = { ascending: true }.merge(opts) vector_index = resort_index(@data.each_with_index, opts, &block) vector, index = vector_index.transpose index = @index.reorder index DaruLite::Vector.new(vector, index: index, name: @name, dtype: @dtype) end |
#sort_by_index(opts = {}) ⇒ Vector
Sorts the vector according to it’s`Index` values. Defaults to ascending order sorting.
632 633 634 635 636 637 |
# File 'lib/daru_lite/vector.rb', line 632 def sort_by_index(opts = {}) opts = { ascending: true }.merge(opts) _, new_order = resort_index(@index.each_with_index, opts).transpose reorder new_order end |
#sorted_data(&block) ⇒ Object
Just sort the data and get an Array in return using Enumerable#sort. Non-destructive. :nocov:
654 655 656 |
# File 'lib/daru_lite/vector.rb', line 654 def sorted_data(&block) @data.to_a.sort(&block) 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]>}
735 736 737 738 739 740 741 742 743 744 |
# File 'lib/daru_lite/vector.rb', line 735 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
746 747 748 749 750 |
# File 'lib/daru_lite/vector.rb', line 746 def split_by_separator_freq(sep = ',') split_by_separator(sep).transform_values do |v| v.sum(&:to_i) end end |
#splitted(sep = ',') ⇒ Object
Return an Array with the data splitted by a separator.
a=DaruLite::Vector.new(["a,b","c,d","a,b","d"])
a.splitted
=>
[["a","b"],["c","d"],["a","b"],["d"]]
710 711 712 713 714 715 716 717 718 719 720 |
# File 'lib/daru_lite/vector.rb', line 710 def splitted(sep = ',') @data.map do |s| if s.nil? nil elsif s.respond_to? :split s.split sep else [s] end end end |
#summary(indent_level = 0) ⇒ String
Create a summary of the Vector
983 984 985 986 987 988 989 990 991 992 993 994 995 |
# File 'lib/daru_lite/vector.rb', line 983 def summary(indent_level = 0) non_missing = size - count_values(*DaruLite::MISSING_VALUES) summary = (' =' * indent_level) + "= #{name}" \ "\n n :#{size}" \ "\n non-missing:#{non_missing}" case type when :object summary << object_summary when :numeric summary << numeric_summary end summary.split("\n").join("\n#{' ' * indent_level}") end |
#tail(q = 10) ⇒ Object
435 436 437 438 |
# File 'lib/daru_lite/vector.rb', line 435 def tail(q = 10) start = [size - q, 0].max self[start..(size - 1)] end |
#to_a ⇒ Object
Return an array
922 923 924 |
# File 'lib/daru_lite/vector.rb', line 922 def to_a @data.to_a end |
#to_category(opts = {}) ⇒ DaruLite::Vector
Converts a non category type vector to category type vector.
1376 1377 1378 1379 1380 1381 |
# File 'lib/daru_lite/vector.rb', line 1376 def to_category(opts = {}) dv = DaruLite::Vector.new to_a, type: :category, name: @name, index: @index dv.ordered = opts[:ordered] || false dv.categories = opts[:categories] if opts[:categories] dv end |
#to_df ⇒ DaruLite::DataFrame
Returns the vector as a single-vector dataframe.
896 897 898 |
# File 'lib/daru_lite/vector.rb', line 896 def to_df DaruLite::DataFrame.new({ @name => @data }, name: @name, index: @index) end |
#to_h ⇒ Object
Convert to hash (explicit). Hash keys are indexes and values are the correspoding elements
917 918 919 |
# File 'lib/daru_lite/vector.rb', line 917 def to_h @index.to_h { |index| [index, self[index]] } end |
#to_html(threshold = 30) ⇒ Object
Convert to html for iruby
932 933 934 935 936 937 938 939 940 941 |
# File 'lib/daru_lite/vector.rb', line 932 def to_html(threshold = 30) table_thead = to_html_thead table_tbody = to_html_tbody(threshold) path = if index.is_a?(MultiIndex) File.('iruby/templates/vector_mi.html.erb', __dir__) else File.('iruby/templates/vector.html.erb', __dir__) end ERB.new(File.read(path).strip).result(binding) end |
#to_html_tbody(threshold = 30) ⇒ Object
953 954 955 956 957 958 959 960 961 |
# File 'lib/daru_lite/vector.rb', line 953 def to_html_tbody(threshold = 30) table_tbody_path = if index.is_a?(MultiIndex) File.('iruby/templates/vector_mi_tbody.html.erb', __dir__) else File.('iruby/templates/vector_tbody.html.erb', __dir__) end ERB.new(File.read(table_tbody_path).strip).result(binding) end |
#to_html_thead ⇒ Object
943 944 945 946 947 948 949 950 951 |
# File 'lib/daru_lite/vector.rb', line 943 def to_html_thead table_thead_path = if index.is_a?(MultiIndex) File.('iruby/templates/vector_mi_thead.html.erb', __dir__) else File.('iruby/templates/vector_thead.html.erb', __dir__) end ERB.new(File.read(table_thead_path).strip).result(binding) end |
#to_json ⇒ Object
Convert the hash from to_h to json
927 928 929 |
# File 'lib/daru_lite/vector.rb', line 927 def to_json(*) to_h.to_json end |
#to_matrix(axis = :horizontal) ⇒ Object
Convert Vector to a horizontal or vertical Ruby Matrix.
Arguments
-
axis
- Specify whether you want a :horizontal or a :vertical matrix.
905 906 907 908 909 910 911 912 913 914 |
# File 'lib/daru_lite/vector.rb', line 905 def to_matrix(axis = :horizontal) case axis when :horizontal Matrix[to_a] when :vertical Matrix.columns([to_a]) else raise ArgumentError, "axis should be either :horizontal or :vertical, not #{axis}" end end |
#to_s ⇒ Object
963 964 965 |
# File 'lib/daru_lite/vector.rb', line 963 def to_s "#<#{self.class}#{": #{@name}" if @name}(#{size})#{':category' if category?}>" end |
#type ⇒ Object
The type of data contained in the vector. Can be :object.
Running through the data to figure out the kind of data is delayed to the last possible moment.
538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/daru_lite/vector.rb', line 538 def type if @type.nil? || @possibly_changed_type @type = :numeric each do |e| next if e.nil? || e.is_a?(Numeric) @type = :object break end @possibly_changed_type = false end @type end |
#uniq ⇒ Object
Keep only unique elements of the vector alongwith their indexes.
572 573 574 575 576 577 |
# File 'lib/daru_lite/vector.rb', line 572 def uniq uniq_vector = @data.uniq new_index = uniq_vector.map { |element| index_of(element) } DaruLite::Vector.new uniq_vector, name: @name, index: new_index, dtype: @dtype end |
#verify ⇒ Object
Reports all values that doesn’t comply with a condition. Returns a hash with the index of data and the invalid data.
698 699 700 701 702 703 |
# File 'lib/daru_lite/vector.rb', line 698 def verify (0...size) .map { |i| [i, @data[i]] } .reject { |_i, val| yield(val) } .to_h end |
#where(bool_array) ⇒ Object
Return a new vector based on the contents of a boolean array. Use with the comparator methods to obtain meaningful results. See this notebook for a good overview of using #where.
406 407 408 |
# File 'lib/daru_lite/vector.rb', line 406 def where(bool_array) DaruLite::Core::Query.vector_where self, bool_array end |