问题描述
假设我有两个NumPy数组,分别是 a
和 b
:
Let’s say I have two NumPy arrays, a
and b
:
a = np.array([
[1, 2, 3],
[2, 3, 4]
])
b = np.array([8,9])
我想将相同的数组 b
附加到每一行(即添加多列)以获得一个数组, c
:
And I would like to append the same array b
to every row (ie. adding multiple columns) to get an array, c
:
b = np.array([
[1, 2, 3, 8, 9],
[2, 3, 4, 8, 9]
])
如何在NumPy中轻松高效地做到这一点?
How can I do this easily and efficiently in NumPy?
我特别担心它在大型数据集(其中 a
比 b
大)的行为,是否有办法创建多个副本(即
)? b
的> a.shape [0]
I am especially concerned about its behaviour with big datasets (where a
is much bigger than b
), is there any way around creating many copies (ie. a.shape[0]
) of b
?
与此问题有关,但具有多个值.
Related to this question, but with multiple values.
推荐答案
concatenate
方法的一种替代方法是制作一个收件人数组,并将值复制到其中:
An alternative to concatenate
approach is to make a recipient array, and copy values to it:
In [483]: a = np.arange(300).reshape(100,3)
In [484]: b=np.array([8,9])
In [485]: res = np.zeros((100,5),int)
In [486]: res[:,:3]=a
In [487]: res[:,3:]=b
采样时间
sample timings
In [488]: %%timeit
...: res = np.zeros((100,5),int)
...: res[:,:3]=a
...: res[:,3:]=b
...:
...:
6.11 µs ± 20.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [491]: timeit np.concatenate((a, b.repeat(100).reshape(2,-1).T),1)
7.74 µs ± 15.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [164]: timeit np.concatenate([a, np.ones([a.shape[0],1], dtype=int).dot(np.array([b]))], axis=1)
8.58 µs ± 160 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
这篇关于如何将多个额外的列添加到NumPy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!