0

So I have a function iterating through a multidimensional numpy array and preforming a function on every item in the array, the funtion returns either True or False and I need to filter out all the items which return false:

def unhappy_grid(grid, empty_value=-1, threshold=3):
    for y in grid:
        for x in y:
            if happy(grid, y, x, threshold).any() == False:
                print('test')
    return

Happy is the function that returns true or false but even after I use .any() it still gives me this error? I don't know what I am doing wrong here, can someone please explain how to fix this?

Yoeril
  • 83
  • 6
  • 1
    Also, this is not necessary, but the if statement can be used like this too: `if True: print("I didn't need ==True! Wow!")` or `if not False: print("Hey... This works too")`. It's a good thing to know; makes the code look nicer sometimes :) – Eric B Feb 12 '20 at 16:57
  • 1
    What gives this error? Show the full traceback. – hpaulj Feb 12 '20 at 17:10
  • 1
    Is the error in `happy` itself? What's the shape of `grid`. `y` has 1 less dimension, `x` one less than that. That means you are still passing an array to `happy`. `any()` after the call does nothing to correct an ambiguity error within `happy`. – hpaulj Feb 12 '20 at 17:44
  • Omg I found it I am stupid, thank you for your comments because otherwise I wouldn't have thought of it. I didn't think about that y and x would both take 1 argument less. – Yoeril Feb 12 '20 at 19:13

1 Answers1

2

You haven't provided an error message, but;

Booleans (True or False) do not have a .any() method. If happy() returns a boolean, you should check if that is True or False.

Python also has a builtin function called filter(function, iterable) which could be used here.

Edit:

Oversaw the error in the question title... My bad...

The error apparently arises when attempting to evaluate numpy arrays. This answer may answer your question.

Eric B
  • 152
  • 1
  • 11
  • Ok thank you for suggesting filter. I get the same error (see the title) if I just do: happy(grid, y, x, threshold) == False and I checked if happy does return either True or False and when I call it on it's own it works perfectly fine. – Yoeril Feb 12 '20 at 16:57
  • Oh god, my bad... I oversaw the error in the title. – Eric B Feb 12 '20 at 16:58
  • Ok thank you for the edit and pointing me in the right direction! – Yoeril Feb 12 '20 at 17:07