Home > Software engineering >  Python Numpy Jupter Notebook "The truth value of an array with more than one element is ambiguo
Python Numpy Jupter Notebook "The truth value of an array with more than one element is ambiguo

Time:09-21

Code with Error Screenshot

Hi I have a question of why I'm getting this error message:

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

When I'm trying to run this function, and pass in a NumPy array:

testValues=np.arange(-5,5,0.01)

def factorial(n):
    if n == 0:
        return 0
    else:
        return 1
        
factorial(testValues)

Appreciate any help!

CodePudding user response:

Your function is set up to receive an integer, not a NumPy array. Furthermore, if you are trying to calculate a factorial recursively, you are missing the recursive call to the factorial function.

CodePudding user response:

With numpy you are passing the entire ndarray to your function. So n isnt a single value.

Your function also doesnt make much sense. Maybe we can help you better if you tell us your ultimate goal.

If you want to apply your function to every number in testValues, you might want to check out numpy.vectorize

np.vectorize(factorial)(testValues)
  • Related