簡體   English   中英

Numpy:唯一后的ID組

[英]Numpy: Group of ids after unique

在使用獨特的 numpy function 后,我正在尋找組數據的解決方案。 我認為一個例子更好:

>>> t
[[0, 3, 4], [1, 2, 8], [1, 2, 8]] #array of multiples values
>>> ids = ['A', 'B', 'C'] #Ids associated with previous values
>>> np.unique(t, axis=0)
array([[0, 3, 4],
       [1, 2, 8]]) #Result of unique (so 2 rows ofc)
>>> array([['A'],
           ['B', 'C']]) #What i want to got (and generated with numpy ideally)

非常感謝您的幫助。

也許您可以創建一個字典,其中鍵是t元素的元組,值是ids中的字母

d = {}
for i in range(len(ids)):
    d.setdefault(tuple(t[i]), []).append(ids[i])
d
# {(0, 3, 4): ['A'], (1, 2, 8): ['B', 'C']}

一位朋友找到了一個很好的方法來做到這一點。 (僅在數據已排序時有效)

import numpy as np

t = np.array([[0, 3, 4], [1, 2, 8], [1, 2, 8]])
ids = np.array(['A', 'B', 'C'])
print(t)
res = np.split(ids, np.unique(t, return_index=True, axis=0)[1][1:])
print(res) # [array(['A'], dtype='<U1'), array(['B', 'C'], dtype='<U1')]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM