根据最后一列累积NumPy数组的行

时间:2018-04-03 13:41:53

标签: python arrays numpy cumsum accumulate

我有以下问题。我有一个带坐标数组的数组,前三个条目是x,y,z坐标,第四个条目是轨道的id。我想在第一个时间点之后开始向轨道添加漂移。是否有一种简单的方法可以将轨道动态地添加到轨道上,它们的ID可以具有不同的长度,可以立即传输到整个阵列? (所以你可以看到,id的轨道只有3个坐标条目,id 3的轨道有6个)

import numpy as np
drift=np.array([1,1,0])
a = np.array([[1,1,1,0],[1,1,1,0],[1,1,1,0],
              [1,1,1,2],[1,1,1,2],[1,1,1,3],
              [1,1,1,3],[1,1,1,3],[1,1,1,3],
              [1,1,1,3],[1,1,1,3]])

输出

output = np.array([[1,1,1,0],[2,2,1,0],[3,3,1,0],
                   [1,1,1,2],[2,2,1,2],[1,1,1,3],
                   [2,2,1,3],[3,3,1,3],[4,4,1,3],
                   [5,5,1,3],[6,6,1,3]])

2 个答案:

答案 0 :(得分:2)

以下是如何以矢量化方式完成的示例:

import numpy as np


drift = np.array([1, 1, 0])
a = np.array([[1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 2], 
              [1, 1, 1, 2], [1, 1, 1, 3], [1, 1, 1, 3], [1, 1, 1, 3], 
              [1, 1, 1, 3], [1, 1, 1, 3], [1, 1, 1, 3]])


def multirange(counts: np.ndarray) -> np.ndarray:
    """
    Calculates concatenated ranges. Code was taken at:
    https://stackoverflow.com/questions/20027936/how-to-efficiently-concatenate-many-arange-calls-in-numpy
    """
    counts = counts[counts != 0]
    counts1 = counts[:-1]
    reset_index = np.cumsum(counts1)
    incr = np.ones(counts.sum(), dtype=int)
    incr[0] = 0
    incr[reset_index] = 1 - counts1
    incr.cumsum(out=incr)
    return incr


def drifts(ids: np.ndarray,
           drift: np.ndarray) -> np.ndarray:
    diffs = np.diff(ids)
    max_drifts_per_id = np.concatenate((np.where(diffs)[0], [len(ids) - 1])) + 1
    max_drifts_per_id[1:] = max_drifts_per_id[1:] - max_drifts_per_id[:-1]
    multipliers = multirange(max_drifts_per_id)
    drifts = np.tile(drift, (len(ids), 1))
    return drifts * multipliers[:, np.newaxis]


a[:, :-1] += drifts(a[:, -1], drift)
print(a)

输出:

array([[0, 0, 0, 0],
       [1, 1, 0, 0],
       [2, 2, 0, 0],
       [0, 0, 0, 2],
       [1, 1, 0, 2],
       [0, 0, 0, 3],
       [1, 1, 0, 3],
       [2, 2, 0, 3],
       [3, 3, 0, 3],
       [4, 4, 0, 3],
       [5, 5, 0, 3]])

解释

drifts函数的想法是获取一组id(在我们的例子中,我们可以获得a[:, -1]array([0, 0, 0, 2, 2, 3, 3, 3, 3, 3, 3]))和drift({{ 1}})获取以下数组,然后可以将其附加到原始数组:

np.array([1, 1, 0])

逐行:

array([[0, 0, 0],
       [1, 1, 0],
       [2, 2, 0],
       [0, 0, 0],
       [1, 1, 0],
       [0, 0, 0],
       [1, 1, 0],
       [2, 2, 0],
       [3, 3, 0],
       [4, 4, 0],
       [5, 5, 0]])

这里我们得到一个数组,其中所有非零元素都将具有第一个数组中最后一个id的索引:

diffs = np.diff(ids)

有关详细信息,请参阅np.diff

array([0, 0, 2, 0, 1, 0, 0, 0, 0, 0]) 

max_drifts_per_id = np.concatenate((np.where(diffs)[0], [len(ids) - 1])) + 1 将为前一个数组中的非零元素提供索引。我们附加最后一个元素的索引并将结果索引递增1以便稍后获得范围。有关详细信息,请参阅np.where。连接后np.where(diffs)[0]将是:

max_drifts_per_id
array([ 3,  5, 11])

在上一个结果中,我们得到了一系列范围的结束值:

max_drifts_per_id[1:] = max_drifts_per_id[1:] - max_drifts_per_id[:-1]
array([3, 2, 6])

我们使用multipliers = multirange(max_drifts_per_id) 作为连接np.arange调用的有效替代方法。有关详细信息,请参阅How to efficiently concatenate many arange calls in numpy? 。结果multirange将是:

multipliers
array([0, 1, 2, 0, 1, 0, 1, 2, 3, 4, 5])

np.tile我们将drifts = np.tile(drift, (len(ids), 1)) 展开为与drift具有相同的行数:

ids
array([[1, 1, 0],
       [1, 1, 0],
       [1, 1, 0],
       [1, 1, 0],
       [1, 1, 0],
       [1, 1, 0],
       [1, 1, 0],
       [1, 1, 0],
       [1, 1, 0],
       [1, 1, 0],
       [1, 1, 0]])

我们将其乘以return drifts * multipliers[:, np.newaxis] 并得到:

multipliers

最后,这个返回的值可以添加到原始数组中:

array([[0, 0, 0],
       [1, 1, 0],
       [2, 2, 0],
       [0, 0, 0],
       [1, 1, 0],
       [0, 0, 0],
       [1, 1, 0],
       [2, 2, 0],
       [3, 3, 0],
       [4, 4, 0],
       [5, 5, 0]])

答案 1 :(得分:0)

据我所知,没有内置的方法可以做到这一点,但你可以通过这个简单的循环来解决它:

import numpy as np
drift=np.array([1,1,0])
a = np.array([[1,1,1,0],[1,1,1,0],[1,1,1,0],
[1,1,1,2],[1,1,1,2],[1,1,1,3],[1,1,1,3],[1,1,1,3],[1,1,1,3],[1,1,1,3],[1,1,1,3]])

_id = 0
n = 0
for i in range(a.shape[0]):
    if a[i, 3] == _id:
        a[i, 0:3] = a[i, 0:3] + n * drift
        n += 1
    else:
        _id = a[i, 3]
        n = 1

print(a)