Python 初始化一个 numpy 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4535374/
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
initialize a numpy array
提问by Curious2learn
Is there way to initialize a numpy array of a shape and add to it? I will explain what I need with a list example. If I want to create a list of objects generated in a loop, I can do:
有没有办法初始化一个形状的 numpy 数组并添加到它?我将用一个列表示例来解释我需要什么。如果我想创建一个循环生成的对象列表,我可以这样做:
a = []
for i in range(5):
a.append(i)
I want to do something similar with a numpy array. I know about vstack, concatenate etc. However, it seems these require two numpy arrays as inputs. What I need is:
我想用 numpy 数组做类似的事情。我知道 vstack、concatenate 等。但是,这些似乎需要两个 numpy 数组作为输入。我需要的是:
big_array # Initially empty. This is where I don't know what to specify
for i in range(5):
array i of shape = (2,4) created.
add to big_array
The big_arrayshould have a shape (10,4). How to do this?
本big_array应具有的形状(10,4)。这该怎么做?
EDIT:
编辑:
I want to add the following clarification. I am aware that I can define big_array = numpy.zeros((10,4))and then fill it up. However, this requires specifying the size of big_array in advance. I know the size in this case, but what if I do not? When we use the .appendfunction for extending the list in python, we don't need to know its final size in advance. I am wondering if something similar exists for creating a bigger array from smaller arrays, starting with an empty array.
我想添加以下说明。我知道我可以定义big_array = numpy.zeros((10,4))然后填写。但是,这需要提前指定 big_array 的大小。我知道这种情况下的尺寸,但如果我不知道怎么办?当我们.append在python中使用扩展列表的函数时,我们不需要提前知道它的最终大小。我想知道是否存在类似的东西,用于从较小的数组创建一个较大的数组,从一个空数组开始。
采纳答案by Katriel
Return a new array of given shape and type, filled with zeros.
返回一个给定形状和类型的新数组,用零填充。
or
或者
Return a new array of given shape and type, filled with ones.
返回一个给定形状和类型的新数组,填充为 1。
or
或者
Return a new array of given shape and type, without initializing entries.
返回给定形状和类型的新数组,而不初始化条目。
However, the mentality in which we construct an array by appending elements to a list is not much used in numpy, because it's less efficient (numpy datatypes are much closer to the underlying C arrays). Instead, you should preallocate the array to the size that you need it to be, and then fill in the rows. You can use numpy.appendif you must, though.
然而,我们通过将元素附加到列表来构造数组的心态在 numpy 中并没有太多使用,因为它的效率较低(numpy 数据类型更接近底层 C 数组)。相反,您应该将数组预先分配到您需要的大小,然后填充行。numpy.append不过,如果必须,您可以使用。
回答by Andreas L?ve Selvik
You do want to avoid explicit loops as much as possible when doing array computing, as that reduces the speed gain from that form of computing. There are multiple ways to initialize a numpy array. If you want it filled with zeros, do as katrielalex said:
在进行数组计算时,您确实希望尽可能避免显式循环,因为这会降低这种计算形式的速度增益。有多种方法可以初始化 numpy 数组。如果你想用零填充,按照 katrielalex 说的做:
big_array = numpy.zeros((10,4))
big_array = numpy.zeros((10,4))
EDIT: What sort of sequence is it you're making? You should check out the different numpy functions that create arrays, like numpy.linspace(start, stop, size)(equally spaced number), or numpy.arange(start, stop, inc). Where possible, these functions will make arrays substantially faster than doing the same work in explicit loops
编辑:你正在制作什么样的序列?您应该查看创建数组的不同 numpy 函数,例如numpy.linspace(start, stop, size)(等距数字) 或numpy.arange(start, stop, inc). 在可能的情况下,这些函数将使数组比在显式循环中执行相同的工作快得多
回答by tom10
For your first array example use,
对于您的第一个数组示例使用,
a = numpy.arange(5)
To initialize big_array, use
要初始化 big_array,请使用
big_array = numpy.zeros((10,4))
This assumes you want to initialize with zeros, which is pretty typical, but there are many other ways to initialize an array in numpy.
这假设您想用零初始化,这是非常典型的,但还有许多其他方法可以在 numpy 中初始化数组。
Edit:If you don't know the size of big_array in advance, it's generally best to first build a Python list using append, and when you have everything collected in the list, convert this list to a numpy array using numpy.array(mylist). The reason for this is that lists are meant to grow very efficiently and quickly, whereas numpy.concatenate would be very inefficient since numpy arrays don't change size easily. But once everything is collected in a list, and you know the final array size, a numpy array can be efficiently constructed.
编辑:如果您事先不知道 big_array 的大小,通常最好先使用 append 构建一个 Python 列表,当您收集到列表中的所有内容后,使用numpy.array(mylist). 这样做的原因是列表旨在非常有效和快速地增长,而 numpy.concatenate 会非常低效,因为 numpy 数组不会轻易改变大小。但是一旦将所有内容都收集到一个列表中,并且您知道最终的数组大小,就可以有效地构建一个 numpy 数组。
回答by mad7777
The way I usually do that is by creating a regular list, then append my stuff into it, and finally transform the list to a numpy array as follows :
我通常这样做的方法是创建一个常规列表,然后将我的东西附加到其中,最后将列表转换为一个 numpy 数组,如下所示:
import numpy as np
big_array = [] # empty regular list
for i in range(5):
arr = i*np.ones((2,4)) # for instance
big_array.append(arr)
big_np_array = np.array(big_array) # transformed to a numpy array
of course your final object takes twice the space in the memory at the creation step, but appending on python list is very fast, and creation using np.array() also.
当然,您的最终对象在创建步骤中占用了两倍的内存空间,但是附加到 python 列表中非常快,并且使用 np.array() 创建也是如此。
回答by Quant Metropolis
numpy.fromiter()is what you are looking for:
numpy.fromiter()是你要找的:
big_array = numpy.fromiter(xrange(5), dtype="int")
It also works with generator expressions, e.g.:
它也适用于生成器表达式,例如:
big_array = numpy.fromiter( (i*(i+1)/2 for i in xrange(5)), dtype="int" )
If you know the length of the array in advance, you can specify it with an optional 'count' argument.
如果您事先知道数组的长度,则可以使用可选的 'count' 参数指定它。
回答by Adobe
Array analogue for the python's
python的数组模拟
a = []
for i in range(5):
a.append(i)
is:
是:
import numpy as np
a = np.empty((0))
for i in range(5):
a = np.append(a, i)
回答by Mad Physicist
I realize that this is a bit late, but I did not notice any of the other answers mentioning indexing into the empty array:
我意识到这有点晚了,但我没有注意到任何其他提到索引到空数组的答案:
big_array = numpy.empty(10, 4)
for i in range(5):
array_i = numpy.random.random(2, 4)
big_array[2 * i:2 * (i + 1), :] = array_i
This way, you preallocate the entire result array with numpy.emptyand fill in the rows as you go using indexed assignment.
这样,您numpy.empty可以使用索引分配预先分配整个结果数组并在行中填充行。
It is perfectly safe to preallocate with emptyinstead of zerosin the example you gave since you are guaranteeing that the entire array will be filled with the chunks you generate.
在您提供的示例中使用empty而不是预分配是完全安全的,zeros因为您保证整个数组将被您生成的块填充。
回答by Franck Dernoncourt
Introduced in numpy 1.8:
在 numpy 1.8 中引入:
Return a new array of given shape and type, filled with fill_value.
返回一个给定形状和类型的新数组,填充了 fill_value。
Examples:
例子:
>>> import numpy as np
>>> np.full((2, 2), np.inf)
array([[ inf, inf],
[ inf, inf]])
>>> np.full((2, 2), 10)
array([[10, 10],
[10, 10]])
回答by GT GT
I'd suggest defining shape first. Then iterate over it to insert values.
我建议先定义形状。然后迭代它以插入值。
big_array= np.zeros(shape = ( 6, 2 ))
for it in range(6):
big_array[it] = (it,it) # For example
>>>big_array
array([[ 0., 0.],
[ 1., 1.],
[ 2., 2.],
[ 3., 3.],
[ 4., 4.],
[ 5., 5.]])
回答by Heapify
Whenever you are in the following situation:
每当您处于以下情况时:
a = []
for i in range(5):
a.append(i)
and you want something similar in numpy, several previous answers have pointed out ways to do it, but as @katrielalex pointed out these methods are not efficient. The efficient way to do this is to build a long list and then reshape it the way you want after you have a long list. For example, let's say I am reading some lines from a file and each row has a list of numbers and I want to build a numpy array of shape (number of lines read, length of vector in each row). Here is how I would do it more efficiently:
并且您想要在 numpy 中使用类似的东西,之前的几个答案已经指出了这样做的方法,但是正如@katrielalex 指出的那样,这些方法效率不高。做到这一点的有效方法是建立一个长列表,然后在你有一个长列表后按照你想要的方式重新调整它。例如,假设我正在从文件中读取一些行,每一行都有一个数字列表,我想构建一个形状的 numpy 数组(读取的行数,每行向量的长度)。以下是我如何更有效地做到这一点:
long_list = []
counter = 0
with open('filename', 'r') as f:
for row in f:
row_list = row.split()
long_list.extend(row_list)
counter++
# now we have a long list and we are ready to reshape
result = np.array(long_list).reshape(counter, len(row_list)) # desired numpy array

