0

Would you help me understand if it possible to call map with multiple arguments ?

My goal is to calculate the following:

numpy.mean(array,axis=1)
numpy.var(array,axis=0)
numpy.std(array,axis=None)

I can call map with list comprehension, but struggling to pass named argument to it

print "{}\n{}\n{}".format(*[ map(x,array) for x in [numpy.mean,numpy.var,numpy.std]]) 
martineau
  • 119,623
  • 25
  • 170
  • 301
Andy
  • 2,706
  • 3
  • 13
  • 22

2 Answers2

3

You can use functools.partial:

>>> from functools import partial
>>> array = np.arange(12).reshape(3,4)
>>> array
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> func_list = [
        partial(numpy.mean, axis=1), 
        partial(numpy.var, axis=0), 
        partial(numpy.std, axis=None)
    ]
>>> print(*map(lambda x:x(array), func_list), sep='\n')
[1.5 5.5 9.5]
[10.66666667 10.66666667 10.66666667 10.66666667]
3.452052529534663

Another way:

>>> func_list = [(numpy.mean, numpy.var, numpy.std), 
                 ({'axis':1}, {'axis':0}, {'axis':None})]

>>> print(*map(lambda f,kw: f(array, **kw), *func_list), sep='\n')
[1.5 5.5 9.5]
[10.66666667 10.66666667 10.66666667 10.66666667]
3.452052529534663

A slight variation of the above:

>>> func_list = [
        (numpy.mean, {'axis':1}), 
        (numpy.var, {'axis':0}), 
        (numpy.std, {'axis':None})
    ]
>>> print(*map(lambda f,kw: f(array, **kw), *zip(*func_list)), sep='\n')
[1.5 5.5 9.5]
[10.66666667 10.66666667 10.66666667 10.66666667]
3.452052529534663
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
2

You can specify the functions with a lambda expression:

def a(x): return numpy.mean(x,axis=1)
def b(x): return numpy.var(x,axis=0)
def c(x): return numpy.std(x,axis=None)

print ("{}\n{}\n{}".format(*[x(array) for x in [a,b,c]]))

in the lambda expression you can specify the arguments. If you have more informations you can specify this in the lambda expression as well. Example:

def a_1 (x,y): numpy.std(x,axis=y)

But then you need to pass this arguments as well. For example in a tuple

func_1 = (a_1, None) #Functionname and attribute
Sebastian Baum
  • 365
  • 1
  • 9
  • 1
    [Avoid named lambdas](https://stackoverflow.com/q/38381556/4518341). Use a `def` instead, or a partial like in [Sayandip's answer](https://stackoverflow.com/a/63330003/4518341) – wjandrea Aug 09 '20 at 18:40
  • 1
    Thank you, will it work for python2.7 ? Im getting an error ".../2.7/Extras/lib/python/numpy/core/fromnumeric.py", line 2716, in mean out=out, keepdims=keepdims) File ".../2.7/Extras/lib/python/numpy/core/_methods.py", line 52, in _mean rcount = _count_reduce_items(arr, axis) File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/_methods.py", line 46, in _count_reduce_items items *= arr.shape[ax] IndexError: tuple index out of range >>> array array([[1, 2], [3, 4]]) – Andy Aug 09 '20 at 18:46
  • I checked it with python 2.7 and it worked for me :) – Sebastian Baum Aug 09 '20 at 18:49
  • ah ok i checked my function. I threw an arra with the map function. Remove the map function as i showd in my edit and the error won't be raised. – Sebastian Baum Aug 09 '20 at 19:01