From: Keith G. <kwg...@gm...> - 2006-11-12 21:57:19
|
Is anybody interested in making x.max() and nanmax() behave the same for matrices, except for the NaN part? That is, make numpy.matlib.nanmax return a matrix instead of an array. Example ===== >> import numpy.matlib as M >> x = M.rand(3,2) >> x.max(0) matrix([[ 0.91749823, 0.94942954]]) >> M.nanmax(x, 0) array([ 0.91749823, 0.94942954]) |
From: A. M. A. <per...@gm...> - 2006-11-12 22:09:17
|
On 12/11/06, Keith Goodman <kwg...@gm...> wrote: > Is anybody interested in making x.max() and nanmax() behave the same > for matrices, except for the NaN part? That is, make > numpy.matlib.nanmax return a matrix instead of an array. Sounds good to me; maybe put a patch in the tracker? A. M. Archibald |
From: Pierre GM <pgm...@gm...> - 2006-11-12 23:09:50
|
On Sunday 12 November 2006 17:08, A. M. Archibald wrote: > On 12/11/06, Keith Goodman <kwg...@gm...> wrote: > > Is anybody interested in making x.max() and nanmax() behave the same > > for matrices, except for the NaN part? That is, make > > numpy.matlib.nanmax return a matrix instead of an array. Or, you could use masked arrays... In the new implementation, you can add a mask to a subclassed array (such as matrix) to get a regular masked array. If you fill this masked array, you get an array of the same subclass. >>> import numpy as N >>> import numpy.matlib as M >>> import maskedarray as MA >>> x=M.rand(3,3) >>> assert isinstance(x.max(0), M.matrix) >>> assert isinstance(N.max(x,0), M.matrix) >>> assert isinstance(MA.max(x,0).filled(0), M.matrix) >>> assert isinstance(MA.max(x,0)._data, M.matrix) >>> x[-1,-1] = N.nan >>> tmp = MA.max(MA.array(x,mask=N.isnan(x)), 0) >>> assert (tmp == N.nanmax(x,0)).all() >>> assert isinstance(tmp.filled(0), M.matrix) |
From: Sven S. <sve...@gm...> - 2006-11-13 10:03:38
|
Pierre GM schrieb: > On Sunday 12 November 2006 17:08, A. M. Archibald wrote: >> On 12/11/06, Keith Goodman <kwg...@gm...> wrote: >>> Is anybody interested in making x.max() and nanmax() behave the same >>> for matrices, except for the NaN part? That is, make >>> numpy.matlib.nanmax return a matrix instead of an array. > > Or, you could use masked arrays... In the new implementation, you can add a > mask to a subclassed array (such as matrix) to get a regular masked array. If > you fill this masked array, you get an array of the same subclass. > That is very interesting, but I agree with Keith and would actually call this a bug. (If still present in 1.0, that is, haven't checked, I think Keith used some rc?.) One proclaimed goal of numpy for the 1.0 release has been to be as matrix-friendly as possible, for which I am very grateful. Still, the use of masked arrays looks more attractive every time they're mentioned... -sven |
From: Keith G. <kwg...@gm...> - 2006-11-15 17:55:42
|
On 11/12/06, Pierre GM <pgm...@gm...> wrote: > On Sunday 12 November 2006 17:08, A. M. Archibald wrote: > > On 12/11/06, Keith Goodman <kwg...@gm...> wrote: > > > Is anybody interested in making x.max() and nanmax() behave the same > > > for matrices, except for the NaN part? That is, make > > > numpy.matlib.nanmax return a matrix instead of an array. > > Or, you could use masked arrays... In the new implementation, you can add a > mask to a subclassed array (such as matrix) to get a regular masked array. If > you fill this masked array, you get an array of the same subclass. > > >>> import numpy as N > >>> import numpy.matlib as M > >>> import maskedarray as MA > >>> x=M.rand(3,3) > >>> assert isinstance(x.max(0), M.matrix) > >>> assert isinstance(N.max(x,0), M.matrix) > >>> assert isinstance(MA.max(x,0).filled(0), M.matrix) > >>> assert isinstance(MA.max(x,0)._data, M.matrix) > > >>> x[-1,-1] = N.nan > >>> tmp = MA.max(MA.array(x,mask=N.isnan(x)), 0) > >>> assert (tmp == N.nanmax(x,0)).all() > >>> assert isinstance(tmp.filled(0), M.matrix) I didn't know you could use masked arrays with matrices. I guess I took the name literally. I think an easier way to use masked arrays would be to introduce a new thing called mis. I could make a regular matrix x = M.rand(3,3) and assign a missing value x[0,0] = M.mis x would then behave as a missing array matrix. I could also do x[M.isnan(x)] = M.mis or x[mask] = M.mis To get the mask from x: x.mask or M.ismis(x) I think that would make missing arrays accessible to everyone. |
From: Pierre GM <pgm...@gm...> - 2006-11-16 02:01:21
|
On Wednesday 15 November 2006 12:55, Keith Goodman wrote: > I didn't know you could use masked arrays with matrices. I guess I > took the name literally. :) Please check the developer zone: http://projects.scipy.org/scipy/numpy/wiki/MaskedArray for an alternative implementation of masked arrays that support subclasses of ndarray. > I think an easier way to use masked arrays would be to introduce a new > thing called mis. > > I could make a regular matrix > > x = M.rand(3,3) > > and assign a missing value > > x[0,0] = M.mis > > x would then behave as a missing array matrix. .... > I think that would make missing arrays accessible to everyone. Well, there's already something like that, sort of: MA.masked, or MA.masked_singleton. The emphasis here is on "sort of". That works well if x is already a masked array. Else, a "ValueError: setting an array element with a sequence" is raised. I haven't tried to find where the problem comes from (ndarray.__setitem__ ? The masked_singleton larger than it seems ?), but I wonder whether it's an issue worth solving. If you want to get a masked_matrix from x, just type x=masked_array(x). You won't be able to access some specific matrix attributes (A, T), at least directly, but you can fill your masked_matrix and get a matrix back. And multiplication of two masked_matrices work as expected ! The main advantage of this approach is that we don't overload ndarray or matrices, the work is solely on the masked_array side. |