Improve the execution speed using Numpy

Surabhi Amit Chembra

I have the following for loop and want to use numpy vectorizing, boolean, mask arrays etc to improve the execution speed. arr is a numpy array

def translate(arr, x=0):
    arr1 = arr.copy()
    for i in range(arr1.shape[0]):
        for j in range(arr1.shape[1]):
            if i + x < arr1.shape[0] and i + x > 0:
                arr1[i,j] = arr[i+x,j]
            if i + x < 0 or i + x > arr1.shape[0]:
                arr1[i,j] = 255
    return arr1

Any help or suggestions will be much appreciated.

Edit: instead of 255, when (i+x) <0, it should be arr[i,j]

def translate(arr, x=0):
    arr1 = arr.copy()
    for i in range(arr1.shape[0]):
        for j in range(arr1.shape[1]):
            if i + x < arr1.shape[0] and i + x > 0:
                arr1[i,j] = arr[i+x,j]
            if i + x < 0 or i + x > arr1.shape[0]:
                arr1[i,j] = arr[i,j]
    return arr1
R.yan

Since @Paul Panzer solution didn't give out the same output as your function, I revised on his work by trying boolean array. Hopefully its what you want.

Code:

def my_translate(arr, x=0):
    arr = arr.copy()
    if x == 0:
        return arr.copy()
    elif x > 0:
        replacement1 = np.zeros(arr.shape)
        replacement1[:-x] = arr[x:]
    else:
        replacement1 = np.zeros(arr.shape)
        replacement1[-x:] = arr[:x]
    replacement2 = np.zeros(arr.shape)+255 # Array filled with 255 for second logic
    l = [np.repeat(i,arr.shape[1]) for i in range(arr.shape[0])]
    firstlooplogic1 = np.asarray(l)+x < arr.shape[0]
    firstlooplogic2 = np.asarray(l)+x > 0
    secondlooplogic1 = np.asarray(l)+x > arr.shape[0]
    secondlooplogic2 = np.asarray(l)+x < 0

    part1logic = np.logical_and(firstlooplogic1,firstlooplogic2)
    part2logic = np.logical_or(secondlooplogic1,secondlooplogic2)

    part1 = part1logic*replacement1
    part2 = part2logic*replacement2
    part3 = ((part1 == 0) * (part2 == 0)) * arr       
    return (part1 + part2 +part3).astype(arr.dtype)

Result:

arr = 3 - np.maximum.outer(*2*(np.abs(np.arange(-3, 4)),))
output1 = my_translate(arr,-2) #output from my function
output2 = translate(arr,-2)    #output from your function above
np.array_equal(output1,output2)
>Out[10]: True

Basically, I just break down your nested for-loop into boolean array and do the operation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

improve speed of mysql import

Improve the speed of a JavaScript function

How to improve "on keyup" speed?

Improve speed/use of gDistance function by using parallel processing and/or plyr/dplyr?

Improve speed of queries using complex survey design in R

Improve BeautifulSoup parsing speed

How can I improve this nested for loops using numpy arrays

How to improve speed of query?

Why does numpy vectorization not improve the speed of my code

Will using an "else" speed the up the execution of code?

Improve pdf render speed

how to improve speed with using RallyAPIForJava

Unable to improve speed of operation of ios app using grand central dispatch

Improve file transfer speed using CoreBluetooth

How to improve the speed while using NSURLSessionDownloadTask?

Improve execution of for cycle

How to increase pixel math speed using NumPy

How to improve Sauce Labs test execution speed for watir+ruby+rspec via Jenkins?

how to improve speed in database?

PL\SQL How can I improve the execution speed of my function?

How to improve read/write speed when using distributed file system?

How to loop numpy ndarray by using cupy? Is will really improve execution time?

There is a way to improve the execution time?

How can I improve my custom function vectorization using numpy

Improve speed of python algorithm

Using scrapy to parse items and improve code speed/overall performance

Improve speed of getpixel and putpixel

How to improve PostgreSQL intersect speed?

Parallel execution speed of a vectorized function using numba