[英]Numpy: Group of ids after unique
i'm looking for a solution for group data after the use of the unique numpy function.在使用独特的 numpy function 后,我正在寻找组数据的解决方案。 I think an example is better:
我认为一个例子更好:
>>> 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)
Thank you very much for your help.非常感谢您的帮助。
Maybe you can create a dictionary where keys are tuples from elements of t
and values are letters from ids
也许您可以创建一个字典,其中键是
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']}
A friend found a good way to do that.一位朋友找到了一个很好的方法来做到这一点。 (Works only if the data is sorted)
(仅在数据已排序时有效)
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.