简体   繁体   English

使用 numpy.nditer 访问不同的单元格

[英]Accessing different cells using numpy.nditer

I have the following code我有以下代码

a1 = 0.5
b0 = 3
ui_delta = np.arange(100000)
for i in range(1, ui_delta.shape[0]):
    ui_delta[i] = b0 * ui_delta[i] + a1 * ui_delta[i-1]

Would it be possible to generate this code using nditer or other instruction from numpy?是否可以使用 nditer 或来自 numpy 的其他指令生成此代码? I am trying to accelerate the code, is quite slow with the number of values I am working with.我正在尝试加速代码,对于我正在使用的值的数量来说速度非常慢。 Thanks谢谢

How about you use numba instead of numpy to speet things up.您如何使用 numba 而不是 numpy 来加快速度。

@nb.njit
def calc():
    a1 = 0.5
    b0 = 3
    ui_delta = np.arange(n)
    for i in range(1, ui_delta.shape[0]):
        ui_delta[i] = b0 * ui_delta[i] + a1 * ui_delta[i-1]
    
    return ui_delta
calc()

This takes ~88.1 ms with the @nb.njit and ~393 ms without it.使用 @nb.njit 需要 ~88.1 ms,没有它需要 ~393 ms。 So it's a more than 4 times speed up.所以它的速度提高了 4 倍多。

The fastest and more Numpy-like way would be using array views .最快和更像 Numpy 的方法是使用数组视图
If you look at the procedure from a perspective of the whole array, what you are trying to do is take the array except the first item and add it to itself but without the last item.如果你从整个数组的角度来看这个过程,你试图做的是把除了第一项之外的数组添加到自身,但没有最后一项。 Basically you sum your array with itself but with a 1-element shift.基本上,您将数组与自身相加,但进行 1 个元素的移位。

Numpy provides a very handy feature called views , that allow having a reference or a view to a part of an array. Numpy 提供了一个非常方便的功能,称为views ,它允许对数组的一部分进行引用或查看 This means, that changing the elements in the view will also change the original array.这意味着,更改视图中的元素也会更改原始数组。

a1 = 0.5
b0 = 3
ui_delta = np.arange(100000)
ui_delta1 = ui_delta[1:] # choosing all but the first element
ui_delta2 = ui_delta[:-1] # all but the last element
ui_delta1[:] = b0 * ui_delta1 + a1 * ui_delta2 

Using whole array operations not only simplifies code, but it is dramatically faster than iteration.使用整个数组操作不仅可以简化代码,而且比迭代快得多。 Numpy has very efficient internal array loops that run extremely fast, because they are written in С under the hood. Numpy 具有非常高效的内部数组循环,运行速度极快,因为它们是在引擎盖下用 С 编写的。 The bigger the array - the more significant is the efficiency gain.阵列越大 - 效率增益越显着。

Your code executes in roughly ~1 s The code above executes in just ~0.4 ms您的代码大约在 ~1 秒内执行 上面的代码仅在 ~0.4 毫秒内执行

Thank you very much.非常感谢你。 The solution with numby, find it quite good.用numby的解决方案,觉得还不错。 On the other side, I still do not find the same results when using numpy This is the numpy solution array([ 0, 3, 6, ..., 349989, 349992, 349996])另一方面,当使用 numpy 时,我仍然没有找到相同的结果 这是 numpy 解决方案数组([ 0, 3, 6, ..., 349989, 349992, 349996])

Using the numby solution the result is correct.使用 numby 解决方案,结果是正确的。 array([ 0, 3, 7, ..., 599976, 599982, 599988], dtype=int64) array([ 0, 3, 7, ..., 599976, 599982, 599988], dtype=int64)

I see now your comments about my answer.我现在看到您对我的回答的评论。

What I am trying to say is that the following solution using Numpy is still not OK.我想说的是,以下使用 Numpy 的解决方案仍然不行。

a1 = 0.5
b0 = 3
ui_delta = np.arange(100000)
ui_delta1 =    ui_delta[1:] # choosing all but the first element    
ui_delta2 =    ui_delta[:-1] # all but the last element    
ui_delta1[:] = b0 *    ui_delta1 + a1 * ui_delta2

The result for this is,结果是,

array([ 0, 3, 6, ..., 349989, 349992, 349996])阵列([ 0, 3, 6, ..., 349989, 349992, 349996])

When should be什么时候应该

array([ 0, 3, 7, ..., 599976, 599982, 599988])阵列([ 0, 3, 7, ..., 599976, 599982, 599988])

Despite it, the solution using Numba is quite good.尽管如此,使用 Numba 的解决方案还是相当不错的。 Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM