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'
source
share