Issue
I have an array sigma
. If any element of sigma
is less than threshold
, then the value of the specific element is equal to the threshold
. The current and expected outputs are presented.
import numpy as np
sigma=np.array([[ 0.02109 ],
[ 0.01651925],
[ 0.02109 ],
[ 0.02109 ],
[ -0.009 ]])
threshold=0.010545
for i in range(0,len(sigma)):
if(sigma[i]<=threshold):
sigma[i]==threshold
print([sigma])
The current output is
[array([[ 0.02109 ],
[ 0.01651925],
[ 0.02109 ],
[ 0.02109 ],
[-0.009 ]])]
The expected output is
[array([[ 0.02109 ],
[ 0.01651925],
[ 0.02109 ],
[ 0.02109 ],
[0.010545 ]])]
Solution
It is a good start you are doing threshold. In fact, numpy has a good function to help you perform faster, that is np.clip
.
sigma=np.array([[ 0.02109 ],
[ 0.01651925],
[ 0.02109 ],
[ 0.02109 ],
[ -0.009 ]])
threshold=0.010545
np.clip(sigma,threshold,np.inf)
Out[4]:
array([[0.02109 ],
[0.01651925],
[0.02109 ],
[0.02109 ],
[0.010545 ]])
np.clip
is actually a function that limits your array within a boundary(with min and max value). So, if you are performing array elementwise operation, you can use np.clip
to make sure all the elements stay in the boundary.
Answered By - Kevin Choon Liang Yew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.