Class: Mikon::DArray
- Inherits:
-
Object
show all
- Includes:
- Enumerable, Stats
- Defined in:
- lib/mikon/core/array.rb
Overview
Internal data structure to wrap NMatrix Its stastical methods (i.e. #median) is compartible with Statsample::Vector’s
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Stats
#==, #average_deviation_population, #coefficient_of_variation, #count, #each_index, #factors, #frequencies, #has_missing_data?, #is_valid?, #kurtosis, #mean, #median, #median_absolute_deviation, #mode, #n_valid, #percentil, #product, #proportion, #proportion_confidence_interval_t, #proportion_confidence_interval_z, #proportions, #push, #range, #ranked, #recode, #recode!, #skew, #standard_deviation_population, #standard_deviation_sample, #standard_error, #sum, #sum_of_squared_deviation, #sum_of_squares, #variance_sample, #vector_standarized
Constructor Details
#initialize(source, options = {}) ⇒ DArray
Returns a new instance of DArray.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/mikon/core/array.rb', line 14
def initialize(source, options={})
case
when source.is_a?(Array)
if source.all? {|el| el.is_a?(Numeric)}
@data = NMatrix.new([source.length], source, options)
else
extend UseArray
@data = Mikon::ArrayWrapper.new(source)
end
when source.is_a?(NMatrix)
unless source.shape.length == 1 && source.shape.first.is_a?(Numeric)
raise "Matrix shape is not valid"
end
@data = source
else
raise "Non-acceptable Argument Error"
end
@dtype = @data.dtype
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
10
11
12
|
# File 'lib/mikon/core/array.rb', line 10
def data
@data
end
|
#dtype ⇒ Object
Returns the value of attribute dtype.
10
11
12
|
# File 'lib/mikon/core/array.rb', line 10
def dtype
@dtype
end
|
Instance Method Details
#[](pos) ⇒ Object
61
62
63
|
# File 'lib/mikon/core/array.rb', line 61
def [](pos)
@data[pos]
end
|
#coerce(other) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/mikon/core/array.rb', line 99
def coerce(other)
if [NMatrix, Array].any?{|cls| other.is_a?(cls) && @data.is_a?(cls)}
return other, @data
else
super
end
end
|
#each(&block) ⇒ Object
43
44
45
|
# File 'lib/mikon/core/array.rb', line 43
def each(&block)
@data.each(&block)
end
|
#expand(length) ⇒ Object
51
52
53
54
55
|
# File 'lib/mikon/core/array.rb', line 51
def expand(length)
raise "The argument 'length' should be greater than length of now." if length < self.length
data = NMatrix.new([expand], @data.to_a)
@data = data.map.with_index{|val, i| i < self.length ? val : 0}
end
|
#fillna(fill_value = 0) ⇒ Object
111
112
113
|
# File 'lib/mikon/core/array.rb', line 111
def fillna(fill_value=0)
@data = @data.map{|val| val.to_f.nan? ? fill_value : val}
end
|
#length ⇒ Object
57
58
59
|
# File 'lib/mikon/core/array.rb', line 57
def length
@data.shape.first
end
|
#reduce(init, &block) ⇒ Object
47
48
49
|
# File 'lib/mikon/core/array.rb', line 47
def reduce(init, &block)
@data.inject_rank(0, init, &block).first
end
|
#reverse ⇒ Object
74
75
76
77
|
# File 'lib/mikon/core/array.rb', line 74
def reverse
len = self.length
Mikon::DArray.new(@data.map.with_index{|v, i| @data[self.length-i-1]})
end
|
#sort ⇒ Object
65
66
67
|
# File 'lib/mikon/core/array.rb', line 65
def sort
Mikon::DArray.new(@data.sort)
end
|
#sort_by(&block) ⇒ Object
69
70
71
72
|
# File 'lib/mikon/core/array.rb', line 69
def sort_by(&block)
return self.to_enum(:sort_by) unless block_given?
Mikon::DArray.new(@data.sort_by(&block))
end
|
#to_a ⇒ Object
107
108
109
|
# File 'lib/mikon/core/array.rb', line 107
def to_a
@data.to_a
end
|