Skip to content

Comparison with Numpy

Masahiro TANAKA edited this page Apr 4, 2017 · 6 revisions

Comparison with Numpy:

Indexing

  • Numpy:
a[ 0, ... , 1:5, 1: , ::-1, numpy.newaxis, : ]
  • Numo::NArray:
a[ 0, false, 1..4, 1..-1, (-1..0).step(-1), :new, true ]

1-D indexing on 2-D array

  • Numpy
>>> import numpy as np
>>> x = np.arange(12).reshape(3,4)
>>> x
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

>>> x[1,...]
array([4, 5, 6, 7])

>>> x[1]
array([4, 5, 6, 7])

>>> x[1:3]
array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

>>> x[8]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index 8 is out of bounds for axis 0 with size 3
  • Numo::NArray
irb(main):001:0> x = Numo::DFloat.new(3,4).seq
=> Numo::DFloat#shape=[3,4]
[[0, 1, 2, 3], 
 [4, 5, 6, 7], 
 [8, 9, 10, 11]]

irb(main):002:0> x[1,false]
=> Numo::DFloat(view)#shape=[4]
[4, 5, 6, 7]

irb(main):003:0> x[1]
=> 1.0

irb(main):004:0> x[1..2]
=> Numo::DFloat(view)#shape=[2]
[1, 2]

irb(main):005:0> x[8]
=> 8.0

irb(main):006:0> x[8..10]
=> Numo::DFloat(view)#shape=[3]
[8, 9, 10]

Clone this wiki locally