映射到位numpy的阵列 [英] Mapping a NumPy array in place

查看:143
本文介绍了映射到位numpy的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能映射代替numpy的阵列?如果是,如何?

由于 a_values​​ - 二维数组 - 这是code的,做的把戏,我此刻的位:

Given a_values - 2D array - this is the bit of code that does the trick for me at the moment:

for row in range(len(a_values)):
    for col in range(len(a_values[0])):
        a_values[row][col] = dim(a_values[row][col])

但它是如此的丑陋,我怀疑是某处内numpy的,必须有做同样的东西看起来像一个函数:

But it's so ugly that I suspect that somewhere within NumPy there must be a function that does the same with something looking like:

a_values.map_in_place(dim)

但如果像上面的存在,我一直无法找到它。

but if something like the above exists, I've been unable to find it.

推荐答案

这是唯一值得一试,如果你正在显著空间的限制做到这一点在地方。如果是这样的情况下,可以通过遍历数组的平面视图,以加快code一点点。由于重塑返回一个新视图的时可能时,数据本身不复制(除非原具有不寻常的结构)。

It's only worth trying to do this in-place if you are under significant space constraints. If that's the case, it is possible to speed up your code a little bit by iterating over a flattened view of the array. Since reshape returns a new view when possible, the data itself isn't copied (unless the original has unusual structure).

我不知道一个更好的方式来实现真正的就地应用程序中的任意的Python功能。

I don't know of a better way to achieve bona fide in-place application of an arbitrary Python function.

>>> def flat_for(a, f):
...     a = a.reshape(-1)
...     for i, v in enumerate(a):
...         a[i] = f(v)
... 
>>> a = numpy.arange(25).reshape(5, 5)
>>> flat_for(a, lambda x: x + 5)
>>> a

array([[ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29]])

有些时段:

>>> a = numpy.arange(2500).reshape(50, 50)
>>> f = lambda x: x + 5
>>> %timeit flat_for(a, f)
1000 loops, best of 3: 1.86 ms per loop

这是关于快两倍,嵌套循环的版本:

It's about twice as fast as the nested loop version:

>>> a = numpy.arange(2500).reshape(50, 50)
>>> def nested_for(a, f):
...     for i in range(len(a)):
...         for j in range(len(a[0])):
...             a[i][j] = f(a[i][j])
... 
>>> %timeit nested_for(a, f)
100 loops, best of 3: 3.79 ms per loop

当然矢量仍然是比较快,所以如果你可以复制,使用:

Of course vectorize is still faster, so if you can make a copy, use that:

>>> a = numpy.arange(2500).reshape(50, 50)
>>> g = numpy.vectorize(lambda x: x + 5)
>>> %timeit g(a)
1000 loops, best of 3: 584 us per loop

如果你可以重写昏暗使用内置ufuncs,那么请你,请不要矢量

And if you can rewrite dim using built-in ufuncs, then please, please, don't vectorize:

>>> a = numpy.arange(2500).reshape(50, 50)
>>> %timeit a + 5
100000 loops, best of 3: 4.66 us per loop

numpy的不喜欢 + = 操作的地方,就像你所期望的 - 这样你就可以得到与不花钱就地应用ufunc的速度。有时甚至更快!请参见这里一个例子。

numpy does operations like += in place, just as you might expect -- so you can get the speed of a ufunc with in-place application at no cost. Sometimes it's even faster! See here for an example.

顺便说一句,我原来的回答这个问题,可以在它的编辑历史被看作是可笑的,并参与了量化指标为 A 。它不仅必须做一些时髦的东西绕过的矢量类型的检测机制,它竟然是一样的嵌套循环的版本一样慢。这么多聪明!

By the way, my original answer to this question, which can be viewed in its edit history, is ridiculous, and involved vectorizing over indices into a. Not only did it have to do some funky stuff to bypass vectorize's type-detection mechanism, it turned out to be just as slow as the nested loop version. So much for cleverness!

这篇关于映射到位numpy的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆