Home > database >  How do I vectorize a function in python or numpy?
How do I vectorize a function in python or numpy?

Time:04-19

For instance, in Julia language, a function can easily be vectorized as shown

function circumference_of_circle(r)
    return 2*π * r
end

a = collect([i for i=1:200])
circumference_of_circle.(a) # easy vactorization using just (.)

Although I like Julia very much, it has not matured like Python.

Is there a similar vectorization technique in the Python function?

CodePudding user response:

In [1]: def foo(r):
   ...:     return 2*np.pi * r
   ...: 
In [2]: arr = np.arange(5)
In [3]: foo(arr)
Out[3]: array([ 0.        ,  6.28318531, 12.56637061, 18.84955592, 25.13274123])

All operations in your function work with numpy arrays. There's no need to do anything special.

If your function only works with scalar arguments, "vectorizing" becomes trickier, especially if you are seeking compiled performance.

Have you spent much time reading the numpy basics? https://numpy.org/doc/stable/user/absolute_beginners.html

===

I don't know julia, but this code

function _collect(::Type{T}, itr, isz::SizeUnknown) where T
    a = Vector{T}()
    for x in itr
        push!(a, x)
    end
    return a
end

looks a lot like

def foo(func, arr):
    alist = []
    for i in arr:
         alist.append(func(i))
    return alist # or np.array(alist)

or equivalently the list comprehension proposed in the other answer.

or list(map(func, arr))

CodePudding user response:

I'm not familiar with Julia or vectorization of functions, but if I'm understanding correctly, I believe in Python there are a few ways to do this. The plain-jane python way is using list comprehension

An example using your circumference function would be:

def circumference_of_circle(r):
    return 2 * 3.14152 * r

circles = [[x, circumference_of_circle(x)] for x in range(1,201)]

print(circles)

circles list will contain inner lists that have both the radius (generated by the range() function) as well as its circumference. Like Julia function vectorization, python list comprehension is just short-hand for loops, but they take in a list object and return a list object, so they are very handy.

CodePudding user response:

Your function contains only simple math. Python's numpy and pandas modules are designed in ways that allow such operations to be performed on them.

import numpy as np
a = np.array([1,2,3,4])
def circumference_of_circle(r):
    return 2 * np.pi * r
print(circumference_of_circle(a)) # array([ 6.28318531, 12.56637061, 18.84955592, 25.13274123])

More complicated functions cannot be applied directly to an array. You may be able to rewrite the function in a vectorized way, for example using np.where for conditions that would be represented by an if block within a normal function.

If this isn't an option, or speed is not a major concern, then you can iterate over the list using a list comprehension [func(v) for v in arr], numpy's vectorize, pandas's apply. You can sometimes optimize these approaches by pre-compiling parts of the code.

  • Related