mercredi 21 octobre 2020

How to efficiently concatenate Numpy Array based on position conditioning?

The objective is to concatenate a Numpy Array according to a set of position. However, I am curious whether the concatenate and step as shown in the code below can be optimized further without the need of for loop and if-else statement?

tot_length=0.2 implementation
steps=0.1
start_val=0
repeat_perm=3 
list_no =np.arange(start_val, tot_length, steps)
x, y, z = np.meshgrid(*[list_no for _ in range(3)], sparse=True)
ix = np.array(((x>=y) & (y>=z)).nonzero()).T
final_opt=list_no[ix]
final_opt[:,[0, 1]] = final_opt[:,[1, 0]] 
all_result=itertools.product(range(0,ix.shape[1]), repeat=repeat_perm)

for num, num_pair in enumerate(all_result, start=1):
    for num_x, num_pair_x in enumerate ( num_pair, start=0 ):
        if (num == 1) &(num_x==0) :
            cont_arry = final_opt [num_pair_x, :]
        else:
            cont_arry= np.concatenate((cont_arry, final_opt  [num_pair_x, :]), axis=0)

final_arr =np.reshape(cont_arry, (-1, 9))

print(final_arr)

Output of size (27, 9), but only partial are shown below

 [[0.  0.  0.  0.  0.  0.  0.  0.  0. ]
     [0.  0.  0.  0.  0.  0.  0.1 0.  0. ]
     [0.  0.  0.  0.  0.  0.  0.1 0.1 0. ]
     [0.  0.  0.  0.1 0.  0.  0.  0.  0. ]
     [0.  0.  0.  0.1 0.  0.  0.1 0.  0. ]
     [0.  0.  0.  0.1 0.  0.  0.1 0.1 0. ]
     [0.1 0.1 0.  0.1 0.1 0.  0.1 0.1 0. ]]

Just some heads up,the cont_arry will be vectorised multiply with a 1D array of similar length with the cont_arry. Knowing this, is there a way to avoid from storing the result of concatenation on memory or what not to minimise potential memory issue since in actual application, the worst possible parameter setting is as below

tot_length=200 
steps=0.1
start_val=0
repeat_perm=1200 

Aucun commentaire:

Enregistrer un commentaire