<div dir="ltr">There is a reported bug (issue <a href="https://github.com/numpy/numpy/issues/5837">#5837</a>) regarding different returns from np.nonzero with 1-D vs higher dimensional arrays. A full summary of the differences can be seen from the following output:<div><br><div><div><font face="monospace, monospace">>>> class C(np.ndarray): pass</font></div><div><font face="monospace, monospace">... </font></div><div><font face="monospace, monospace">>>> a = np.arange(6).view(C)</font></div><div><font face="monospace, monospace">>>> b = np.arange(6).reshape(2, 3).view(C)</font></div><div><font face="monospace, monospace">>>> anz = a.nonzero()</font></div><div><font face="monospace, monospace">>>> bnz = b.nonzero()</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">>>> type(anz[0])</font></div><div><font face="monospace, monospace"><type 'numpy.ndarray'></font></div><div><font face="monospace, monospace">>>> anz[0].flags</font></div><div><font face="monospace, monospace">  C_CONTIGUOUS : True</font></div><div><font face="monospace, monospace">  F_CONTIGUOUS : True</font></div><div><font face="monospace, monospace">  OWNDATA : True</font></div><div><font face="monospace, monospace">  WRITEABLE : True</font></div><div><font face="monospace, monospace">  ALIGNED : True</font></div><div><font face="monospace, monospace">  UPDATEIFCOPY : False</font></div><div><font face="monospace, monospace">>>> anz[0].base</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">>>> type(bnz[0])</font></div><div><font face="monospace, monospace"><class '__main__.C'></font></div><div><font face="monospace, monospace">>>> bnz[0].flags</font></div><div><font face="monospace, monospace">  C_CONTIGUOUS : False</font></div><div><font face="monospace, monospace">  F_CONTIGUOUS : False</font></div><div><font face="monospace, monospace">  OWNDATA : False</font></div><div><font face="monospace, monospace">  WRITEABLE : False</font></div><div><font face="monospace, monospace">  ALIGNED : True</font></div><div><font face="monospace, monospace">  UPDATEIFCOPY : False</font></div><div><font face="monospace, monospace">>>> bnz[0].base</font></div><div><font face="monospace, monospace">array([[0, 1],</font></div><div><font face="monospace, monospace">       [0, 2],</font></div><div><font face="monospace, monospace">       [1, 0],</font></div><div><font face="monospace, monospace">       [1, 1],</font></div><div><font face="monospace, monospace">       [1, 2]])</font></div></div><div><br></div><div>The original bug report was only concerned with the non-writeability of higher dimensional array returns, but there are more differences: 1-D always returns an ndarray that owns its memory and is writeable, but higher dimensional arrays return views, of the type of the original array, that are non-writeable.</div><div><br></div><div>I have a branch that attempts to fix this by making both 1-D and n-D arrays:</div><div><ol><li>return a view, never the base array,</li><li>return an ndarray, never a subclass, and</li><li>return a writeable view.</li></ol><div>I guess the most controversial choice is #2, and in fact making that change breaks a few tests. I nevertheless think that all of the index returning functions (nonzero, argsort, argmin, argmax, argpartition) should always return a bare ndarray, not a subclass. I'd be happy to be corrected, but I can't think of any situation in which preserving the subclass would be needed for these functions.</div></div><div><br></div><div>Since we are changing the returns of a few other functions in 1.10 (diagonal, diag, ravel), it may be a good moment to revisit the behavior for these other functions. Any thoughts?</div><div><br></div><div>Jaime</div><div><br></div><div>-- <br><div class="gmail_signature">(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.</div>
</div></div></div>