class Mikon::DArray

Internal data structure to wrap NMatrix Its stastical methods (i.e. median) is compartible with Statsample::Vector’s @example

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

Attributes

data[R]
dtype[R]

Public Class Methods

new(source, options={}) click to toggle source

@param [NMatrix|Array] source @param [Hash] options

# 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

Public Instance Methods

[](pos) click to toggle source
# File lib/mikon/core/array.rb, line 65
def [](pos)
  @data[pos]
end
coerce(other) click to toggle source
Calls superclass method
# 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
dup() click to toggle source
# File lib/mikon/core/array.rb, line 43
def dup
  Mikon::DArray.new(@data.dup)
end
each(&block) click to toggle source
# File lib/mikon/core/array.rb, line 47
def each(&block)
  @data.each(&block)
end
expand(length) click to toggle source
# 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) click to toggle source
# 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
length() click to toggle source
# File lib/mikon/core/array.rb, line 61
def length
  @data.shape.first
end
reduce(init, &block) click to toggle source
# File lib/mikon/core/array.rb, line 51
def reduce(init, &block)
  @data.inject_rank(0, init, &block).first
end
reverse() click to toggle source
# 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
sort() click to toggle source
# File lib/mikon/core/array.rb, line 69
def sort
  Mikon::DArray.new(@data.sort)
end
sort_by(&block) click to toggle source
# 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_a() click to toggle source
# File lib/mikon/core/array.rb, line 115
def to_a
  @data.to_a
end
to_json() click to toggle source
# File lib/mikon/core/array.rb, line 111
def to_json
  @data.to_json
end