是否有使用排列的更好方法?

时间:2019-07-31 08:16:45

标签: python performance numpy generator permutation

我需要制作一个排列的数据结构。目前,我正在使用非常耗时的发电机。是否有替代生成器的方法或某种其他方法来系统地逐步处理矩阵的不同索引?另一个问题可能是现在使用字符串并将它们制成列表列表的函数。

这是针对分配问题的。

def ourpermutations(iterable, r=None):
    """
    Input:
    String or numbers separated by a space
    optional= the length that the permutations must be

    Output:
    a generator of permutations
    """

    pool = iterable.split(" ")
    n = len(pool)
    r = n if r is None else r
    for indices in product(range(n), repeat=r):
        if len(set(indices)) == r:
            yield tuple(pool[i] for i in indices)
def ExhaustiveSearchinputs(datamatrix):
    """
    Input:
    datamatrix: numpy array

    Output:
    list of every permutation allowed and the time it took to run(this is to help with the optimisation and 
    testing process)

    """

    # Important starting values
    start = time.time()
    length = len(datamatrix)
    thestring = ""

    #Generate the permutations
    for i in range(0,length):  #this is making a string of numbers from 0 to the size of the matrix -1
        thestring += str(i) + " "
    thestring = thestring[:-1]
    listofassociations = list(ourpermutations(thestring,length))  #this was the function we made earlier

    #these are the time calculation
    end = time.time()
    thetime = end - start
    return listofassociations, thetime,thestring
##########PLEASE NOTE THIS FUNCTION TAKES 4 seconds once datamatrix is length 8 and takes 99 seconds for length 9 

输出正确,只是速度很慢。

1 个答案:

答案 0 :(得分:0)

问题通常不是生成器,而是您使用的生成器。

遍历整个乘积并滤除排列的那些元组确实效率极低。所有元组的数量为N^N,其中N!是实际排列。让我们为N:N = 5:〜0.038的几个值计算比率N!/ N ^ N; N = 7:〜0.0061; N = 10:〜0.00036; N = 20:〜0.000000023。换句话说,已经是N = 5时,您就为每个好的元组遍历了25个无用的元组。

使用Stirling's approximation,我们可以看到该比率非常接近于sqrt(2 pi N)exp(-N)。

当然,指数级的不良产量是完全不能接受的。因此,您将不得不提出一个更好的枚举策略。或使用itertools.permutations