Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中以最快的方式确定每个组在数组中复制值的索引_Python_Performance_Numpy_Duplicates - Fatal编程技术网

在Python中以最快的方式确定每个组在数组中复制值的索引

在Python中以最快的方式确定每个组在数组中复制值的索引,python,performance,numpy,duplicates,Python,Performance,Numpy,Duplicates,我想找到每组重复值的索引,如下所示: s = [2,6,2,88,6,...] 结果必须返回原始s的索引:[[0,2],[1,4],…],或者结果可以以另一种方式显示 我找到了许多解决方案,因此我找到了获得重复组的最快方法: s = np.sort(a, axis=None) s[:-1][s[1:] == s[:-1]] 但是在排序之后,我从原始s中得到了错误的索引 在我的例子中,列表中有大约200 mil的值,我想找到最快的方法。我使用数组来存储值,因为我想使用GPU使其更快。使用像di

我想找到每组重复值的索引,如下所示:

s = [2,6,2,88,6,...]
结果必须返回原始s的索引:
[[0,2],[1,4],…]
,或者结果可以以另一种方式显示

我找到了许多解决方案,因此我找到了获得重复组的最快方法:

s = np.sort(a, axis=None)
s[:-1][s[1:] == s[:-1]]
但是在排序之后,我从
原始s
中得到了错误的索引


在我的例子中,列表中有大约200 mil的值,我想找到最快的方法。我使用数组来存储值,因为我想使用GPU使其更快。

使用像
dict
这样的散列结构会有所帮助

例如:

将numpy导入为np
从集合导入defaultdict
a=np.数组([2,4,2,88,15,4])
table=defaultdict(列表)
对于ind,枚举中的num(a):
表[num]+=[ind]
产出:

{2: [0, 2], 4: [1, 5], 88: [3], 15: [4]}
2 : [0, 2]
4 : [1, 5]
如果要按从小到大的顺序显示重复的图元,请执行以下操作:

排序(table.items())中的k、v的
:
如果len(v)>1:
打印(k,“:”,v)
产出:

{2: [0, 2], 4: [1, 5], 88: [3], 15: [4]}
2 : [0, 2]
4 : [1, 5]

速度由数字列表中的多少个不同值决定。

查看这是否满足您的性能要求(此处,
s
是您的输入数组):

注意事项:

  • 结果将是一个数组列表

  • 每个数组都将包含
    s
    中重复值的索引。例如,如果值
    13
    s
    中重复
    7次
    ,则
    result
    的数组中会有一个索引为
    7的数组

  • 如果要忽略
    s
    的单例值(在
    s
    中只出现一次的值),可以将
    minlength=2
    传递到
    np.bincount()

  • (这是我另一个答案的一个变体。在这里,我们不是拆分已排序的大型数组,而是从中提取片段,因此它可能具有不同的性能特征)

    如果
    s
    是输入数组:

    counts       = np.bincount(s)
    cum_counts   = np.add.accumulate(counts)
    sorted_inds  = np.argsort(s)
    
    result       = np.split(sorted_inds, cum_counts[:-1])
    
    counts       = np.bincount(s)
    cum_counts   = np.add.accumulate(counts)
    sorted_inds  = np.argsort(s)
    
    result = [sorted_inds[:cum_counts[0]]] + [sorted_inds[cum_counts[i]:cum_counts[i+1]] for i in range(cum_counts.size-1)]
    

    您好@fountainhead,我有一个错误
    无法为具有形状(4688145146881523,)和数据类型int64的数组分配33.3 PiB。我的python版本是3.7.8和64位。我试图纠正这个错误,但做不到。“你有什么想法吗?”ToanNguyenPhuoc——这看起来像是一个记忆障碍。发生在哪一行?
    argsort()
    ?在第
    np.bincount(s)
    行中输入数组的大小是多少?输入数组的大小是2000万Hi@Autumnii,我在约20mil数据下测试了您的解决方案,需要34秒。看起来不错,谢谢。