Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy apply_along_axis function

Tags:

python

numpy

I am trying to use numpys apply_along_axis with a function who needs more than one argument.

test_array = np.arange(10)
test_array2 = np.arange(10)

def example_func(a,b):
   return a+b

np.apply_along_axis(example_func, axis=0, arr=test_array, args=test_array2)

In the manual: http://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html there is the parameter args for additional parameters. But if I try to add that parameter python returns an error:

*TypeError: apply_along_axis() got an unexpected keyword argument 'args'*

or if I don't use args an argument is missing

*TypeError: example_func() takes exactly 2 arguments (1 given)*

This here is just an example code and I know I could solve that in different ways like using numpy.add or np.vectorize. But my question is if I can use numpys apply_along_axis function with a function which uses more than one argument.

like image 768
MonteCarlo Avatar asked Feb 16 '14 14:02

MonteCarlo


People also ask

How do I apply a function to every row in Numpy?

In order to apply a function to every row, you should use axis=1 param to apply(). By applying a function to each row, we can create a new column by using the values from the row, updating the row e.t.c. Note that by default it uses axis=0 meaning it applies a function to each column.

Is Numpy apply along axis fast?

The np. apply_along_axis() function seems to be very slow (no output after 15 mins).


2 Answers

the *args in the signature numpy.apply_along_axis(func1d, axis, arr, *args) means that there are some other positional arguments could be passed.

If you want to add two numpy arrays elementwise, just use + operator:

In [112]: test_array = np.arange(10)
     ...: test_array2 = np.arange(10)

In [113]: test_array+test_array2
Out[113]: array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

Remove the keywords axis=, arr=, args= should also work:

In [120]: np.apply_along_axis(example_func, 0, test_array, test_array2)
Out[120]: array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
like image 124
zhangxaochen Avatar answered Sep 21 '22 18:09

zhangxaochen


I just want to briefly elaborate on zhangxaochen's answer, in case that helps someone. Let's use an example where we want to greet a list of people with a particular greeting.

def greet(name, greeting):
    print(f'{greeting}, {name}!')

names = np.array(["Luke", "Leia"]).reshape(2,1)

Since apply_along_axis accepts *args, we can pass an arbitrary number of arguments to it, which in this case will each be passed along to func1d.

Avoiding Syntax Error

In order to avoid a SyntaxError: positional argument follows keyword argument we have to label the argument:

np.apply_along_axis(func1d=greet, axis=1, arr=names, greeting='Hello')

More Than One Additional Argument

If we also had a function that took even more arguments

def greet_with_date(name, greeting, date):
    print(f'{greeting}, {name}! Today is {date}.')

we could use it in either of the following ways:

np.apply_along_axis(greet_with_date, 1, names, 'Hello', 'May 4th')
np.apply_along_axis(func1d=greet_with_date, axis=1, arr=names, date='May 4th', greeting='Hello')

Note that we don't need to worry about the order of the keyword arguments.

like image 40
Mikhail Golubitsky Avatar answered Sep 24 '22 18:09

Mikhail Golubitsky