Python 将元素添加到 numpy 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36998260/
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
Prepend element to numpy array
提问by piRSquared
I have the following numpy array
我有以下 numpy 数组
import numpy as np
X = np.array([[5.], [4.], [3.], [2.], [1.]])
I want to insert [6.]
at the beginning.
I've tried:
我想[6.]
在开头插入。我试过了:
X = X.insert(X, 0)
how do I insert into X?
我如何插入到 X 中?
回答by Rosey
numpy has an insert
function that's accesible via np.insert
with documentation.
numpy 有一个insert
可以通过np.insert
with documentation访问的功能。
You'll want to use it in this case like so:
在这种情况下,您需要像这样使用它:
X = np.insert(X, 0, 6., axis=0)
the first argument X
specifies the object to be inserted into.
第一个参数X
指定要插入的对象。
The second argument 0
specifies where.
第二个参数0
指定在哪里。
The third argument 6.
specifies what is to be inserted.
第三个参数6.
指定要插入的内容。
The fourth argument axis=0
specifies that the insertion should happen at position 0
for every column. We could've chosen rows but your X is a columns vector, so I figured we'd stay consistent.
第四个参数axis=0
指定插入应该发生在0
每一列的位置。我们可以选择行,但您的 X 是列向量,所以我认为我们会保持一致。
回答by jbf81tb
I just wrote some code that does this operation ~100,000 times, so I needed to figure out the fastest way to do this. I'm not an expert in code efficiency by any means, but I could figure some things out by using the %%timeit
magic function in a jupyter notebook.
我只是写了一些代码来执行这个操作 ~100,000 次,所以我需要找出最快的方法来做到这一点。我无论如何都不是代码效率方面的专家,但我可以通过%%timeit
在 jupyter notebook 中使用魔法函数来解决一些问题。
My findings:
我的发现:
np.concatenate(([number],array))
requires the least time. Let's call it 1x time.
np.concatenate(([number],array))
需要最少的时间。我们称之为 1x 次。
np.asarray([number] + list(array))
comes in at ~2x.
np.asarray([number] + list(array))
进来〜2x。
np.r_[number,array]
is ~4x.
np.r_[number,array]
是 ~4 倍。
np.insert(array,0,number)
appears to be the worst option here at 8x.
np.insert(array,0,number)
在 8 倍时似乎是最糟糕的选择。
I have no idea how this changes with the size of array
(I used a shape (15,) array) and most of the options I suggested only work if you want to put the number at the beginning. However, since that's what the question is asking about, I figure this is a good place to make these comparisons.
我不知道这会如何随array
(我使用形状 (15,) 数组)的大小而变化,而且我建议的大多数选项仅在您想将数字放在开头时才有效。但是,由于这就是问题所问的问题,我认为这是进行这些比较的好地方。
回答by Okroshiashvili
You can try the following
您可以尝试以下操作
X = np.append(arr = np.array([[6]]), values = X, axis= 0)
Instead of inserting 6 to the existing X, let append 6 by X.
不是将 6 插入到现有的 X 中,而是通过 X 附加 6。
So, first argument arr
is numpy array of scalar 6, second argument is your array to be added, and third is the place where we want to add
因此,第一个参数arr
是标量 6 的 numpy 数组,第二个参数是要添加的数组,第三个是我们要添加的位置
回答by T0eJam
I know this is a fairly old one, but a short solution is using numpy slicing tricks:
我知道这是一个相当古老的方法,但一个简短的解决方案是使用 numpy 切片技巧:
np.r_[[[6.]], X]
If you need to do it in a second dimension you can use np.c_.
如果您需要在第二维中进行操作,您可以使用 np.c_。
I think this is the least cluttered version I can think of
我认为这是我能想到的最不杂乱的版本