Home > database >  Python Numpy: Is there a faster way to modify multiple arrays of the same shape when the changes are
Python Numpy: Is there a faster way to modify multiple arrays of the same shape when the changes are

Time:12-16

I have multiple numpy arrays that are all the same shape. I have two masks that are also that same shape.

Right now I'm changing each array one at a time. Is there a faster way to do this?

This is a smaller scale example of what I am doing.

start = time.time()

mask1 = np.array([[1,0,1],[0,1,1],[1,0,1]])
mask2 = np.array([[0,0,0],[1,0,1],[0,0,1]])

arr1 = np.array([[20,10,51],[21,1,2],[25,23,38]])
arr2 = np.array([[99,1,6],[66,54,11],[22,21,1]])
arr3 = np.array([[23,2,3],[55,2,16],[90,37,1]])
arr4 = np.array([[81,25,22],[1,63,24],[47,58,1]])

arr1[(mask1 == 1) & (mask2 == 0)] = 9999
arr2[(mask1 == 1) & (mask2 == 0)] = 9999
arr3[(mask1 == 1) & (mask2 == 0)] = 9999
arr4[(mask1 == 1) & (mask2 == 0)] = 9999

print(time.time() - start)

CodePudding user response:

at @hpaulj suggestion, I did the following and saw some improvement!

import timeit

def func1():
    mask1 = np.array([[1,0,1],[0,1,1],[1,0,1]])
    mask2 = np.array([[0,0,0],[1,0,1],[0,0,1]])
    arr1 = np.array([[20,10,51],[21,1,2],[25,23,38]])
    arr2 = np.array([[99,1,6],[66,54,11],[22,21,1]])
    arr3 = np.array([[23,2,3],[55,2,16],[90,37,1]])
    arr4 = np.array([[81,25,22],[1,63,24],[47,58,1]])
    arr1[(mask1 == 1) & (mask2 == 0)] = 9999
    arr2[(mask1 == 1) & (mask2 == 0)] = 9999
    arr3[(mask1 == 1) & (mask2 == 0)] = 9999
    arr4[(mask1 == 1) & (mask2 == 0)] = 9999

def func2():
    mask1 = np.array([[1,0,1],[0,1,1],[1,0,1]])
    mask2 = np.array([[0,0,0],[1,0,1],[0,0,1]])
    mask1[(mask1==1) & (mask2 != 0)] = 0
    arr1 = np.array([[20,10,51],[21,1,2],[25,23,38]])
    arr2 = np.array([[99,1,6],[66,54,11],[22,21,1]])
    arr3 = np.array([[23,2,3],[55,2,16],[90,37,1]])
    arr4 = np.array([[81,25,22],[1,63,24],[47,58,1]])
    arr1[(mask1 == 1)] = 9999
    arr2[(mask1 == 1)] = 9999
    arr3[(mask1 == 1)] = 9999
    arr4[(mask1 == 1)] = 9999

print(timeit.timeit(func1, number=1000000))
#17.574998669995693

print(timeit.timeit(func2, number=1000000))
#15.318040108977584

CodePudding user response:

What I was trying to suggest was:

mask3 = (mask1 == 1) & (mask2 == 0)    
arr1[mask3] = 9999
arr2[mask3] = 9999
arr3[mask3] = 9999
arr4[mask3] = 9999
  • Related