Search code examples
pythonarraysnumpybooleancomparison

Numpy Array Element-Comparison to Float64


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!


Solution

  • 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]