为什么这个 numpy 数组操作这么慢?
我是一个 python 初学者,我正在尝试对形状为 (1024,1024) 的两个 NumPy 2D 数组进行平均。这样做非常快:
newImage = (image1 + image2) / 2
但是现在图像有一个“掩码”,如果设置为零,则某些元素会失效。这意味着如果其中一个元素为零,则结果元素也应该为零。我的简单解决方案是:
newImage = numpy.zeros( (1024,1024) , dtype=numpy.int16 )
for y in xrange(newImage.shape[0]):
for x in xrange(newImage.shape[1]):
val1 = image1[y][x]
val2 = image2[y][x]
if val1!=0 and val2!=0:
newImage[y][x] = (val1 + val2) / 2
但这真的很慢。我没有计时,但它似乎慢了 100 倍。
我还尝试使用 lambda 运算符和“map”,但这不会返回 NumPy 数组。
I am a python beginner and I am trying to average two NumPy 2D arrays with shape of (1024,1024). Doing it like this is quite fast:
newImage = (image1 + image2) / 2
But now the images have a "mask" that invalidate certain elements if set to zero. That means if one of the elements is zero, the resulting element should also be zero. My trivial solution is:
newImage = numpy.zeros( (1024,1024) , dtype=numpy.int16 )
for y in xrange(newImage.shape[0]):
for x in xrange(newImage.shape[1]):
val1 = image1[y][x]
val2 = image2[y][x]
if val1!=0 and val2!=0:
newImage[y][x] = (val1 + val2) / 2
But this is really slow. I did not time it, but it seems to be slower by a factor of 100.
I also tried using a lambda operator and "map", but this does not return a NumPy array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
如果
image1
和image2
都不为零,则取其平均值,否则为零。Try this:
Where none of
image1
andimage2
equals zero, take their mean, otherwise zero.使用本机 Python 代码循环通常比使用循环慢得多
使用快速 C 循环的内置工具。我对 NumPy 不熟悉;能
您使用
map()
将两个输入数组转换为输出?如果是这样的话,应该会更快。
Looping with native Python code is generally much slower than using
built-in tools that use fast C loops. I'm not familiar with NumPy; can
you use
map()
to do a transformation from your two input arrays tothe output? If so, that should be faster.
一般来说,显式
for
循环在 Python 中效率非常低,不仅对于numpy
操作而言。幸运的是,有更快的方法来解决我们的问题。如果内存不是问题,这个解决方案非常好:另一种使用屏蔽数组的解决方案,它不会创建数组的副本(它们代表原始
image1/2
的视图:更新:对于具有大约 1000 个非零条目的数组,第一个解决方案似乎比第二个解决方案快 3 倍。
Explicit
for
loops are very inefficient in Python in general, not only fornumpy
operations. Fortunately, there are faster ways to solve our problem. If memory is not an issue, this solution is quite good:Another solution using masked arrays, which do not create copies of the arrays (they represent views of the original
image1/2
:Update: The first solution seems to be 3 times faster than the second for arrays with about 1000 non-zero entries.
numpy 数组访问操作充其量看起来很慢。我看不出有什么理由。通过构造一个简单的例子就可以清楚地看到:
结果:[0.21972250938415527, 0.15950298309326172]
numpy array access operation seems slow at best. I can't see any reason for it. You can clearly see it by constructing a simple example:
Result: [0.21972250938415527, 0.15950298309326172]