[英]Concatenate 1D and 2D arrays as per index position
I am trying to concatenate a 1D into a 2D array.我正在尝试将 1D 连接到 2D 数组中。 I'd like to avoid doing a loop as it's very computer intensive if my array lengths are greater than 1000.
我想避免执行循环,因为如果我的数组长度大于 1000,它会占用大量计算机资源。
I have tried vstack, stack and concatenate with no success.我尝试过 vstack、stack 和 concatenate,但没有成功。
import numpy as np
array_a = np.array([1,2,3])
array_b = np.array([[10, 11, 12], [20, 21, 22], [30, 31, 32]])
The expected output should be预期的 output 应该是
array([[1, 10, 11, 12], [2, 20, 21, 22], [3, 30, 31, 32]])
Many thanks for your help!非常感谢您的帮助!
Mykola showed the right way to do this, but I suspect you need a little help in understanding why. Mykola 展示了执行此操作的正确方法,但我怀疑您需要一些帮助来理解原因。 You tried several things without telling us what was wrong.
您尝试了几件事,却没有告诉我们出了什么问题。
In [241]: array_a = np.array([1,2,3])
...: array_b = np.array([[10, 11, 12], [20, 21, 22], [30, 31, 32]])
vstack
runs: vstack
运行:
In [242]: np.vstack((array_a, array_b))
Out[242]:
array([[ 1, 2, 3],
[10, 11, 12],
[20, 21, 22],
[30, 31, 32]])
But the result is a vertical join, by rows, not columns.但结果是垂直连接,按行而不是按列。 The
v
in vstack
is supposed to remind us of that. vstack
中的v
应该提醒我们这一点。
stack
tries to join the arrays on a new axis, and requires that all input array have a matching shape: stack
尝试在新轴上加入 arrays,并要求所有输入数组具有匹配的形状:
In [243]: np.stack((array_a, array_b))
...
ValueError: all input arrays must have the same shape
I suspect you tried this at random, without really reading the docs.我怀疑您是随意尝试的,而没有真正阅读文档。
Both of these use concatenate
, which is the basic joiner.这两个都使用
concatenate
,这是基本的连接器。 But it's picky about dimensions:但它对尺寸很挑剔:
In [244]: np.concatenate((array_a, array_b))
...
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)
You clearly realized that the number of dimensions didn't match.您清楚地意识到维度的数量不匹配。
You want to make a (3,4) array.你想创建一个 (3,4) 数组。 One is (3,3), the other needs to be (3,1).
一个是(3,3),另一个需要是(3,1)。 And join axis needs to be 1
并且连接轴需要为1
In [247]: np.concatenate((array_a[:,None], array_b), axis=1)
Out[247]:
array([[ 1, 10, 11, 12],
[ 2, 20, 21, 22],
[ 3, 30, 31, 32]])
If we made a (1,3) array, and tried to join on the default 0 axis, we get the same thing as the vstack
.如果我们创建了一个 (1,3) 数组,并尝试在默认的 0 轴上加入,我们会得到与
vstack
相同的东西。 In fact that's what vstack
does:事实上,这就是
vstack
所做的:
In [248]: np.concatenate((array_a[None,:], array_b))
Out[248]:
array([[ 1, 2, 3],
[10, 11, 12],
[20, 21, 22],
[30, 31, 32]])
Another function is:另一个function是:
In [249]: np.column_stack((array_a, array_b))
Out[249]:
array([[ 1, 10, 11, 12],
[ 2, 20, 21, 22],
[ 3, 30, 31, 32]])
This does the same thing as [247].这与 [247] 的作用相同。
Functions like vstack
and column_stack
are handy, but in long run it's better to understand how to use concatenate
itself. vstack
和column_stack
之类的函数很方便,但从长远来看,最好了解如何使用concatenate
本身。
You can reshape()
the first array and then concatenate()
both arrays:您可以
reshape()
第一个数组,然后concatenate()
两个 arrays:
np.concatenate([array_a.reshape(3, -1), array_b], axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.