代码之家  ›  专栏  ›  技术社区  ›  jdiction

python在3 x n数组中最常见的索引对

  •  0
  • jdiction  · 技术社区  · 8 年前

    我有一个形状为(3600219)的numpy数组,这是一个索引列表。

    array([[   0,    0,    0, ..., 2879, 2879, 2879],
           [  40,   40,   40, ...,  162,  165,  168],
           [ 249,  250,  251, ...,  195,  196,  198]])
    

    第一行是时间索引,第二行和第三行是坐标的索引。我试图找出哪一对坐标最常出现,不管时间。

    e、 g.是(49249)还是(40250)…等等。?

    4 回复  |  直到 8 年前
        1
  •  4
  •   blacksite    8 年前

    import numpy as np
    
    array = np.array([[   0,    0,    0, 2879, 2879, 2879],
           [  40,   40,   40, 162,  165,  168],
           [ 249,  250,  251, 195,  196,  198]])
    
    # Zip together only the second and third rows
    only_coords = zip(array[1,:], array[2,:])
    
    from collections import Counter
    
    Counter(only_coords).most_common()
    

    生产:

    Out[11]: 
    [((40, 249), 1),
     ((165, 196), 1),
     ((162, 195), 1),
     ((168, 198), 1),
     ((40, 251), 1),
     ((40, 250), 1)]
    
        2
  •  2
  •   Divakar    8 年前

    这是一种矢量化方法-

    IDs = a[1].max()+1 + a[2]
    unq, idx, count = np.unique(IDs, return_index=1,return_counts=1)
    out = a[1:,idx[count.argmax()]]
    

    如果可能存在负坐标,请使用 a[1].max()-a[1].min()+1 + a[2] IDs .

    In [44]: a
    Out[44]: 
    array([[8, 3, 6, 6, 8, 5, 1, 6, 6, 5],
           [5, 2, 1, 1, 5, 1, 5, 1, 1, 4],
           [8, 2, 3, 3, 8, 1, 7, 3, 3, 3]])
    
    In [47]: IDs = a[1].max()+1 + a[2]
    
    In [48]: unq, idx, count = np.unique(IDs, return_index=1,return_counts=1)
    
    In [49]: a[1:,idx[count.argmax()]]
    Out[49]: array([1, 3])
    
        3
  •  0
  •   Calum Beck    8 年前

    这可能看起来有点抽象,但您可以尝试将每个坐标保存为一个数字,例如[2,1]=2.1。并将数据放入这些坐标的列表中。例如,[1,1,2]的第二行和[2,2,1]的第三行将是[1.2,1.2,2.1],然后您可以使用代码:

    from collections import Counter
    list1=[1.2,1.2,2.1]
    data = Counter(list1)
    print (data.most_common(1))  # Returns the highest occurring item
    

    打印最常见的数字,以及它出现的次数,然后如果需要在代码中使用它,您可以简单地将数字转换回坐标。

        4
  •  0
  •   Mohanad Kaleia    8 年前

    import numpy as np
    import collections
    
    a = np.array([[0, 1, 2, 3], [10, 10, 30 ,40], [25, 25, 10, 50]])
    # You don't care about time
    b = np.transpose(a[1:])
    
    # convert list items to tuples
    c = map(lambda v:tuple(v), b)
    collections.Counter(c)
    

    Counter({(10, 25): 2, (30, 10): 1, (40, 50): 1})