简体   繁体   English

numpy数组/矩阵上的值的排列

[英]Permutation of values on numpy array/matrix

I'm looking for a code that speed up a creation of permutation matrix. 我正在寻找可加快置换矩阵创建速度的代码。 ie, I want to create a matrix of n columns, where each column value iterates over m values, creating an^m combinations on each row. 即,我想创建一个n列的矩阵,其中每个列的值迭代m个值,并在每行上创建一个^ m组合。 On the example below, there are 2 methods to create the matrix, in this case n=7 and m=5 that creates a matrix similar to: 在下面的示例中,有两种创建矩阵的方法,在这种情况下,n = 7和m = 5创建矩阵,类似于:

1 1 1 1 1 1 1
1 1 1 1 1 1 2
1 1 1 1 1 1 3
1 1 1 1 1 1 4
1 1 1 1 1 1 5
1 1 1 1 1 2 1
...
5 5 5 5 5 5 5

The order of the rows doesn't matter, only that all combinations are created. 行的顺序无关紧要,仅创建所有组合即可。 I have written the following 2 methods to create the arrays, but the metod1 is very slow (although very verbose and clear to understand) and method2 is faster using numpy functions. 我已经编写了以下2种方法来创建数组,但是metod1非常慢(尽管非常冗长且易于理解),而method2使用numpy函数则更快。 But I still need to find a faster methodology to create the matrix. 但是我仍然需要找到一种更快的方法来创建矩阵。

import numpy as np

############################################
def permArray_meth1():

    allArray = np.zeros((1,7))

    for a1  in range(1,6):
        for a2  in range(1,6):
            for a3  in range(1,6):
                for a4  in range(1,6):
                    for a5  in range(1,6):
                        for a6  in range(1,6):
                            for a7  in range(1,6):

                                allArray = np.append(allArray, np.array([a1,a2,a3,a4,a5,a6,a7]).reshape(1,7), axis=0)

    allArray = np.delete(allArray, 0, 0)
    return allArray

############################################
def permArray_meth2():

    ##### Create permutation matrix #####
    a = np.arange(np.power(5,7)).reshape(5,5,5,5,5,5,5)
    allArray = [(a1,a2,a3,a4,a5,a6,a7) for a1,a2,a3,a4,a5,a6,a7 in np.ndindex(a.shape)]

    ##### Convert list to array #####
    allArray = np.asarray(allArray)+1
    return allArray


############################################
if __name__ == "__main__":

    allArray = permArray_meth1()    #  (50sec)
    print 'allArray1', np.shape(allArray)

    allArray = permArray_meth2()    #  (1sec)
    print 'allArray2', np.shape(allArray)

I know that the speed is dependent also on the used CPU hardware, but I'm looking for a relatively faster code thatn the shown above. 我知道速度也取决于所用的CPU硬件,但是我正在寻找一个相对较快的代码,该代码如上图所示。

Is there any other method/code? 还有其他方法/代码吗?

You could do this by creating an (n, m, m, ..., m) array of indices for column 1, column 2, ..., column n using np.indices() , then reshaping the output into an (n ** m, n) array: 您可以使用np.indices()为第1列,第2列,...,第n列创建一个(n, m, m, ..., m)个索引数组,然后将输出重塑为(n ** m, n)数组:

import numpy as np

def permgrid(m, n):
    inds = np.indices((m,) * n)
    return inds.reshape(n, -1).T

For example: 例如:

print(permgrid(2, 3))

# [[0 0 0]
#  [0 0 1]
#  [0 1 0]
#  [0 1 1]
#  [1 0 0]
#  [1 0 1]
#  [1 1 0]
#  [1 1 1]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM