有效地重新排列2D NumPy数组 [英] Rearrange 2D NumPy Array Efficiently
问题描述
假设我有一个2D NumPy数组:
Let's say I have a 2D NumPy array:
x = np.random.rand(100, 100000)
然后我检索按列排序的索引(即,每个列都独立于其他列和索引进行排序返回):
And I retrieve the column-wise sorted indices (i.e., each column is sorted independently from the others and the indices are returned):
idx = np.argsort(x, axis=0)
然后,对于每一列,我都需要索引= [10、20、30、40、50]中的值成为前5行中的第一行(该列的),然后是其余的排序值(不是索引!)。
Then, for each column, I need the values from indices = [10, 20, 30, 40, 50] to be first the first 5 rows (of that column) and then followed by the rest of the sorted values (not the indices!).
一种幼稚的方法可能是:
A naive approach might be:
indices = np.array([10, 20, 30, 40, 50])
out = np.empty(x.shape, dtype=int64)
for col in range(x.shape[1]):
# For each column, fill the first few rows with `indices`
out[:indices.shape[0], col] = x[indices, col] # Note that we want the values, not the indices
# Then fill the rest of the rows in this column with the remaining sorted values excluding `indices`
n = indices.shape[0]
for row in range(indices.shape[0], x.shape[0]):
if idx[row, col] not in indices:
out[n, col] = x[row, col] # Again, note that we want the value, not the index
n += 1
推荐答案
方法#1
这里是一个基于 上一个帖子
的那个不需要 idx
-
xc = x.copy()
xc[indices] = (xc.min()-np.arange(len(indices),0,-1))[:,None]
out = np.take_along_axis(x,xc.argsort(0),axis=0)
方法#2
另一个与 np.isin
使用 idx
-
mask = np.isin(idx, indices)
p2 = np.take_along_axis(x,idx.T[~mask.T].reshape(x.shape[1],-1).T,axis=0)
out = np.vstack((x[indices],p2))
方法#2-替代
如果您连续地编辑到 out
来更改除 indices
之外的所有内容,分配可能适合您-
Approach #2- Alternative
If you are continously editing into out
to change everything except those indices
, an array-assignment might be the one for you -
n = len(indices)
out[:n] = x[indices]
mask = np.isin(idx, indices)
lower = np.take_along_axis(x,idx.T[~mask.T].reshape(x.shape[1],-1).T,axis=0)
out[n:] = lower
这篇关于有效地重新排列2D NumPy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!