I want to apply function to every column in matrix. I would like to use function with arguments but I don't know how to do it, things I tried ends with an error.
code I am runnung
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 produce TypeError: apply_along_axis() got an unexpected keyword argument 'arg'
Solution:
def my_function_allong_axis(M, argument):
return np.apply_along_axis(my_function, 0, M, argument)
keyword arguments were the problem, because of the old numpy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With