python - Numpy:如何将单个数组堆叠到更大数组的每一行中并将其转换为二维数组?

标签 python arrays numpy stack concatenation

我有一个名为 heartbeats 的 numpy 数组,有 100 行。每行有 5 个元素。

我还有一个名为 time_index 的数组,包含 5 个元素。 我需要在每行心跳前添加时间索引

heartbeats = np.array([
    [-0.58, -0.57, -0.55, -0.39, -0.40],
    [-0.31, -0.31, -0.32, -0.46, -0.46]
])
time_index = np.array([-2, -1, 0, 1, 2])

我需要什么:

array([-2, -0.58],
      [-1, -0.57],
      [0, -0.55],
      [1, -0.39],
      [2, -0.40],
      [-2, -0.31],
      [-1, -0.31],
      [0, -0.32],
      [1, -0.46],
      [2, -0.46])

我只写了两行心跳来说明。

最佳答案

假设您正在使用 numpy,您要查找的确切输出数组可以通过堆叠 time_index 的重复版本来制作。与 heartbeats 的困惑版本:

np.stack((np.tile(time_index, len(heartbeats)), heartbeats.ravel()), axis=-1)

关于python - Numpy:如何将单个数组堆叠到更大数组的每一行中并将其转换为二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72549588/

相关文章:

python - 按组计算每行之后的行数

python - 如何在终端窗口python中创建超链接

python - 我如何从numpy datetime64中获取年-月-日?

python - np.concatenate 的内存错误

python - 连接两个具有交错列的矩阵

python - matplotlib basemap ,通过 matplotlib 中的经纬度坐标绘制图形

python - 使用 Python Bottle 生成项目不起作用

c - 初始化指向 char 的指针数组

javascript - 如何从js调用php方法?

python - 在 Python 中选择一系列列