2

I am trying to return an array (element-wise) of True/False values on a numpy array comparsion to a float64 static variable. Input and desired output array is 1x10 (column x row)

array = np.random.randint(10, size=(10,1))

Attempt 1:

bool = np.any((array >= min)&(array <= max))

Attempt 2:

bool = np.logical_and((array >= min),(array <= max))

Attempt 3:

bool = np.any([(array >= min)&(array <= max)])

Attempt 4:

bool = np.array(np.any([(array >= min)&(array <= max)]))

All four of the above methods produce this output in the interpreter

print(bool) = True

When desired output looks something like:

print(bool) = [True
               False
               True
               True
               False
               False
               True
               False
               False
               True]

Thank you in advance for any insight you can provide me!

3
  • print your original array, notice that every individual value is wrapped in an array, is this intentional? Commented Jul 1, 2019 at 12:55
  • 1
    No it was not, I am as you call "noob" haha especially with numpy Commented Jul 1, 2019 at 13:13
  • OK so you actually just want size to be (10) and not (10,1) :), and then there's no need for .ravel() in my answer. Commented Jul 1, 2019 at 13:15

1 Answer 1

1

you can use .ravel() to get your output in the desired shape.

try this:

import numpy as np

array = np.random.randint(10, size=(10, 1))

min = 2.2
max = 6.6
result = ((array >= min) & (array <= max)).ravel()

print(result)

Output (example, as it is random):

[False True True True True True False False False True]

Sign up to request clarification or add additional context in comments.

1 Comment

just confirmed in compiler that .ravel option works! Neat find, didn't know this option existed. Very useful

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.