如何合并2个numpy数组? [英] How to merge 2 numpy arrays?
问题描述
我觉得我缺少一些文档,但我在这个特定示例中找不到任何内容 - 一切都只是关于连接或堆叠数组.
I feel like there is some documentation I am missing, but I can't find anything on this specific example - everything is just about concatenating or stacking arrays.
我有形状 (2,3) 的数组 x 和数组 y
I have array x and array y both of shape (2,3)
x = [[1,2,3],[4,5,6]]
x = [[1,2,3],[4,5,6]]
y = [[7,8,9],[10,11,12]]
y = [[7,8,9],[10,11,12]]
x = 1 2 3
4 5 6
y = 7 8 9
10 11 12
我想要形状 (2,3,2) 的数组 z 看起来像这样
I want array z with shape (2,3,2) that looks like this
z = [[[1,7],[2,8],[3,9]],[[4,10],[5,11],[6,12]]]
z = [[[1,7],[2,8],[3,9]],[[4,10],[5,11],[6,12]]]
z = [1,7] [2,8] [3,9]
[4 10] [5 11] [6 12]
基本上在每个位置连接 x 和 y 元素.
basically joins the x and y element at each position.
推荐答案
听起来你要找的函数是 stack()
,用它来沿第三维堆叠.
Sounds like the function you're looking for is stack()
, using it to stack along the 3rd dimension.
import numpy as np
x = np.asarray([[1,2,3],[4,5,6]])
y = np.asarray([[7,8,9],[10,11,12]])
z = np.stack((x, y), 2)
这篇关于如何合并2个numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!