Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectorize() vs apply()

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?

like image 867
andrew Avatar asked Aug 01 '14 13:08

andrew


People also ask

Is NP vectorize faster than apply?

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.

Does pandas apply use vectorization?

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.

Is apply faster than Iterrows?

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.

What does vectorize function do?

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.


1 Answers

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?

like image 90
Thomas Avatar answered Sep 27 '22 17:09

Thomas