I face a problem with Numpy, I try to use the values of each row (of B) as the indexes of another multidimensional array (A):
>>> A
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> B
array([[ 0, 1, 2, 3],
[ 1, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> np.clip(B, 0, 2)
array([[0, 1, 2, 2],
[1, 2, 2, 2],
[2, 2, 2, 2]])
Here is the expected result:
array([[0, 1, 2, 2],
[4, 5, 5, 5],
[8, 8, 8, 8]])
Any idea ? Thanks a lot for your help.
np.clipjust clips (i.e. clamps) the values of each element of an array, so in your case it would clip the values to between 0 and 2. Why do you expect that result from np.clip(...)?Bso that they are in the closed range[0, 2]; therefore, it is impossible fornp.clipto return an array that has elements valued outside this range, which is what you expect it to do (looking by the values 4, 5 and 8 in the last two rows of the expected result). I suspectnp.clipis not what you want. Can you elaborate what exactly you're trying to do here, perhaps with steps? I could not understand much just from the question itself.