-1

I am not quite sure why this isn´t working.. any explanation is highly appreciated. Mapping a function to np.array like:

test = np.array([0.6,0.7,1,0,0.5,0.2,0.4,0.3])
decision_boundary=0.6
decision_func = lambda x: 1. if (x >= decision_boundary) else 0.
decision_func(test)

results in the following value-error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Bennimi
  • 416
  • 5
  • 14

1 Answers1

1

You're not applying the function to each element of the array. You're applying the function to the whole array, and numpy is rightly telling you you can't convert a numpy array to Boolean to be used as the condition of if. Fortunately, in your case, >= is already vectorized on numpy arrays, so you can just do

x >= decision_boundary

Or if you really want ones and zeroes,

1 * (x >= decision_boundary)

In general, if you have a function that's not vectorized, you can make it so with the numpy function vectorize.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116