Class: Mikon::DArray

Inherits:
Object
  • 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

Examples:

Mikon::DArray.new([1, 2, 3]) #-> #<Mikon::DArray:0xbacfc99c @data=[1, 2, 3], @dtype=:int32>

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.

Parameters:

  • source (NMatrix|Array)
  • options (Hash) (defaults to: {})


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
      #
      # NMatrix instance whose dtype is :object frequently causes Segmentation Fault
      # @example
      #   df = DataFrame.new({a: ["a", "b"], b: [1, 2]})
      #   df[:a].to_html #-> Segmentation Fault
      #

      # @data = NMatrix.new([source.length], source, options.merge({:dtype => :object}))
      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

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/mikon/core/array.rb', line 10

def data
  @data
end

#dtypeObject (readonly)

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



65
66
67
# File 'lib/mikon/core/array.rb', line 65

def [](pos)
  @data[pos]
end

#coerce(other) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/mikon/core/array.rb', line 103

def coerce(other)
  if [NMatrix, Array].any?{|cls| other.is_a?(cls) && @data.is_a?(cls)}
    return other, @data
  else
    super
  end
end

#dupObject



43
44
45
# File 'lib/mikon/core/array.rb', line 43

def dup
  Mikon::DArray.new(@data.dup)
end

#each(&block) ⇒ Object



47
48
49
# File 'lib/mikon/core/array.rb', line 47

def each(&block)
  @data.each(&block)
end

#expand(length) ⇒ Object



55
56
57
58
59
# File 'lib/mikon/core/array.rb', line 55

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



119
120
121
# File 'lib/mikon/core/array.rb', line 119

def fillna(fill_value=0)
  @data = @data.map{|val| val.to_f.nan? ? fill_value : val}
end

#lengthObject



61
62
63
# File 'lib/mikon/core/array.rb', line 61

def length
  @data.shape.first
end

#reduce(init, &block) ⇒ Object



51
52
53
# File 'lib/mikon/core/array.rb', line 51

def reduce(init, &block)
  @data.inject_rank(0, init, &block).first
end

#reverseObject



78
79
80
81
# File 'lib/mikon/core/array.rb', line 78

def reverse
  len = self.length
  Mikon::DArray.new(@data.map.with_index{|v, i| @data[self.length-i-1]})
end

#sortObject



69
70
71
# File 'lib/mikon/core/array.rb', line 69

def sort
  Mikon::DArray.new(@data.sort)
end

#sort_by(&block) ⇒ Object



73
74
75
76
# File 'lib/mikon/core/array.rb', line 73

def sort_by(&block)
  return self.to_enum(:sort_by) unless block_given?
  Mikon::DArray.new(@data.sort_by(&block))
end

#to_aObject



115
116
117
# File 'lib/mikon/core/array.rb', line 115

def to_a
  @data.to_a
end

#to_jsonObject



111
112
113
# File 'lib/mikon/core/array.rb', line 111

def to_json
  @data.to_json
end