Python how to put argument for function with numpy aply_along_axis

I want to apply a function to each column in a matrix. I would like to use a function with arguments, but I do not know how to do this, everything I tried ends in an error.

code I'm running

import numpy as np

M = np.array([[1,2,3,4],
              [1,2,3,4],
              [1,2,3,4],
              [1,2,3,4]])

def my_function(arr, arg="default"):
    print arg
    return arr

def my_function_allong_axis(M, argument):
    return np.apply_along_axis(my_function, axis=0, arr=M, arg=argument)

my_function_allong_axis(M, "something else")

this will cause a TypeError to be created: apply_along_axis () received an unexpected keyword argument 'arg'

+4
source share
2 answers

Decision:

def my_function_allong_axis(M, argument):
    return np.apply_along_axis(my_function, 0, M, argument)

keyword arguments were a problem due to old numpy

+3
source

"arg" "my_function" "apply_along_axis", . , , :

def my_function_allong_axis(M, argument):
    return np.apply_along_axis(my_function, axis=0, arr=M, arg=argument)
-1

All Articles