从列表理解和一般情况下有效创建numpy数组 [英] Efficient creation of numpy arrays from list comprehension and in general

查看:88
本文介绍了从列表理解和一般情况下有效创建numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前的工作中,我经常使用Numpy和列表理解,并且为了获得最佳性能,我有以下问题:

In my current work i use Numpy and list comprehensions a lot and in the interest of best possible performance i have the following questions:

如果我按如下方式创建一个Numpy数组,在幕后实际上会发生什么? :

What actually happens behind the scenes if i create a Numpy array as follows? :

a = numpy.array( [1,2,3,4] )

我的猜测是python首先创建一个包含值的普通列表,然后使用列表大小分配一个numpy数组,然后将值复制到此新数组中.这是正确的,还是解释器足够聪明,以至于意识到列表只是中间的,而是直接复制值?

My guess is that python first creates an ordinary list containing the values, then uses the list size to allocate a numpy array and afterwards copies the values into this new array. Is this correct, or is the interpreter clever enough to realize that the list is only intermediary and instead copy the values directly?

类似地,如果我希望使用numpy.fromiter()从列表理解中创建一个numpy数组:

Similarly, if i wish to create a numpy array from list comprehension using numpy.fromiter():

a = numpy.fromiter( [ x for x in xrange(0,4) ], int )

这会导致在将其输入fromiter()之前创建中间值列表吗?

will this result in an intermediary list of values being created before being fed into fromiter()?

最诚挚的问候 尼尔斯

推荐答案

我相信比您正在寻找的答案更配合使用generator expressions

I believe than answer you are looking for is using generator expressions with numpy.fromiter.

numpy.fromiter((<some_func>(x) for x in <something>),<dtype>,<size of something>)

生成器表达式是惰性的-当您遍历它们时,它们会评估该表达式.

Generator expressions are lazy - they evaluate the expression when you iterate through them.

使用列表推导式制作列表,然后将其输入numpy,而生成器表达式一次生成一个.

Using list comprehensions makes the list, then feeds it into numpy, while generator expressions will yield one at a time.

Python像大多数语言(如果不是全部)一样,对内部->外的内容进行评估,因此使用[<something> for <something_else> in <something_different>]可以创建列表,然后对其进行迭代.

Python evaluates things inside -> out, like most languages (if not all), so using [<something> for <something_else> in <something_different>] would make the list, then iterate over it.

这篇关于从列表理解和一般情况下有效创建numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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