本文介绍了为什么我不能复制NumPy v1.13.dev0手册示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为示例,我正在阅读以下内容:

As an example I am reading through the following:

https://docs.scipy.org /doc/numpy-dev/neps/new-iterator-ufunc.html

因此,我使用iPython在计算机上运行了一些代码,例如在手册中显示为:

So I run a bit of code on my computer using iPython shown for example in the manual as:

   def iter_add_itview(x, y, out=None):
        it = np.nditer([x,y,out], [],
                    [['readonly'],['readonly'],['writeonly','allocate']])

        (a, b, c) = it.itviews
        np.add(a, b, c)

        return it.operands[2]

根据每个示例得出的测试案例如下:

Which per the example results in tests cases as follows:

In [10]: a = np.arange(1000000,dtype='f4').reshape(100,100,100).T
In [12]: b = np.arange(10000,dtype='f4').reshape(100,100,1).T
In [11]: c = np.arange(10000,dtype='f4').reshape(1,100,100).T

In [4]: timeit np.add(np.add(np.add(a,b), c), a)
1 loops, best of 3: 99.5 ms per loop

In [9]: timeit iter_add_itview(iter_add_itview(iter_add_itview(a,b), c), a)
10 loops, best of 3: 29.3 ms per loop

因此,我自然希望自己在使用Intel芯片组的Linux上的Python 2.7中使用NumPy1.12.1时感到兴奋,唯一的问题是,对于与上述示例完全相同的实验设置,我始终会得到空结果:

SO naturally I want to try this excitement for myself on using NumPy1.12.1 in Python 2.7 on Linux with an Intel chipset, the only problem is I consistently get null results for the exact same experimental setups as in the example above:

In [12]: timeit np.add(np.add(np.add(a,b), c), a)
100 loops, best of 3: 10.7 ms per loop

In [13]: timeit iter_add_itview(iter_add_itview(iter_add_itvie
    ...: w(a,b), c), a)
100 loops, best of 3: 10.7 ms per loop

在这种情况下,我应该看到缓冲区缓存优化带来的改进.

In this case I was supposed to be seeing improvements from buffer cache optimization.

为什么我无法使用旧版代码为更新的NumPy版本复制开发手册的一部分?

Why is it that I am not able to replicate a section of the dev manual for a updated NumPy version using a legacy version of the code?

推荐答案

仅需清除In[4][9]时间是从该6年旧文档中复制的. [12][13]时间来自您自己的测试(与我得到的基本上相同).我没有研究过旧的dev文档,但是在示例上运行了

Just to be clear the In[4] and [9] times are copied from that 6 yr old document. The [12][13] times are from your own tests (and basically the same as what I get). I haven't studied that old dev document, but have run the np.nditer examples on

https://docs.scipy.org/doc/numpy /reference/arrays.nditer.html

,并且知道nditer通常不会加速代码.该迭代器应在C级代码中使用,它在Python级的公开很方便.它使我们可以在将想法转移到C或Cython之前,用Python代码对其进行测试.请注意最后的Cython示例.即便如此,我发现通过使用Cython内存视图,在一个简单的乘法情况下我可以获得更好的速度.

and know that nditer does not normally speed code. This iterator is meant to be used in C level code, and its exposure at the Python level is a convenience. It lets us test ideas in Python code before moving them to C or Cython. Note the Cython example at the end. Even so, I found I could get better speed in a simple multiplication case by using Cython memoryviews.

np.ndindex是在Python代码中使用np.nditer的少数numpy函数之一.我偶尔建议使用类似的模式来产生深度受限的迭代.

np.ndindex is one of the few numpy functions that uses np.nditer in Python code. I've occasionally suggested a similar pattern to produce depth limited iteration.

因此,不必太担心精通np.nditer.

So don't worry too much about mastering np.nditer.

这篇关于为什么我不能复制NumPy v1.13.dev0手册示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 01:44