Numpy 2d数组联合保证非零相等

时间:2020-11-28 23:53:41

I have two NxN numpy arrays, they are equal size.

我有两个NxN numpy数组,它们大小相等。

If a given row and column in the first array is nonzero, then it is guaranteed that we either have the same value in the same row and column of the other array, or that we have a zero there.

如果第一个数组中的给定行和列非零,那么可以保证我们在另一个数组的同一行和列中具有相同的值,或者我们在那里有零。

If a given row and column in the first array is zero, then we can have either a zero or a nonzero value in that row and column in the other array.

如果第一个数组中的给定行和列为零,那么我们可以在另一个数组中的该行和列中具有零值或非零值。

I would like to combine both array, such that for every [row,col], if one array has a value of zero, and the other has nonzero, then my second array will be modified (if necessary), to have the nonzero value.

我想结合两个数组,这样对于每个[row,col],如果一个数组的值为零,另一个数组的值为非零,那么我的第二个数组将被修改(如果需要),以具有非零值。

And, if they both have a nonzero value, (which is guaranteed to be the same value), then there will be no modification for that row,column - it stays the same.

而且,如果它们都具有非零值(保证是相同的值),那么该行,列将不会有任何修改 - 它保持不变。

Example:

array 1:

[[0,9],[2,0]]

array 2:

[[0,0],[2,2]]

After doing my "union", I want array 2 to be:

做完“联盟”之后,我想要数组2:

[[0,9],[2,2]]

What is a fast way to do this for large matrices? Thank you.

对大型矩阵执行此操作的快速方法是什么?谢谢。

2 个解决方案

#1


2  

All you wanna do is to change the zeros in second array to items in same index in first array. You can do the following:

您要做的就是将第二个数组中的零更改为第一个数组中相同索引中的项。您可以执行以下操作:

mask = arr2 == 0
arr2[mask] = arr1[mask]

Demo:

In [7]: arr1 = np.array([[0,9],[2,0]])

In [8]: arr2 = np.array([[0,0],[2,2]])

In [9]: mask = arr2 == 0

In [10]: arr2[mask] = arr1[mask]

In [11]: arr2
Out[11]: 
array([[0, 9],
       [2, 2]])

#2


1  

Since you are asking for "fast" you may be interested in np.copyto:

既然你要求“快”,你可能会对np.copyto感兴趣:

>>> a = np.random.randint(0, 2, (100, 100))
>>> b = np.random.randint(-1, 1, (100, 100))
>>> 
>>> 
>>> timeit("bk = b.copy(); mask=bk==0; bk[mask] = a[mask]", globals=globals(), number=10000)
1.3142543959984323
>>> timeit("bp = b.copy(); np.copyto(bp, a, where=bp==0)", globals=globals(), number=10000)
0.7330851459992118
>>> 
# check results are the same
>>> bk = b.copy(); mask=bk==0; bk[mask] = a[mask]
>>> bp = b.copy(); np.copyto(bp, a, where=bp==0)
>>> np.all(bk==bp)
True

#1


2  

All you wanna do is to change the zeros in second array to items in same index in first array. You can do the following:

您要做的就是将第二个数组中的零更改为第一个数组中相同索引中的项。您可以执行以下操作:

mask = arr2 == 0
arr2[mask] = arr1[mask]

Demo:

In [7]: arr1 = np.array([[0,9],[2,0]])

In [8]: arr2 = np.array([[0,0],[2,2]])

In [9]: mask = arr2 == 0

In [10]: arr2[mask] = arr1[mask]

In [11]: arr2
Out[11]: 
array([[0, 9],
       [2, 2]])

#2


1  

Since you are asking for "fast" you may be interested in np.copyto:

既然你要求“快”,你可能会对np.copyto感兴趣:

>>> a = np.random.randint(0, 2, (100, 100))
>>> b = np.random.randint(-1, 1, (100, 100))
>>> 
>>> 
>>> timeit("bk = b.copy(); mask=bk==0; bk[mask] = a[mask]", globals=globals(), number=10000)
1.3142543959984323
>>> timeit("bp = b.copy(); np.copyto(bp, a, where=bp==0)", globals=globals(), number=10000)
0.7330851459992118
>>> 
# check results are the same
>>> bk = b.copy(); mask=bk==0; bk[mask] = a[mask]
>>> bp = b.copy(); np.copyto(bp, a, where=bp==0)
>>> np.all(bk==bp)
True