Anano to numpy switch

In anano, I have a vector with values ​​around 0 and using switch, I set each value less than 0 to 0:

T.switch(x < 0, 0, x) 

How to do the same with numpy?

+5
source share
2 answers

I think you are looking for something like this:

 import numpy as np a= np.array([0,1,-12]) a[a<0]= 0 print a >> [0,1,0] 

So, the key is to a[a<0] find all the negative elements.

+3
source

Answer the numpy switch analogue question

 numpy.where(x < 0, 0, x) 

But especially for this task, I prefer to use a function created for this purpose

 x.clip(0) 
+4
source