从2个1D数组生成2D数组的矢量化方法

时间:2019-11-25 18:11:25

标签: python arrays numpy vectorization

我有一对等长的numpy数组。 dwells包含代表停留时间的浮点数,ids代表状态。在我的示例中,只有3个唯一的状态分别标记为012

dwells = np.array([4.3,0.2,3,1.5])
ids = np.array([2, 0, 1, 2])

之前的2个数组对系统进行建模,该系统从状态2开始,停留在那里4.3秒,跳到状态0,停留0.2秒,依此类推。 我想生成另一个numpy数组。它需要与dwells.sum()一样多的列,每列代表一个整数0、1、2、3 ...表示时间。每行匹配唯一状态之一(在本例中为3)。该数组的每个元素表示该时间段内每个状态的相对贡献。例如,在前4个时间点期间,只有状态2具有任何贡献,因此行2的前4个元素等于1。第五列来自所有3个州,但sum等于1

[[0, 0, 0, 0, 0.2, 0, 0,  0,  0]
 [0, 0, 0, 0, 0.5, 1, 1, 0.5, 0]
 [1, 1, 1, 1, 0.3, 0, 0, 0.5, 1]]

我可以使用for循环来做到这一点,但我想知道是否存在更有效的矢量化方式。

1 个答案:

答案 0 :(得分:2)

假设我们的时间步长为delta

import numpy as np

dwells = np.array([4.3,0.2,3,1.5])
ids = np.array([2, 0, 1, 2])

def dwell_map(dwells, ids, delta=0.1):
    import numpy as np
    import sys

    idelta = 1 / delta

    # ensure that idelta is an integer number
    if not idelta.is_integer():
        raise ValueError("1/delta is not integer") 

    idelta = int(idelta)

    # create new longer dwells array
    dwells_l = (dwells*idelta).astype(int)

    # create target array
    a = np.zeros((ids.max()+1, dwells_l.sum().astype(int)), dtype=int)

    # create repeats of the ids according to the dwell time
    ind = np.repeat(ids, dwells_l)

    # put ones at the position where we have the indices
    a[ind, np.arange(ind.size)] = 1

    # reduce back to the original time resolution
    a = a.reshape(ids.max()+1, -1, idelta).sum(axis=2)/idelta

    return a

res = dwell_map(dwells, ids, 0.1)

这仅在增量足够大且总持续时间足够小时才有效,这样中间数组就不会“无限”增大。

根据您的示例数组的iPython %timeit魔术的性能,将其与for循环解决方案进行比较:

10000 loops, best of 5: 58.5 µs per loop