您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 优化/删除循环

5b51 2022/1/14 8:21:56 python 字数 2962 阅读 477 来源 www.jb51.cc/python

我有以下代码,我想使用numpy进行优化,最好是删除循环.我看不出如何接近它,所以任何建议都会有所帮助.indices是一个(N,2)numpy整数数组,N可以是几百万.代码的作用是在第一列中找到重复的索引.对于这些索引,我在第二列中进行了两个相应索引的所有组合.然后我将它们与第一列中的索引一起收集.index_sets = [] uniques, coun

概述

我有以下代码,我想使用numpy进行优化,最好是删除循环.我看不出如何接近它,所以任何建议都会有所帮助.

indices是一个(N,2)numpy整数数组,N可以是几百万.代码的作用是在第一列中找到重复的索引.对于这些索引,我在第二列中进行了两个相应索引的所有组合.然后我将它们与第一列中的索引一起收集.

index_sets = []
uniques,counts = np.unique(indices[:,0],return_counts=True)
potentials = uniques[counts > 1]
for p in potentials:
    correspondents = indices[(indices[:,0] == p),1]
    combs = np.vstack(list(combinations(correspondents,2)))
    combs = np.hstack((np.tile(p,(combs.shape[0],1)),combs))
    index_sets.append(combs)

对于N = 1.000.000,运行时间在我的电脑上是一秒的数量级.

import numpy_indexed as npi
N = 1000000
indices = np.random.randint(0,N/10,size=(N,2))

def combinations(x):
    """vectorized computation of combinations for an array of sequences of equal length

    Parameters
    ----------
    x : ndarray,[...,n_items]

    Returns
    -------
    ndarray,n_items * (n_items - 1) / 2,2]
    """
    return np.rollaxis(x[...,np.triu_indices(x.shape[-1],1)],-2,x.ndim+1)

def process(indices):
    """process a subgroup of indices,all having equal multiplicity

    Parameters
    ----------
    indices : ndarray,[n,2]

    Returns
    -------
    ndarray,[m,3]
    """
    keys,vals = npi.group_by(indices[:,indices[:,1])
    combs = combinations(vals)
    keys = np.repeat(keys,combs.shape[1])
    return np.concatenate([keys[:,None],combs.reshape(-1,2)],axis=1)

index_groups = npi.group_by(npi.multiplicity(indices[:,0])).split(indices)
result = np.concatenate([process(ind) for ind in index_groups])

免责声明:我是numpy_indexed套餐的作者.

总结

以上是编程之家为你收集整理的python – 优化/删除循环全部内容,希望文章能够帮你解决python – 优化/删除循环所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶