Class: Array

Inherits:
Object show all
Includes:
BasicStatistics, FeldtRuby::Normalization
Defined in:
lib/feldtruby/array.rb,
lib/feldtruby/array/count_by.rb,
lib/feldtruby/array/basic_stats.rb,
lib/feldtruby/statistics/normalization.rb,
lib/feldtruby/array/permutations_and_subsets.rb

Instance Method Summary collapse

Methods included from FeldtRuby::Normalization

#min_max_normalize, #normalize, #z_normalize

Methods included from BasicStatistics

#average, #inter_quartile_range, #mean, #median, #quantile_at_ratio, #quantiles, #quartiles, #rms, #rms_from, #rms_from_scalar, #root_mean_square, #sd, #stdev, #sum, #sum_of_abs, #sum_of_abs_deviations, #sum_squared_error, #summary_stats, #var, #variance, #weighted_mean, #weighted_sum

Instance Method Details

#add_unless_there(element) ⇒ Object

Add an element unless it is already in the array



41
42
43
44
# File 'lib/feldtruby/array.rb', line 41

def add_unless_there(element)
	self << element unless self.include?(element)
	self
end

#all_combinations_one_from_eachObject

Create all combinations of values from an array of sub-arrays, with each combination picking one value from each sub-array.

Examples:

[[1,2], [3]].all_combinations_one_from_each => [[1,3], [2,3]]

[[1,2], [3, 7]].all_combinations_one_from_each => [[1,3], [2,3], [1,7], [2,7]]


19
20
21
22
23
24
25
26
27
# File 'lib/feldtruby/array/permutations_and_subsets.rb', line 19

def all_combinations_one_from_each
  return [] if length == 0

  return self.first.map {|v| [v]} if length == 1

  self[1..-1].all_combinations_one_from_each.map do |c|
    self.first.map {|v| [v] + c}
  end.flatten(1)
end

#all_pairsObject



2
3
4
5
6
7
8
9
10
# File 'lib/feldtruby/array/permutations_and_subsets.rb', line 2

def all_pairs

  return [] if length < 2

  x, *rest = self

  rest.map {|e| [x, e]} + rest.all_pairs

end

#count_by(&proc) ⇒ Object



2
3
4
5
6
# File 'lib/feldtruby/array/count_by.rb', line 2

def count_by(&proc)
  counts = Hash.new(0)
  self.each {|e| counts[proc.call(e)] += 1}
  counts
end

#countsObject

Count elements in array and return as hash mapping elements to their counts.



47
48
49
50
51
# File 'lib/feldtruby/array.rb', line 47

def counts
	count_hash = Hash.new(0)
	self.each {|element| count_hash[element] += 1}
	count_hash
end

#counts_within_ratio_of(targetValue, epsilon = 0.10) ⇒ Object

Count the values that are within +/-(epsilon*targetValue) of a targetValue and return the counts as an array. Will skip elements of the array that are not Numeric.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/feldtruby/array.rb', line 56

def counts_within_ratio_of(targetValue, epsilon = 0.10)
	min = targetValue * (1.0 - epsilon)
	max = targetValue * (1.0 + epsilon)
	count_hash = Hash.new(0)
	self.each do |element| 
		if Numeric === element && element >= min && element <= max
			count_hash[element] += 1
		end
	end
	count_hash
end

#distance_between_elementsObject

Calculate the distance between the elements.



10
11
12
13
# File 'lib/feldtruby/array.rb', line 10

def distance_between_elements
	return nil if self.length == 0
	self[0...-1].zip(self[1..-1]).map {|x,y| y-x}
end

#map_with_index(&block) ⇒ Object



4
5
6
7
# File 'lib/feldtruby/array.rb', line 4

def map_with_index(&block)
	index = -1 # Start below zero since we pre-increment in the loop
	self.map {|v| block.call(v,index+=1)}
end

#ranksObject

Rank of values in array from 1..length



21
22
23
24
25
# File 'lib/feldtruby/array.rb', line 21

def ranks
	ranks = Array.new(length)
	self.each_with_index.map {|e,i| [i, e]}.sort_by {|v| v.last}.each_with_index.map {|v,i| ranks[v.first]=length-i}
	ranks
end

#ranks_by(prependRanks = true, &mapToValueUsedForRanking) ⇒ Object

Prepend (or append) ranks after sorting by the value supplied from a block



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/feldtruby/array.rb', line 28

def ranks_by(prependRanks = true, &mapToValueUsedForRanking)
	res = Array.new(length)
	sorted_with_indices = self.each_with_index.map {|e,i| [i, e]}.sort_by {|v| mapToValueUsedForRanking.call(v.last)}
	sorted_with_indices.each_with_index.map do |v,index|
		orig_index, orig_element = v
		rank = length - index
		new_element = prependRanks ? ([rank] + orig_element) : (orig_element + [rank])
		res[orig_index] = new_element
	end
	res
end

#sampleObject



68
69
70
# File 'lib/feldtruby/array.rb', line 68

def sample
	self[rand(self.length)]
end

#swap!(index1, index2) ⇒ Object

Swap two elements given their indices. Assumes both indices are in range.



16
17
18
# File 'lib/feldtruby/array.rb', line 16

def swap!(index1, index2)
	self[index1], self[index2] = self[index2], self[index1]
end