[Numpy-discussion] setting element
Hello, I'm wondering if there's aquick way to do the following: s[:,5]=value in a general function def setval(array,index,value,axis=0): ## code here The issue is to put enough : before the index value inside the square bracket of the assignement. Thanks, C. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] setting element
On Wed, Nov 12, 2008 at 09:43:34AM -0800, Charles سمير Doutriaux wrote: Hello, I'm wondering if there's aquick way to do the following: s[:,5]=value in a general function def setval(array,index,value,axis=0): ## code here The issue is to put enough : before the index value inside the square bracket of the assignement. Make some slice objects! def setval(array, index, value, axis=0): key = [slice(None)]*len(array.shape) key[axis] = index array[key] = value Gabriel ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] setting element
On Wed, Nov 12, 2008 at 12:34:51PM -0600, Ryan May wrote: Charles سمير Doutriaux wrote: Hello, I'm wondering if there's aquick way to do the following: s[:,5]=value in a general function def setval(array,index,value,axis=0): ## code here Assuming that axis specifies where the index goes, that would be: def setval(array, index, value, axis=0): slices = [slice(None)] * len(array.shape) slices[axis] = index array[slices] = value (Adapted from the code for numpy.diff) Ryan Jinx! ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] setting element
Nope this one wouldn't have worked for me, it's basically axis=-1 but there might be additional dimensions after index C. On Nov 12, 2008, at 10:43 AM, Roberto De Almeida wrote: On Wed, Nov 12, 2008 at 4:36 PM, Gabriel Gellner [EMAIL PROTECTED] wrote: On Wed, Nov 12, 2008 at 12:34:51PM -0600, Ryan May wrote: Charles سمير Doutriaux wrote: Hello, I'm wondering if there's aquick way to do the following: s[:,5]=value in a general function def setval(array,index,value,axis=0): ## code here Assuming that axis specifies where the index goes, that would be: def setval(array, index, value, axis=0): slices = [slice(None)] * len(array.shape) slices[axis] = index array[slices] = value (Adapted from the code for numpy.diff) Ryan Jinx! Shouldn't s[...,index] = value work too? ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http:// projects.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] setting element
Thx! On Nov 12, 2008, at 10:36 AM, Gabriel Gellner wrote: On Wed, Nov 12, 2008 at 12:34:51PM -0600, Ryan May wrote: Charles سمير Doutriaux wrote: Hello, I'm wondering if there's aquick way to do the following: s[:,5]=value in a general function def setval(array,index,value,axis=0): ## code here Assuming that axis specifies where the index goes, that would be: def setval(array, index, value, axis=0): slices = [slice(None)] * len(array.shape) slices[axis] = index array[slices] = value (Adapted from the code for numpy.diff) Ryan Jinx! ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http:// projects.scipy.org/mailman/listinfo/numpy-discussion ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] setting element
On Wed, Nov 12, 2008 at 4:36 PM, Gabriel Gellner [EMAIL PROTECTED]wrote: On Wed, Nov 12, 2008 at 12:34:51PM -0600, Ryan May wrote: Charles سمير Doutriaux wrote: Hello, I'm wondering if there's aquick way to do the following: s[:,5]=value in a general function def setval(array,index,value,axis=0): ## code here Assuming that axis specifies where the index goes, that would be: def setval(array, index, value, axis=0): slices = [slice(None)] * len(array.shape) slices[axis] = index array[slices] = value (Adapted from the code for numpy.diff) Ryan Jinx! Shouldn't s[...,index] = value work too? ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] setting element
Charles سمير Doutriaux wrote: Hello, I'm wondering if there's aquick way to do the following: s[:,5]=value in a general function def setval(array,index,value,axis=0): ## code here Assuming that axis specifies where the index goes, that would be: def setval(array, index, value, axis=0): slices = [slice(None)] * len(array.shape) slices[axis] = index array[slices] = value (Adapted from the code for numpy.diff) Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Setting element to masked in a masked array previously containing no masked values
Hi numpy users, I have a masked array. I am looping over the elements of this array and sometimes want to set a value to missing. Normally this can be done by: myarray.mask[i] = True However the mask attribute is not indexable when there are no existing missing values in the array (it is simply False). In this case I therefore get an error message: myarray.mask[i] = True TypeError: object does not support item assignment Is the best way to solve the problem to do something like this: mask = ma.getmaskarray(myarray) for i in range(n): if blahblah: mask[i] = True myarray = ma.array(myarray, copy=False, mask=mask) or is there a more elegant solution? Does anyone by the way have any pointers to documentation of the masked array features of numpy? I know that it is treated in the numarray manual but it seems like there are some important syntax differences that make this manual of little use in that regard. - Jesper ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Setting element to masked in a masked array previously containing no masked values
On Monday 25 June 2007 05:12:01 Jesper Larsen wrote: Hi numpy users, I have a masked array. I am looping over the elements of this array and sometimes want to set a value to missing. Normally this can be done by: myarray.mask[i] = True Mmh. Experience shows that directly accessing the mask can lead to bad surprises. To mask a series of values in an array, the easiest (and recommended method) is myarray[i] = masked where 'i' can be whatever object used for indexing (an integer, a sequence, a slice...). Does anyone by the way have any pointers to documentation of the masked array features of numpy? I know that it is treated in the numarray manual but it seems like there are some important syntax differences that make this manual of little use in that regard. I can't really point you to any documentation. The differences of syntax should be minimal. We could however start a wiki page. A side issue is the kind of implementation of masked arrays you want. There are currently two, one directly accessible through numpy.core.ma, another available in the sandbox of the scipy svn site, as maskedarray. This latter considers MaskedArray as a subclass of ndarray, which makes it easier to define subclasses. Moreover, it gives access to soft/hard masks, masked records, more stats functions, and thanks to Eric Firing, can be used directly with matplotlib... I'd be quite happy if you could give it a try and send me your feedback. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Setting element to masked in a masked array previously containing no masked values
Hi Pierre and others, On Monday 25 June 2007 15:37, Pierre GM wrote: On Monday 25 June 2007 05:12:01 Jesper Larsen wrote: myarray.mask[i] = True Mmh. Experience shows that directly accessing the mask can lead to bad surprises. To mask a series of values in an array, the easiest (and recommended method) is myarray[i] = masked I was not aware that the way to use masked arrays was as you describe. I thought you had to somehow modify the mask (but the method you describe is of course much more elegant). Thanks for answering my very basic question. A side issue is the kind of implementation of masked arrays you want. There are currently two, one directly accessible through numpy.core.ma, another available in the sandbox of the scipy svn site, as maskedarray. This latter considers MaskedArray as a subclass of ndarray, which makes it easier to define subclasses. Moreover, it gives access to soft/hard masks, masked records, more stats functions, and thanks to Eric Firing, can be used directly with matplotlib... I'd be quite happy if you could give it a try and send me your feedback. No, no, I don't want that kind of implementation - I just want to figure out how the current implementation works:-) When I get some more experience in using masked arrays and require some of the functionality that you describe I will give it a try. - Jesper ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Setting element to masked in a masked array previously containing no masked values
On Monday 25 June 2007 10:14:21 Jesper Larsen wrote: Hi Pierre and others, I was not aware that the way to use masked arrays was as you describe. I thought you had to somehow modify the mask (but the method you describe is of course much more elegant). Thanks for answering my very basic question. You're quite welcome. Corollary: to unmask values, use myarray[i] = nomask of course, use with caution: there are cases where unmasking masked values can lead to bad surprise (eg, if the masked values were not defined...) No, no, I don't want that kind of implementation - I just want to figure out how the current implementation works:-) When I get some more experience in using masked arrays and require some of the functionality that you describe I will give it a try. You know, the 'new' implementation was designed to be as close as the one available in numpy, so that shouldn't be a problem at all. Besides, it fixes some issues, so I can't but advise you to try it when you'll see fit... ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion