问题:将单个元素添加到numpy中的数组

我有一个numpy数组,其中包含:

[1, 2, 3]

我想创建一个包含以下内容的数组:

[1, 2, 3, 1]

也就是说,我想将第一个元素添加到数组的末尾。

我尝试了明显的方法:

np.concatenate((a, a[0]))

但是我说错了 ValueError: arrays must have same number of dimensions

我不明白这一点-数组都是一维数组。

I have a numpy array containing:

[1, 2, 3]

I want to create an array containing:

[1, 2, 3, 1]

That is, I want to add the first element on to the end of the array.

I have tried the obvious:

np.concatenate((a, a[0]))

But I get an error saying ValueError: arrays must have same number of dimensions

I don’t understand this – the arrays are both just 1d arrays.


回答 0

append() 创建一个新数组,该数组可以是带有附加元素的旧数组。

我认为使用适当的方法添加元素更为正常:

a = numpy.append(a, a[0])

append() creates a new array which can be the old array with the appended element.

I think it’s more normal to use the proper method for adding an element:

a = numpy.append(a, a[0])

回答 1

如果仅一次或一次附加一次,则np.append在数组上使用应该没问题。这种方法的缺点是每次调用都会为一个全新的数组分配内存。当为大量样本增加数组时,最好预先分配数组(如果知道总大小),或者追加到列表中,然后再转换为数组。

使用np.append

b = np.array([0])
for k in range(int(10e4)):
    b = np.append(b, k)
1.2 s ± 16.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

之后使用python列表转换为数组:

d = [0]
for k in range(int(10e4)):
    d.append(k)
f = np.array(d)
13.5 ms ± 277 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

预分配numpy数组:

e = np.zeros((n,))
for k in range(n):
    e[k] = k
9.92 ms ± 752 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

当最终大小未知时,很难进行预分配,我尝试了以50个块为单位进行预分配,但它几乎无法使用列表。

85.1 ms ± 561 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

When appending only once or once every now and again, using np.append on your array should be fine. The drawback of this approach is that memory is allocated for a completely new array every time it is called. When growing an array for a significant amount of samples it would be better to either pre-allocate the array (if the total size is known) or to append to a list and convert to an array afterward.

Using np.append:

b = np.array([0])
for k in range(int(10e4)):
    b = np.append(b, k)
1.2 s ± 16.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Using python list converting to array afterward:

d = [0]
for k in range(int(10e4)):
    d.append(k)
f = np.array(d)
13.5 ms ± 277 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Pre-allocating numpy array:

e = np.zeros((n,))
for k in range(n):
    e[k] = k
9.92 ms ± 752 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

When the final size is unkown pre-allocating is difficult, I tried pre-allocating in chunks of 50 but it did not come close to using a list.

85.1 ms ± 561 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

回答 2

a[0]不是数组,它是的第一个元素,a因此没有尺寸。

尝试a[0:1]改用,这将返回a单个项目数组内的第一个元素。

a[0] isn’t an array, it’s the first element of a and therefore has no dimensions.

Try using a[0:1] instead, which will return the first element of a inside a single item array.


回答 3

试试这个:

np.concatenate((a, np.array([a[0]])))

http://docs.scipy.org/doc/numpy/reference/generation/numpy.concatenate.html

连接需要两个元素都是numpy数组;但是,[0]不是数组。这就是为什么它不起作用。

Try this:

np.concatenate((a, np.array([a[0]])))

http://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html

concatenate needs both elements to be numpy arrays; however, a[0] is not an array. That is why it does not work.


回答 4

这个命令

numpy.append(a, a[0])

不会改变a数组。但是,它返回一个新的修改后的数组。因此,如果a需要修改,则必须使用以下内容。

a = numpy.append(a, a[0])

This command,

numpy.append(a, a[0])

does not alter a array. However, it returns a new modified array. So, if a modification is required, then the following must be used.

a = numpy.append(a, a[0])

回答 5

t = np.array([2, 3])
t = np.append(t, [4])
t = np.array([2, 3])
t = np.append(t, [4])

回答 6

这可能有点矫kill过正,但是我始终将np.take函数用于任何环绕索引:

>>> a = np.array([1, 2, 3])
>>> np.take(a, range(0, len(a)+1), mode='wrap')
array([1, 2, 3, 1])

>>> np.take(a, range(-1, len(a)+1), mode='wrap')
array([3, 1, 2, 3, 1])

This might be a bit overkill, but I always use the the np.take function for any wrap-around indexing:

>>> a = np.array([1, 2, 3])
>>> np.take(a, range(0, len(a)+1), mode='wrap')
array([1, 2, 3, 1])

>>> np.take(a, range(-1, len(a)+1), mode='wrap')
array([3, 1, 2, 3, 1])

回答 7

假设a=[1,2,3]您希望它成为[1,2,3,1]

您可以使用内置的附加功能

np.append(a,1)

这里1是一个整数,它可以是字符串,并且可以属于或不属于数组中的元素。印刷品:[1,2,3,1]

Let’s say a=[1,2,3] and you want it to be [1,2,3,1].

You may use the built-in append function

np.append(a,1)

Here 1 is an int, it may be a string and it may or may not belong to the elements in the array. Prints: [1,2,3,1]


回答 8

如果要添加元素,请使用 append()

a = numpy.append(a, 1) 在这种情况下,在数组末尾添加1

如果要插入元素,请使用 insert()

a = numpy.insert(a, index, 1) 在这种情况下,您可以将1放置在所需的位置,并使用index设置数组中的位置。

If you want to add an element use append()

a = numpy.append(a, 1) in this case add the 1 at the end of the array

If you want to insert an element use insert()

a = numpy.insert(a, index, 1) in this case you can put the 1 where you desire, using index to set the position in the array.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。