SecretAgent
SecretAgent

Reputation: 107

Numpy all or any is not giving me the desired result

I am trying to achieve the following.

  1. I want to make sure that in the array below, I want to retrieve sub arrays where ALL values at index 0,1 are less than 2000 and ANY value at index 3-5 is greater than 10,000. According to this i should retrieve the first array of this 2d array. However i get an empty array. Does anyone know why?

  2. Would you know how i can test this condition has been applied correctly from the resulting array x?

    test = np.array([[1000,1500,1000,7000,200,40000], [1000,2200,5000,7000,200,4000], [1000,2200,5000,7000,200,40000]])
    x = np.where(np.all(test[0:2]<2000) & np.any(test[3:6]>10000))
    print(x) 
    

Upvotes: 1

Views: 34

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150785

Do you mean:

mask = (test[:,:2] < 2000).all(1) & (test[:,3:6] > 10000).any(1)

test[mask]
# array([[ 1000,  1500,  1000,  7000,   200, 40000]])

np.where(mask)
# (array([0], dtype=int64),)

Upvotes: 1

Related Questions