如何在没有嵌套for循环的情况下实现更快的冗余减少

时间:2017-07-13 14:25:15

标签: python arrays numpy tensorflow

我有一大堆代码,我想尝试加快速度。如果这是一个解决这个问题的好方法,我一直在使用tensorflow。这是我正在进行的大型模拟的一部分代码。但是,此代码运行时间太长。我有一种方法可以改变它们被分割的表面的频率,并且现在的分辨率降低了10倍,这个循环运行了一个多小时。有没有人有任何提示来加快这个速度?

Points = PlanesList[0:3,0:3]
Indices = np.array([[0],[1],[2]])
print(np.prod(PlanesList[0,:].shape))
for k in range(1,int(np.prod(PlanesList[0,:].shape)/3)+1):
    for ind in range(1,int(np.prod(Points.shape)/3)+1):
        truths = np.array([False,False,False])
        if np.all(PlanesList[0:3,(3*k-3)] == Points[0:3,ind-1]):
            index1=ind-1
            truths[0] = True
        if np.all(PlanesList[0:3,(3*k-2)] == Points[0:3,ind-1]):
            index2=ind-1
            truths[1] = True
        if np.all(PlanesList[0:3,(3*k-1)] == Points[0:3,ind-1]):
            index3=ind-1
            truths[2] = True
    if truths[0] == False:
        Points = np.column_stack([Points,PlanesList[0:3,(3*k-3)]])
        index1 = np.prod(Points[0,:].shape)-1
    if truths[1] == False:
        Points = np.column_stack([Points,PlanesList[0:3,(3*k-2)]])
        index2 = np.prod(Points[0,:].shape)-1
    if truths[2] == False:
        Points = np.column_stack([Points,PlanesList[0:3,(3*k-1)]])
        index3 = np.prod(Points[0,:].shape)-1
    Tempind = np.array([[int(index1)],[int(index2)],[int(index3)]])
    Indices = np.column_stack([Indices,Tempind])

此代码的目的是从阵列PlanesList中删除冗余。由于我创建阵列的方式,我还没有想出一种方法来形成它没有冗余。 PlanesList是一个包含多组3D三角形的数组,稍后在模拟中,我需要能够找到这些三角形。第0行是X坐标,第1行是Y,第2行是Z.Plane列表是一个2D数组,有超过60,000列(20,000个三角形)和3行。

1 个答案:

答案 0 :(得分:0)

要将numpy数组中的相同列减少为单个匹配项,可以将np.uniqueaxis参数一起使用。

示例数据:

a = np.random.randint(0, 10, (3,10)) 
a[:,3] = a[:,0]  # Duplicate one column

<强>减少

a_reduced = np.unique(a, axis=1)

请注意,这会对您的数组进行排序。

另请注意,axis参数是在numpy版本1.13.0中添加的。我假设底层实现很快,但可能存在专门针对您的问题定制的更快的解决方案。

编辑:如果您想知道减少点在完整数组中的索引,请使用return_index参数:

a_reduced, a_indices = np.unique(a, axis=1, return_index=True)