I am new to numpy and trying to replace for loops using np.where
. What I am trying to achieve is simple, I have 4 different conditions and based on these conditions, I am assigning values to elements of the array:
Period = np.arange(0.0,8.0,0.01)
Ta = 0.075
Tb = 0.375
Tl = 6.0
Sds = 1.2
Sd1 = 0.45
Sae = np.zeros(len(Period))
Sae = np.where((Period>=0.0) & (Period<=Ta),0.4+0.6*(Period/Ta)*Sds,Sae)
Sae = np.where((Period>=Ta) & (Period<=Tb),Sds,Sae)
Sae = np.where((Period>=Tb) & (Period<=Tl),Sd1/Period,Sae)
Sae = np.where((Period>=Tl),Sd1*Tl/Period**2,Sae)
And I get RuntimeWarning:
RuntimeWarning: divide by zero encountered in true_divide
Sae = np.where((Period>=Tb) & (Period<=Tl),Sd1/Period,Sae)
RuntimeWarning: divide by zero encountered in true_divide
Sae = np.where((Period>=Tl),Sd1*Tl/Period**2,Sae)
I know Period
array starts with 0.0, but imposed conditions should avoid running into division by zero.
However, there is nothing wrong with the resultant Sae
array. Still I would like not to see division by zero warning.
Thanks!