title: Numpy Apply Custom Function Along Axis (or Map)
body: <p><a href="https://stackoverflow.com/questions/66190182/scipy-compute-mann-whitney-u-along-multiple-slices">Related to this question:</a></p>
  <p>I have the following code with custom functions:</p>
  <pre><code>import numpy as np
  from scipy.integrate import quad
  from scipy.special import binom
  from scipy.stats import norm
  import math as mt
  ranks = np.array([[8, 5, 3, 7, 6, 2, 1, 4],
         [8, 5, 3, 7, 6, 2, 1, 4]])
  
  def E(r, n):
      print(r,n)
      def f(x):
          F = Phi(x)
          return x*(1-F)**(r-1)*F**(n-r)*phi(x)
      return -r*binom(n, r)*quad(f, -inf, inf)[0]
  </code></pre>
  <p>I want to apply the function E over each row of the array <code>ranks</code>.</p>
  <p>I tried <code>np.apply_along_axis</code> but the problem I'm having is that I have 2 arguments for the function: the individual rank of an element in a row of an array (<code>r</code>) and the length of that row (<code>n</code>).</p>
  <p>Also, I'm wondering if it would be faster and better anyway to use <code>map</code> as with the accepted answer to the question referenced above (because speed is important with this).</p>
  <p><strong>Here's what I've tried:</strong></p>
  <pre><code>result = np.apply_along_axis(E, 1, ranks)
  </code></pre>
  <p>Issue: I need to provide the <code>n</code> argument. Also, this may be slower than mapping.</p>
  <pre><code>#not sure if the &quot;*&quot; is needed or what it does.
  def E2(i):
      return [*E(ranks[i], len(ranks[i]))]
  
  it = 2 #2 rows (iterations)
  indx = np.arange(it)
  result = np.array(list(map(E2, indx))) 
  </code></pre>
  <p>Issue: Needs 1 rank at a time, per row. Not sure how to achieve this.</p>
  <p>Thanks in advance!</p>