Python 初始化和填充 numpy 数组的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22414152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:55:21  来源:igfitidea点击:

Best way to initialize and fill an numpy array?

pythonarraysnumpymultidimensional-arrayinitialization

提问by tbc

I want to initialize and fill a numpyarray. What is the best way?

我想初始化并填充一个numpy数组。什么是最好的方法?

This works as I expect:

这按我的预期工作:

>>> import numpy as np
>>> np.empty(3)
array([ -1.28822975e-231,  -1.73060252e-077,   2.23946712e-314])

But this doesn't:

但这不会:

>>> np.empty(3).fill(np.nan)
>>> 

Nothing?

没有?

>>> type(np.empty(3))
<type 'numpy.ndarray'>

It seems to me that the np.empty()call is returning the correct type of object, so I don't understand why .fill()is not working?

在我看来,np.empty()调用正在返回正确类型的对象,所以我不明白为什么.fill()不起作用?

Assigning the result of np.empty()first works fine:

分配np.empty()first的结果工作正常:

>>> a = np.empty(3)
>>> a.fill(np.nan)
>>> a
array([ nan,  nan,  nan])

Why do I need to assign to a variable in order to use np.fill()? Am I missing a better alternative?

为什么我需要分配给一个变量才能使用np.fill()?我错过了更好的选择吗?

采纳答案by shx2

np.fillmodifies the array in-place, and returns None. Therefor, if you're assigning the result to a name, it gets a value of None.

np.fill就地修改数组,并返回None. 因此,如果您将结果分配给名称,则它的值为None

An alternative is to use an expression which returns nan, e.g.:

另一种方法是使用返回的表达式nan,例如:

a = np.empty(3) * np.nan

回答by JoshAdel

You could also try:

你也可以试试:

In [79]: np.full(3, np.nan)
Out[79]: array([ nan,  nan,  nan])

The pertinent doc:

相关文档:

Definition: np.full(shape, fill_value, dtype=None, order='C')
Docstring:
Return a new array of given shape and type, filled with `fill_value`.

Although I think this might be only available in numpy 1.8+

虽然我认为这可能仅在 numpy 1.8+ 中可用

回答by ryanjdillon

I find this easy to remember:

我觉得这很容易记住:

numpy.array([numpy.nan]*3)

Out of curiosity, I timed it, and both @JoshAdel's answerand @shx2's answerare far faster than mine with large arrays.

出于好奇,我给它计时,@JoshAdel 的回答@shx2 的回答都比我的大数组快得多。

In [34]: %timeit -n10000 numpy.array([numpy.nan]*10000)
10000 loops, best of 3: 273 μs per loop

In [35]: %timeit -n10000 numpy.empty(10000)* numpy.nan
10000 loops, best of 3: 6.5 μs per loop

In [36]: %timeit -n10000 numpy.full(10000, numpy.nan)
10000 loops, best of 3: 5.42 μs per loop

回答by norok2

Just for future reference, the multiplication by np.nanonly works because of the mathematical properties of np.nan. For a generic value N, one would need to use np.ones() * Nmimicking the accepted answer, however, speed-wise, this is not a terribly good choice.

仅供将来参考,乘以np.nan仅适用于 的数学特性np.nan。对于通用值N,需要使用np.ones() * N模仿已接受的答案,但是,就速度而言,这不是一个非常好的选择。

Best choice would be np.full()as already pointed out, and, if that is not available for you, np.zeros() + Nseems to be a better choice than np.ones() * N, while np.empty() + Nor np.empty() * Nare simply not suitable. Note that np.zeros() + Nwill also work when Nis np.nan.

最佳选择np.full()如已经指出的那样,如果这对您不可用,则np.zeros() + N似乎是比 更好的选择np.ones() * N,而np.empty() + Nnp.empty() * N根本不适合。请注意,np.zeros() + N这也适用于Nis np.nan

%timeit x = np.full((1000, 1000, 10), 432.4)
8.19 ms ± 97.8 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit x = np.zeros((1000, 1000, 10)) + 432.4
9.86 ms ± 55.1 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit x = np.ones((1000, 1000, 10)) * 432.4
17.3 ms ± 104 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit x = np.array([432.4] * (1000 * 1000 * 10)).reshape((1000, 1000, 10))
316 ms ± 37.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)