Using numpy to build an array of all combinations of two arrays

后端 未结 10 1488
温柔的废话
温柔的废话 2020-11-22 00:41

I\'m trying to run over the parameters space of a 6 parameter function to study it\'s numerical behavior before trying to do anything complex with it so I\'m searching for a

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 01:39

    You can do something like this

    import numpy as np
    
    def cartesian_coord(*arrays):
        grid = np.meshgrid(*arrays)        
        coord_list = [entry.ravel() for entry in grid]
        points = np.vstack(coord_list).T
        return points
    
    a = np.arange(4)  # fake data
    print(cartesian_coord(*6*[a])
    

    which gives

    array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1],
       [0, 0, 0, 0, 0, 2],
       ..., 
       [3, 3, 3, 3, 3, 1],
       [3, 3, 3, 3, 3, 2],
       [3, 3, 3, 3, 3, 3]])
    

提交回复
热议问题