1

I have a function f(x,y,v,w) that I've evaluated over a range of values in (x,y,v,w) and stored in a 4D NumPy array, let's call it A.

I want a way to find two 2D arrays, V_best and W_best that hold the values of v,w that minimize f(x,y,v,w) over x,y. I've approached this by attempting to retrieve the indices of the values of (v,w) that give the minimum values of A over (x,y).

I've tried to use argmin for this, but I can't wrap my head around what the 3D arrays I get in return are, or how to use them in this context. As with many things I'm sure there's an obvious way to do this.

What I have is,

x = np.linspace(0,1,N1)
y = np.linspace(0,1,N2)
v = np.linspace(-5,5,N3)
w = np.linspace(-5,5,N4)
V,W,X,Y = np.meshgrid(v,w,x,y)
VALUEGRID = myfunc(V,W,X,Y)
V_besti = np.argmin(VALUEGRID,axis=0)
W_besti = np.argmin(VALUEGRID,axis=1)

Ideally, V_best and W_best will be of shape (N1,N2), corresponding to the dimensions of the range of x,y. I hope this is sufficiently clear.

Thank you in advance.

pedestrian
  • 19
  • 3
  • 2
    This looks a lot like another recent one: http://stackoverflow.com/questions/30965132/np-argmax-on-multidimensional-arrays-keeping-some-indexes-fixed – hpaulj Jun 22 '15 at 17:04
  • so, given a value of x and y, you'd like to find the indices of v,w that minimize A? Or are you talking about a *range* of x,y? – user1269942 Jun 22 '15 at 17:04
  • user1269942: I updated my post somewhat, I hope it clarifies a little. You're correct about this being over the range of x,y. For the function f(x,y,v,w) I "simply" want the values of v,w that minimise the function for each value in x,y. Ideally, I will have two arrays, Vbest and Wbest that have dimensions (N1,N2). – pedestrian Jun 22 '15 at 17:20

0 Answers0