[Numpy-discussion] strange behavior of ravel() and flatnonzero() on matrix

braingateway braingateway@gmail....
Wed Nov 3 18:07:44 CDT 2010


braingateway :
>>>> aa=matrix([[-1, 2, 0],[0, 0, 3]])
>>>> aa
>>>>         
> matrix([[-1, 2, 0],
> [ 0, 0, 3]])
>   
>>>> aa.nonzero()
>>>>         
> (matrix([[0, 0, 1]], dtype=int64), matrix([[0, 1, 2]], dtype=int64))
> *********OK*********
>   
>>>> npy.nonzero(aa.flat)
>>>>         
> (array([0, 1, 5], dtype=int64),)
> *********OK*********
>   
>>>> flatnonzero(aa)
>>>>         
> matrix([[0, 0, 0]], dtype=int64)
> *******This is Wrong**********
> If I convert aa to an ndarray, it is OK then
> aaa=asarray(aa)
>   
>>>> flatnonzero(aaa)
>>>>         
> array([0, 1, 5], dtype=int64)
>
> Then I figure it out that it might be induced by the behavior of ravel()
>   
>>>> aaa.shape
>>>>         
> (2L, 3L)
>   
>>>> aaa.ravel().shape
>>>>         
> (6L,)
>   
>>>> aa.ravel()
>>>>         
> matrix([[-1, 2, 0, 0, 0, 3]])
>   
>>>> _.shape
>>>>         
> (1L, 6L)
> Why not make ravel() behaviors consistent under both ndarray and matrix
> contexts?
> Or make different flatnonzero() for the matrix context?
> m.ravel().nonzero()[1]# for matrix
> a.ravel().nonzero()[0]# for ndarray
>
>   
here is the numpy version:
>>> numpy.__version__
'1.5.0'




More information about the NumPy-Discussion mailing list