The Vectorize()
and the apply()
functions in R
can often be used to accomplish the same goal. I usually prefer vectorizing a function for readability reasons, because the main calling function is related to the task at hand while sapply
is not. It is also useful to Vectorize()
when I am going to be using that vectorized function multiple times in my R code. For instance:
a <- 100
b <- 200
c <- 300
varnames <- c('a', 'b', 'c')
getv <- Vectorize(get)
getv(varnames)
vs
sapply(varnames, get)
However, at least on SO I rarely see examples with Vectorize()
in the solution, only apply()
(or one of it's siblings). Are there any efficiency issues or other legitimate concerns with Vectorize()
that make apply()
a better option?
From what I measured (shown below in some experiments), using np. vectorize() is 25x faster (or more) than using the DataFrame function apply() , at least on my 2016 MacBook Pro.
Pandas VectorizationThe fastest way to work with Pandas and Numpy is to vectorize your functions. On the other hand, running functions element by element along an array or a series using for loops, list comprehension, or apply() is a bad practice.
This solution also uses looping to get the job done, but apply has been optimized better than iterrows , which results in faster runtimes. See below for an example of how we could use apply for labeling the species in each row.
The vectorized function evaluates pyfunc over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy. The data type of the output of vectorized is determined by calling the function with the first element of the input.
Vectorize
is just a wrapper for mapply
. It just builds you an mapply
loop for whatever function you feed it. Thus there are often easier things do to than Vectorize()
it and the explicit *apply
solutions end up being computationally equivalent or perhaps superior.
Also, for your specific example, you've heard of mget
, right?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With