Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utility of parameter 'out' in numpy functions

What is the utility of the out parameter in certain numpy functions such as cumsum or cumprod or other mathematical functions?

If the result is huge in size does it help to use the out parameter to improve computational time or memory efficiency?

This thread gives some information about how to use it. But I want to know when should I use it and what could the benefits be?

like image 218
Sounak Avatar asked Dec 04 '14 12:12

Sounak


People also ask

What are the functions of NumPy?

NumPy is a Python library used for working with arrays. It also has functions for working in domain of linear algebra, fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.

How do you write a NumPy function?

To create you own ufunc, you have to define a function, like you do with normal functions in Python, then you add it to your NumPy ufunc library with the frompyfunc() method. The frompyfunc() method takes the following arguments: function - the name of the function. inputs - the number of input arguments (arrays).

What are Ufuncs in NumPy?

ufuncs are used to implement vectorization in NumPy which is way faster than iterating over elements. They also provide broadcasting and additional methods like reduce, accumulate etc. that are very helpful for computation.


1 Answers

Functions that take the out parameter create new objects. This is usually what you would expect from the function: Give some array and get a new one with the transformed data.

However, imagine you want to call this function several thousand times in a row. Each function call will create a new array, which of course takes a lot of time.

In this case you may want to create an output array out and let the function fill the array with the output. After processing the data you can reuse out and let the function overwrite its values. This way you won't allocate or free any memory, which can save you a lot of time.

like image 162
cel Avatar answered Nov 15 '22 22:11

cel