Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python how to put argument to function with numpy aply_along_axis

Tags:

python

numpy

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'

like image 461
user2173836 Avatar asked Nov 01 '22 10:11

user2173836


1 Answers

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

like image 169
user2173836 Avatar answered Nov 15 '22 04:11

user2173836