Change sign of elements in numpy array from a to b. I tried this.
import numpy as np
def do_negative(X, a, b):
lst = []
for i in X:
if (a<i<b):
lst.append(-i)
else:
lst.append(i)
return X
test = np.array(range(9)).reshape(3,3)
do_negative(test, -1, 3).all()
But this returns error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Input data: from -1 to 3.
Output should be: np.array([[ 0, -1, -2], [-3, 4, 5], [ 6, 7, 8]])