<div dir="ltr">Hi all,<div><br></div><div>So, in advanced indexing, numpy decides where to put new axes based on whether the "advanced indices" are all next to each other.</div><div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">>>> np.random.random((3,4,5,6,7,8))[:, [[0,0],[0,0]], 1, :].shape</span><br>(3, 2, 2, 6, 7, 8)<br>>>> np.random.random((3,4,5,6,7,8))[:, [[0,0],[0,0]], :, 1].shape</span></div><div><span style="font-family:monospace">(2, 2, 3, 5, 7, 8)<br></span></div><div><span style="font-family:monospace"><br></span></div><div>In creating a wrapper type around arrays, I'm finding myself needing to suppress this behavior, so that the new axes consistently appear in the front.  I thought of a dumb hat trick:</div><div><br></div><div>def index(x, indices):</div><div>    return x[(True, None) + indices]</div><div><br></div><div>Which certainly gets the new dimensions where I want them, but it introduces a ghost dimension of 1 (and sometimes two such dimensions!) in a place where I'm not sure I can easily find it.</div><div><br></div><div><font face="monospace, monospace">>>> np.random.random((3,4,5,6,7,8))[True, None, 1].shape<br>(1, 1, 4, 5, 6, 7, 8)<br>>>> np.random.random((3,4,5,6,7,8))[True, None, :, [[0,0],[0,0]], 1, :].shape<br>(2, 2, 1, 3, 6, 7, 8)<br>>>> np.random.random((3,4,5,6,7,8))[True, None, :, [[0,0],[0,0]], :, 1].shape<br>(2, 2, 1, 3, 5, 7, 8)</font><span style="font-family:monospace"><br></span><br>any better ideas?</div><div><br></div><div>---</div><div><br></div><div>Michael</div></div>