0

I have a Python NumPy array like this:

array([[[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255]],

       [[  0,   0, 125, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255]]], dtype=uint8)

And I'm trying to get the index of 125 inside the array with 2 numbers in it. At first I had only 3 values, instead of 4, and this worked to get the indexed position:

p = np.unravel_index(tiles[tileindex].argmax(), tiles[tileindex].shape)

Can't seem to find what I need to use now instead of argmax(); the value 125, can be 255 as well, so I'd like to make my selection based on the number of non-zero values in the nested arrays instead of the value itself.

So basically, how to find the line with two values, and then get the position of 125 in this example.

Any help much appreciated.

2
  • 1
    print(*np.where(arr==125)) Commented Feb 14, 2020 at 14:40
  • Actually, print(np.argwhere(arr==125)[0]) is probably more useful Commented Feb 14, 2020 at 14:43

2 Answers 2

1

This will return the position of the value of interest, with both its first and second axis' index:

axis0, axis1 = np.where((a > 0).cumsum(2)[...,-1] > 1)

Or similarly:

axis0, axis1 = np.where((a>0).view('i1').sum(-1) > 1)

print(axis0)
#2
print(axis1)
#0
print(a[axis0, axis1])
# array([[  0,   0, 125, 255]])
Sign up to request clarification or add additional context in comments.

4 Comments

np.argwhere(arr==125)[0] would be easier, no?
I think OP wants the array with 2 numbers in it, regardless of whether it is 125? @roganjosh
Ah, I think we're reading the question differently and your interpretation is probably correct
thx I used this, was going to provide some code, but markdown is all messed up.
0

I'm not sure to totally understand the question, but you say you want to select on the number of non-zeros, so you can use np.count_nonzero

Let say that we call A the matrix you set in your question. A first option, not optimal but more readable is the following

for a in A:
    print( np.count_nonzero(a,axis=1) # print just for example here, you can stock it

And you will have, as a result, for each submatrix of your array A a vector with number of nonzero by lines. You can see that for the fourth matrix, you have two non-zeros values on the first line

[1 1 1 1 1 1 1 1]

[1 1 1 1 1 1 1 1]

[2 1 1 1 1 1 1 1] -> two non-zeros values on the first line

[1 1 1 1 1 1 1 1]

You can do it in a shorter way like this

np.count_nonzero(A,axis=2)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.