Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以像花哨的索引一样使用接管多个轴?
多维数组相当大,所以我希望有可能获得加速。
例如:
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
你的代码:
m = np.where(x>0.5) m = (m[0],m[1],m[2]) result = x[m]
可以编写以避免 np.where 通过使用重复:
m = np.sum(x>0.5,-1) result = x.reshape(-1,x.shape[-1]).repeat(w.ravel(), 0)
这似乎快了大约 4 倍。但是我想知道你是否不是故意要
m = np.any(x>0.5,-1) result = x[m,:]
哪个不会创建重复项(尽管此处仍需要重塑)?