Jaime Fernández del Río
2015-05-09 17:48:46 UTC
There is a reported bug (issue #5837
<https://github.com/numpy/numpy/issues/5837>) regarding different returns
from np.nonzero with 1-D vs higher dimensional arrays. A full summary of
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : False
ALIGNED : True
UPDATEIFCOPY : False
[0, 2],
[1, 0],
[1, 1],
[1, 2]])
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.
I have a branch that attempts to fix this by making both 1-D and n-D arrays:
1. return a view, never the base array,
2. return an ndarray, never a subclass, and
3. return a writeable view.
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.
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?
Jaime
<https://github.com/numpy/numpy/issues/5837>) regarding different returns
from np.nonzero with 1-D vs higher dimensional arrays. A full summary of
class C(np.ndarray): pass
...a = np.arange(6).view(C)
b = np.arange(6).reshape(2, 3).view(C)
anz = a.nonzero()
bnz = b.nonzero()
type(anz[0])
<type 'numpy.ndarray'>b = np.arange(6).reshape(2, 3).view(C)
anz = a.nonzero()
bnz = b.nonzero()
type(anz[0])
anz[0].flags
C_CONTIGUOUS : TrueF_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
anz[0].base
type(bnz[0])
<class '__main__.C'>type(bnz[0])
bnz[0].flags
C_CONTIGUOUS : FalseF_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : False
ALIGNED : True
UPDATEIFCOPY : False
bnz[0].base
array([[0, 1],[0, 2],
[1, 0],
[1, 1],
[1, 2]])
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.
I have a branch that attempts to fix this by making both 1-D and n-D arrays:
1. return a view, never the base array,
2. return an ndarray, never a subclass, and
3. return a writeable view.
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.
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?
Jaime
--
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.