How to apply condition statement using numpy library in Ipython?

ipython

#1

I have created a matrix using randn function in Ipython and wanted to replace all positive values with 2 and all negative values with -2 for doing this I have to use if else condition in Ipython which I am not able to perform .

In [148]: arr
Out[148]:
array([[ 0.6372, 2.2043, 1.7904, 0.0752],
[-1.5926, -1.1536, 0.4413, 0.3483],
[-0.1798, 0.3299, 0.7827, -0.7585],
[ 0.5857, 0.1619, 1.3583, -1.3865]])

#2

@sid100158-
You can use the function np.where for performing conditional statement in Ipython
the syntax is

result = np.where(cond, xarr, yarr) 

cond- is the condition to apply

xarr- is the array or value want to replace with if the condition is true,

yarr- is the array or value want to replace with if the condition is false.

For this question, the answer would be

np.where(arr>0,2,-2)

Hope this helps!

Regards,
Hinduja