ruby-numo / numo-narray

Ruby/Numo::NArray - New NArray class library
http://ruby-numo.github.io/narray/
BSD 3-Clause "New" or "Revised" License
415 stars 41 forks source link

Boolean Indexing , different behavior from Numpy #130

Closed kojix2 closed 4 years ago

kojix2 commented 5 years ago

Hello

I just found out the big difference between NArray and NumPy.

NumPy

import numpy as np

a = np.arange(8).reshape(2,4)
# array([[0, 1, 2, 3],
#        [4, 5, 6, 7]])

a[0, :]
# array([0, 1, 2, 3])

b = a[0, :] > 1
# array([False, False, True, True])

a[:, b]
# array([[2, 3],
#        [6, 7]])

Ruby

require 'numo/narray'

a = Numo::DFloat.new(2,4).seq
# [[0, 1, 2, 3], 
#  [4, 5, 6, 7]]

a[0, true]
# [0, 1, 2, 3]

b = a[0, true] > 1
# => Numo::Bit#shape=[4]
# [0, 0, 1, 1]

a[true, b]
# => Numo::DFloat(view)#shape=[2,4]
# [[0, 0, 1, 1], 
#  [4, 4, 5, 5]]

a[true, b.where]
# [[2, 3], 
#  [6, 7]]

I do not think that the behavior of NArray is bad, but I feel that it may not be consistent with the results below.

a = Numo::DFloat(4).seq
b = a > 1
a[b]
# [2, 3]
a = Numo::DFloat(1, 4).seq
b = a[0,true] > 1
a[true, b]
# [[0, 0, 1, 1]]

Use Case

The Rumale Machine Learning Library uses a 2D NArray as a form of sample data. I use Rumale to classify my sample data. The classification results of Rumale is a 1-dimensional NArray. I would like to separate sample data using boolean indexing.

Thank you.

masa16 commented 5 years ago

OK. I will change the behavior of a[true,b] to be same as a[true,b.where]

masa16 commented 4 years ago

fixed with https://github.com/ruby-numo/numo-narray/commit/98de4cc9509b1e770558bb3259d4939341f7cd55