Skip to content Skip to sidebar Skip to footer

Replacing Numpy Array Elements That Are Non Zero

I have 2 numpy arrays such as: arr1 = np.array([[0,5,5,0],[0,5,5,0]]) arr2 = np.array([[7,7,0,0],[7,7,0,0]]) I'd like to copy non zero elements in arr2 into corresponding position

Solution 1:

You can use three alternatives:

arr1[arr2 > 0] = arr2[arr2 > 0]
arr1[np.where(arr2>0)] = arr2[np.where(arr2>0)]
arr1[arr2.nonzero()] = arr2[arr2.nonzero()]

But a winner is np.copyto(arr1, arr2, where=arr2 != 0), thanks @Mark Meyer.

Every of these 4 methods changes arr1 into

array([[7, 7, 5, 0],
       [7, 7, 5, 0]])

If you don't want side effects in arr1, use arr = arr1.copy() instead and ten replace it in analogous way.

Update

Let's look at results of perfplot

import perfplot

defsimple(arr):
    arr1, arr2 = arr
    arr1[arr2 != 0] = arr2[arr2 != 0]
    return arr1
defwhere(arr):
    arr1, arr2 = arr
    arr1[np.where(arr2 != 0)] = arr2[np.where(arr2 != 0)]
    return arr1
defnonzero(arr):
    arr1, arr2 = arr
    arr1[arr2.nonzero()] = arr2[arr2.nonzero()]
    return arr1
defsimple_improve(arr):
    arr1, arr2 = arr
    idx = arr2 != 0
    arr1[idx] = arr2[idx]
    return arr1
defwhere_improve(arr):
    arr1, arr2 = arr
    idx = np.where(arr2 != 0)
    arr1[idx] = arr2[idx]
    return arr1
defnonzero_improve(arr):
    arr1, arr2 = arr
    idx = arr2.nonzero()
    arr1[idx] = arr2[idx]
    return arr1
defcopyto(arr): #thanks @Mark Meyer
    arr1, arr2 = arr
    np.copyto(arr1, arr2, where=arr2 != 0)
    return arr1
import numexpr as ne
defcopyto_numexpr(arr):
    #some magic boost
    arr1, arr2 = arr
    np.copyto(arr1, arr2, where=ne.evaluate('arr2 != 0'))
    return arr1

perfplot.show(
    setup=lambda n: (np.tile(np.array([[0, 5, 5, 0], [0, 5, 5, 0]]), (n, n)),
                     np.tile(np.array([[7, 7, 0, 0], [7, 7, 0, 0]]), (n, n))),
    # setup=lambda n: [list(range(n))] * 10,
    kernels=[simple, where, nonzero,
             simple_improve, where_improve, nonzero_improve, 
             copyto, copyto_numexpr],
    n_range=[2 ** k for k inrange(12)],
    xlabel="n*n copies of array of shape (2,4)")

enter image description here

Solution 2:

Since you want to mutate arr1, you can just assign with boolean indexing:

import numpy as np

arr1 = np.array([[0,5,5,0],[0,5,5,0]])
arr2 = np.array([[7,7,0,0],[7,7,0,0]])

arr1[arr2 != 0] = arr2[arr2 != 0]

print(arr1)

# [[7 7 5 0]
#  [7 7 5 0]]

You can pick up a little performance on large arrays using copyto():

np.copyto(arr1, arr2, where=arr2 != 0)

Post a Comment for "Replacing Numpy Array Elements That Are Non Zero"