Numpy:如何将一个数组堆放到较大数组的每一行中并将其变成2D数组?

发布于 2025-02-05 15:24:46 字数 610 浏览 3 评论 0原文

我有一个名为Heartbeats的Numpy数组,有100行。每行都有5个元素。

我还有一个名为time_index的单个数组,带有5个元素。 我需要将时间索引预先到Heartbeats的每一行。

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])

我只写了两行heartbeats来说明。

I have a numpy array named heartbeats with 100 rows. Each row has 5 elements.

I also have a single array named time_index with 5 elements.
I need to prepend the time index to each row of heartbeats.

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])

What I need:

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])

I only wrote two rows of heartbeats to illustrate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别在捏我脸啦 2025-02-12 15:24:46

假设您使用的是numpy,则可以通过time_index的重复版本使用heartbeats的ravel版本堆叠time_index

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

Assuming you are using numpy, the exact output array you are looking for can be made by stacking a repeated version of time_index with the raveled version of heartbeats:

np.stack((np.tile(time_index, len(heartbeats)), heartbeats.ravel()), axis=-1)
星光不落少年眉 2025-02-12 15:24:46

另一种方法,使用广播

In [13]: 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])

制造目标数组:

In [14]: res = np.zeros(heartbeats.shape + (2,), heartbeats.dtype)

In [15]: res[:,:,1] = heartbeats     # insert a (2,5) into a (2,5) slot

In [17]: res[:,:,0] = time_index[None]   # insert a (5,) into a (2,5) slot

In [18]: res
Out[18]: 
array([[[-2.  , -0.58],
        [-1.  , -0.57],
        [ 0.  , -0.55],
        [ 1.  , -0.39],
        [ 2.  , -0.4 ]],

       [[-2.  , -0.31],
        [-1.  , -0.31],
        [ 0.  , -0.32],
        [ 1.  , -0.46],
        [ 2.  , -0.46]]])

然后将其重塑为2D:

In [19]: res.reshape(-1,2)
Out[19]: 
array([[-2.  , -0.58],
       [-1.  , -0.57],
       [ 0.  , -0.55],
       [ 1.  , -0.39],
       [ 2.  , -0.4 ],
       [-2.  , -0.31],
       [-1.  , -0.31],
       [ 0.  , -0.32],
       [ 1.  , -0.46],
       [ 2.  , -0.46]])

[17]采用A(5,),将其扩展到(1,5),然后将其扩展到(2,5)对于插入。阅读广播

Another approach, using broadcasting

In [13]: 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])

Make a target array:

In [14]: res = np.zeros(heartbeats.shape + (2,), heartbeats.dtype)

In [15]: res[:,:,1] = heartbeats     # insert a (2,5) into a (2,5) slot

In [17]: res[:,:,0] = time_index[None]   # insert a (5,) into a (2,5) slot

In [18]: res
Out[18]: 
array([[[-2.  , -0.58],
        [-1.  , -0.57],
        [ 0.  , -0.55],
        [ 1.  , -0.39],
        [ 2.  , -0.4 ]],

       [[-2.  , -0.31],
        [-1.  , -0.31],
        [ 0.  , -0.32],
        [ 1.  , -0.46],
        [ 2.  , -0.46]]])

and then reshape to 2d:

In [19]: res.reshape(-1,2)
Out[19]: 
array([[-2.  , -0.58],
       [-1.  , -0.57],
       [ 0.  , -0.55],
       [ 1.  , -0.39],
       [ 2.  , -0.4 ],
       [-2.  , -0.31],
       [-1.  , -0.31],
       [ 0.  , -0.32],
       [ 1.  , -0.46],
       [ 2.  , -0.46]])

[17] takes a (5,), expands it to (1,5), and then to (2,5) for the insert. Read up on broadcasting.

财迷小姐 2025-02-12 15:24:46

作为另一种方式,您可以根据指定的时间重复time_index np.concatenate :

concatenated = np.concatenate([time_index] * heartbeats.shape[0])
# [-2 -1  0  1  2 -2 -1  0  1  2]

# result = np.dstack((concatenated, heartbeats.reshape(-1))).squeeze()
result = np.array([concatenated, heartbeats.reshape(-1)]).T

使用np.concatenate可能比np.tile 。该解决方案比 MAD物理学家更快,但是最快的是使用广播为 hpaulj 的答案。

As an alternative way, you can repeat time_index by np.concatenate based on the specified times:

concatenated = np.concatenate([time_index] * heartbeats.shape[0])
# [-2 -1  0  1  2 -2 -1  0  1  2]

# result = np.dstack((concatenated, heartbeats.reshape(-1))).squeeze()
result = np.array([concatenated, heartbeats.reshape(-1)]).T

Using np.concatenate may be faster than np.tile. This solution is faster than Mad Physicist, but the fastest is using broadcasting as hpaulj's answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文