python-3.x - 逐行堆叠 numpy 数组

标签 python-3.x numpy

我正在尝试按行堆叠两个 numpy 数组。我可以这样做。有没有更好的办法?

>>> a = np.arange(7, 13).reshape(2, 3)
>>> b = np.arange(1, 7).reshape(2, 3)
>>> x = np.row_stack((a[0],b[0]))
>>> y = np.row_stack((a[1],b[1]))
>>> z = np.stack((x, y))
>>> z
array([[[ 7,  8,  9],
        [ 1,  2,  3]],

       [[10, 11, 12],
        [ 4,  5,  6]]])

最佳答案

你可以得到同样的:

import numpy as np
a = np.arange(7, 13).reshape(2, 3)
b = np.arange(1, 7).reshape(2, 3)
z = np.stack((a, b), axis=1)
print(z)
# [[[ 7  8  9]
#   [ 1  2  3]]
# 
#  [[10 11 12]
#   [ 4  5  6]]]

关于python-3.x - 逐行堆叠 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63151417/

相关文章:

python - np.where 多个逻辑语句 pandas

Python 列联表

python - 如何转换日期时间列标题(例如 2007-03-01 00 :00:00) into Date-Month-Year format i. e. 2007-03-01

python - 找到最长重复的长度?

python - Python 中的 FileNotFoundError : [Errno 2] No such file or directory using os. 取消链接

python - 使用 numpy 运算从每行填充 numpy 数组(不包括填充)和非填充值的数量中选择随机数的最快方法

python - 避免在大型数据集上使用智能点/矩阵相乘进行迭代

python-3.x - 有没有办法在异步中使用 youtube-dl

python - 如何定义读取字符串元组的函数

python - 使用 zip 和 np.insert 将零部分插入 numpy 数组