我有两个A&B数组如下,
A = [2,6,4,5,3,1]
B = [1,4,3,6,5,2]从那里我形成了一个矮小的阵列;
oldArr = np.array([[2,6,4,5,3,1],[1,4,3,6,5,2]])然后,我必须随机挑选(例如)3列,让我们说;
idx = np.random.randint(len(oldArr[0]), size = len(oldArr[0])/2)给出了随机数的指数;
array([3, 4, 0])相应的值为;
[[5 3 2]
[6 5 1]]现在,我想构造一个新的数组:我必须将这些随机数保持在相同的位置,并以这样的方式交换第0和第1 raw,这样的话,数字就不应该在新数组中重复,并且必须将非重复的数字按相同的顺序排列。
[2,_,_,5,3,_] # Random number positions didnt change
[1,_,_,6,5,_]交换后,在我的情况下,它必须是这样(我们可以看到,数字是唯一的,在raws);
[2,1,4,5,3,6]
[1,2,4,6,5,3]我在这里插入了图像,以更好的方式来表示。

有人能给出一些线索来获得新的数组吗?谢谢!!
发布于 2018-10-19 01:59:09
下面是一个使用掩蔽和argsort的解决方案。它假设A和B是相互混合的。
它将A中的非固定值按它们在B中出现的顺序排序,反之亦然。
import numpy as np
def reorder(a, b, idx):
ab = np.stack([a, b])
mask = np.ones(a.shape, bool)
mask[idx] = False
aidx, bidx = xidx = ab.argsort(axis=1)
assert np.all(a[aidx] == b[bidx])
xmask = np.empty(ab.shape, bool)
xmask[[[0], [1]], xidx] = mask[xidx[::-1]]
ab[::-1][np.broadcast_to(mask, ab.shape)] = ab[xmask]
return ab
A = np.array([2,6,4,5,3,1])
B = np.array([1,4,3,6,5,2])
idx = np.array([3, 4, 0])
def make(n):
a, b, i = (np.random.permutation(n) for _ in 'xxx')
return a, b, i[:n//2]
def check(a, b, i):
m = np.ones(a.shape, bool)
m[i] = False
result = reorder(a, b, i)
assert np.all(np.sort(result) == np.sort(a))
# warning: expensive!
assert np.all(np.diff(np.where(result[1][m, None]==a)[1]) >= 0)
assert np.all(np.diff(np.where(result[0][m, None]==b)[1]) >= 0)
for a, b, i in [(A, B, idx), make(10), make(20)]:
m = np.zeros(a.shape, int)
m[i] = 1
print(np.stack([a, b, m]), '\n')
print(reorder(a, b, i), '\n')
check(a, b, i)示例运行(打印3个示例(A;B、掩码、结果);第一个示例来自OP):
[[2 6 4 5 3 1]
[1 4 3 6 5 2]
[1 0 0 1 1 0]]
[[2 1 4 5 3 6]
[1 2 4 6 5 3]]
[[4 2 1 6 0 7 9 3 8 5]
[1 7 5 3 9 8 2 0 6 4]
[0 1 0 1 1 0 0 1 1 0]]
[[1 2 7 6 0 5 9 3 8 4]
[4 7 2 3 9 1 8 0 6 5]]
[[ 1 4 15 18 6 19 7 13 0 8 5 17 14 12 3 9 2 11 16 10]
[ 0 6 3 1 11 12 4 7 19 10 8 13 9 14 5 17 18 15 16 2]
[ 1 1 0 1 0 0 1 1 0 0 1 1 1 0 1 0 0 0 1 0]]
[[ 1 4 0 18 6 11 7 13 12 19 5 17 14 10 3 8 9 15 16 2]
[ 0 6 15 1 18 19 4 7 17 14 8 13 9 12 5 3 2 11 16 10]] https://stackoverflow.com/questions/52883156
复制相似问题