初始化数组numpy的 [英] initialize a numpy array
问题描述
有没有方法来初始化形状的numpy的阵列和补充呢?我将解释什么,我需要一个列表的例子。如果我想创建一个循环生成的对象列表,我可以这样做:
A = []
因为我在范围内(5):
a.append㈠
我想要做一个numpy的阵列类似的东西。我知道vstack,连击等。但是,看来这需要两个numpy的数组作为输入。我需要的是:
big_array#最初是空的。这是我不知道要指定哪些
因为我在范围内(5):
阵列I形状的=(2,4)创建。
添加到big_array
的 big_array
应该有一个形状(10,4)
。如何做到这一点?感谢您的帮助。
编辑:我想补充以下澄清。我知道我可以定义 big_array = numpy.zeros((10,4))
,然后填满它。然而,这需要指定预先big_array的大小。我知道在这种情况下的大小,但如果我做什么呢?当我们使用 .append
功能扩展在Python列表中,我们并不需要知道它的提前最终大小。我想知道如果存在从较小的阵列创建一个更大的数组,空数组开始类似的东西。谢谢!
<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html\"><$c$c>numpy.zeros$c$c>
返回给定形状的新的数组
类型,用零填充。
块引用>或
<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html\"><$c$c>numpy.ones$c$c>
返回给定形状的新的数组
型,弥漫着的。
块引用>或
<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.empty.html#numpy.empty\"><$c$c>numpy.empty$c$c>
返回给定形状的新的数组
类型,没有初始化的条目。
块引用>不过,在此我们通过附加元素列表构建阵列的心态没有太大的numpy的使用,因为它的效率较低(numpy的数据类型是更接近底层C数组)。相反,你应该preallocate数组,你需要它的大小,然后在填写行。您可以使用
numpy.append
如果你一定要,但。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:
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:
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_array
should have a shape(10,4)
. How to do this? Thanks for your help.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.append
function 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. Thanks!解决方案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.
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.append
if you must, though.这篇关于初始化数组numpy的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!