本文介绍了如何基于位置条件有效地连接Numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

目标是根据一组位置连接一个Numpy Array.但是,我很好奇是否可以在不使用for loop和if- else statement的情况下进一步优化下面代码中所示的concatenate和步骤吗?

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)

大小为(27, 9)的输出,但下面仅显示部分

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. ]]

请注意,cont_arry将是vectorised multiply,其长度与cont_arry相似的1D数组.知道这一点,有没有一种方法可以避免将串联结果存储在内存中,或者避免将潜在的内存问题最小化,因为在实际应用中,最坏的可能参数设置如下所示

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

推荐答案

我认为您的串联循环可以替换为:

I think your concatenate loop can be replaced with:

alist = []
for num, num_pair in enumerate(all_result, start=1):
    for num_x, num_pair_x in enumerate ( num_pair, start=0 ):
        alist.append( final_opt  [num_pair_x, :]))
arr = np.array(alist)
# arr = np.concatenate(alist, axis=0)
# arr = np.vstack(alist)

可能有一些我没听懂的细节.我没有尝试测试.列表附加比连接快得多,尤其是重复执行时.
给出要连接的整个数组列表时,concatenate效率最高.

There may be some details in this that I didn't catch. I haven't tried to test it. List append is much faster than concatenate, especially when done repeatedly.
concatenate is most efficient when give a whole list of arrays to join.

更好的是,根本不进行迭代;而是使用全数组数学和索引.但是我没有尝试掌握您的代码,所以不会建议如何做.

Better yet, don't iterate at all; instead make use of whole-array math and indexing. But I haven't tried to master your code, so won't suggest how to do that.

这篇关于如何基于位置条件有效地连接Numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:54