简体   繁体   English

在多维数组上使用numpy.take?

[英]Using numpy.take on multidimensional arrays?

Is it possible to use take over multiple axes the same way fancy indexing works? 花式索引的工作方式是否可以使用多个轴?

The multidimensional arrays are fairly large, so I was hoping to potentially get a speedup. 多维数组相当大,所以我希望有可能获得加速。

For example: 例如:

import numpy as np
x = np.random.rand(20,20,20,20)
m = np.where(x>0.5)
m = (m[0],m[1],m[2])
print x[m].shape

Your code: 你的代码:

m = np.where(x>0.5)
m = (m[0],m[1],m[2])
result = x[m]

Can be written to avoid the np.where by using repeat: 可以通过使用repeat来编写以避免使用np.where:

m = np.sum(x>0.5,-1)
result = x.reshape(-1,x.shape[-1]).repeat(w.ravel(), 0)

Which seems about 4 times faster. 这似乎快4倍。 However I wonder if you did not mean to ask for 不过我想知道你是不是故意要求

m = np.any(x>0.5,-1)
result = x[m,:]

which will not create duplicates (though reshaping is still required here)? 这不会产生重复(虽然这里仍需要重塑)?

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

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