【问题标题】:Indexing the unique rows of an array索引数组的唯一行
【发布时间】:2016-07-14 23:35:21
【问题描述】:

我想获取数组中唯一行的索引。唯一的行应该有自己的索引(从零开始)。这是一个例子:

import numpy as np

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

在上面的数组中有六个唯一的行:

import pandas as pd
b = pd.DataFrame(a).drop_duplicates().values

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

每一行代表一个索引 (0, 1, 2, 3, 4 ,5)。为了获取数组a 中唯一行的索引,结果将是:

[0, 1, 2, 0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5, 3, 4, 5]

我怎样才能以有效的方式得到这个结果?

【问题讨论】:

  • pd.DataFrame(a).drop_duplicates().index 将返回原始 NP 数组中唯一行的索引 - 这是您想要的吗?
  • 不,这不是我想要的。这将返回唯一行首次出现的位置。
  • 你好像在求多栏factorize:看这个问答stackoverflow.com/questions/16453465/…
  • 是的,这就是我要找的。谢谢!

标签: python arrays numpy pandas


【解决方案1】:

这是我得到的:

b = pd.DataFrame(a).drop_duplicates()
indexed_rows = np.zeros(a.shape[0], dtype=int)
for index, i in enumerate(a):
    for unique_row, j in enumerate(b.values):
        if np.all(i==j):
            indexed_rows[index] = unique_row

返回结果是:

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

【讨论】:

  • 这不是一个有效的方法...b变量未定义(b = pd.DataFrame(a).drop_duplicates()
【解决方案2】:

纯 numpy 解决方案:

av = a.view(np.complex)
_,inv = np.unique(av,return_inverse=True)

那么inv就是:

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

np.complex用于打包两个组件,保持顺序。对于其他类型,其他方法是可能的。

【讨论】:

    【解决方案3】:

    没有 numpypandas 的解决方案:

    a = [[0, 1],
         [0, 2],
         [0, 3],
         [0, 1],
         [0, 2],
         [0, 3],
         [0, 1],
         [0, 2],
         [0, 3],
         [1, 1],
         [1, 2],
         [1, 3],
         [1, 1],
         [1, 2],
         [1, 3],
         [1, 1],
         [1, 2],
         [1, 3]]
    
    b = []
    
    #= ALGORITHM
    
    point = -1                                               # Increment
    cache = [[-1 for x in range(1000)] for x in range(1000)] # Change to dynamic
    
    for i in a:
        x = i[0]; y = i[1]
    
        # Check what's going on here...
        # print("x: {0} y: {1} --> {2} (cache)".format(x, y, cache[x][y]))
    
        if cache[x][y] == -1:
            point += 1
            cache[x][y] = point
            b.append(point)
        else:
            b.append(cache[x][y])
    
    #= TESTING
    
    print(b) # [0, 1, 2, 0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5, 3, 4, 5]
    

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 1970-01-01
      • 2020-11-12
      • 2018-12-19
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多