[英]applying a function to each element in vector in numpy
I am writing some performance critical code.. 我正在写一些性能关键代码。
def sig(x):
return 1/(1+exp(x))
I have a vector 我有一个向量
x = [.... say n elements]
transform_x = [sig(ele) for ele in x]
transform_x = map(lambda ele: sig(ele), x) # bit faster than above loop
But is there a better way to apply the function to achieve the above effect but maybe in vectorized way(which I presume should be better)? 但是有没有更好的方法来应用该功能来实现上述效果,但可能是矢量化的(我认为应该更好)?
您可以像在MATLAB中一样将函数应用于整个数组:
transformed = sig(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.