1

I've got an array and a boolean array (as one hot encoding)

a = np.arange(12).reshape(4,3)
b = np.array([
    [1,0,0],
    [0,1,0],
    [0,0,1],
    [0,0,1],
], dtype=bool)

print(a)
print(b)
# [[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]]
# [[ True False False]
#  [False  True False]
#  [False False  True]
#  [False False  True]]

And I would like to pick elements using a boolean array

print(a[:, [True, False, False]])
# array([[0],
#        [3],
#        [6],
#        [9]])

print(a[:, [False, True, False]])
# array([[ 1],
#        [ 4],
#        [ 7],
#        [10]])

But this picks based on the same template boolean for all rows. I would like to perform this on a per row basis:

print(a[:, b])
# IndexError: too many indices for array

What should I put in ... so I get:

print(a[:, ...])
# array([[0],
#        [4],
#        [8],
#        [11]])

EDIT: This is analogous to what was used in the infamous CS231 course:

dscores = a
num_examples = 4 
# They had 300
y = b
dscores[range(num_examples),y]
# equivalent to
# a{:,b]

EDIT 2: In CS231 example, y is one dimensional and is not one hot encoded!

They were doing dscores[[rowIdx],[columnIdx]]

2 Answers 2

3

After filter by b broadcast it

a[b][:,None]
Out[168]: 
array([[ 0],
       [ 4],
       [ 8],
       [11]])

Or

a[b,None]
Out[174]: 
array([[ 0],
       [ 4],
       [ 8],
       [11]])
Sign up to request clarification or add additional context in comments.

3 Comments

I was trying to understand dscores[range(num_examples),y] -= 1 from CS231 and that was the old syntax back then?
@SaravanabalagiRamachandran I feel like y is one dimension array right ?
Oh yes, sorry I thought they had one hot encodings! They are indeed one dimensional
0

Here is an alternative way of doing the same. Please note that this is inefficient when compared to advanced indexing. It's just for pedagogical purposes and to illustrate that a problem can be solved using more than one approach.

In [275]: np.add.reduce(a*b, axis=1, keepdims=True)
Out[275]: 
array([[ 0],
       [ 4],
       [ 8],
       [11]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.