如何根据每个记录及其上一个记录过滤NumPy数组

时间:2019-11-07 22:19:11

标签: python numpy

我正在考虑将代码转换为可与NumPy一起使用,并且具有以下功能:

def Clean_One_State(ListsToConvert):
    # Removes duplicate states
    prev_bool = 2
    returnlist = []
    for value in enumerate(ListsToConvert[1:], 0):
        if value[1] != prev_bool:
            try:
                returnlist.append(value)
                prev_bool = value[1]
            except IndexError:
                returnlist.append(value)
                prev_bool = value[1]
    return returnlist

该函数在句子中删除输入中与先前记录具有相同状态的记录。 根据您自己的需要,该函数的输入和输出是:

In:[['Event Time', 'State at A'],[0.0, 1], [0.03253, 1], [0.04757, 0], 
    [0.08479, 0], [0.98534, 1], [0.98748, 1], [1.03602, 0], [1.03717, 0],
    [1.95898, 0], [1.96456, 1], [2.00913, 1], [2.01547, 0]...
Out: [[0.0, 1], [0.04757, 0], [0.98534, 1], [1.03602, 0], [1.96456, 1], [2.01547, 0]...

理想情况下,我希望能够查看输入列表的视图(在NumPy中),以便可以删除将影响输入列表的输出列表中的记录。我在网上看了一些示例,但仍然对如何执行此操作持怀疑态度。

1 个答案:

答案 0 :(得分:1)

一种非常标准的numpy方法是使用高级索引:

data = [['Event Time', 'State at A'],[0.0, 1], [0.03253, 1], [0.04757, 0], 
        [0.08479, 0], [0.98534, 1], [0.98748, 1], [1.03602, 0], [1.03717, 0],
        [1.95898, 0], [1.96456, 1], [2.00913, 1], [2.01547, 0]]

# convert to array
ar = np.array([*map(tuple,data[1:])],dtype=[*zip(data[0],(float,int))])
ar
# array([(0.     , 1), (0.03253, 1), (0.04757, 0), (0.08479, 0),
#        (0.98534, 1), (0.98748, 1), (1.03602, 0), (1.03717, 0),
#        (1.95898, 0), (1.96456, 1), (2.00913, 1), (2.01547, 0)],
#       dtype=[('Event Time', '<f8'), ('State at A', '<i8')])

# find places where State at A changes and select them from ar
# prepend something that is not equal to the first State at A, so the 
# very first item is also selected
ar[np.diff(ar['State at A'],prepend=ar['State at A'][0]-1).nonzero()]
# array([(0.     , 1), (0.04757, 0), (0.98534, 1), (1.03602, 0),
#        (1.96456, 1), (2.01547, 0)],
#       dtype=[('Event Time', '<f8'), ('State at A', '<i8')])