[Q]Best way for an array operation?
What is the best Numpy way for the following work?
for i in range(len(x)): if x[i] > 0: y[i] = v[i] z[i] = u[i]+2
Daehyok Shin (Peter)
You do not define, what values are in z,y if x < 0, so I presume, they keep the current value.
true = greater(x,0.) z=where(true, u+2., z) y=where(true, v, y)
HTH, __Janko
Daehyok Shin writes:
What is the best Numpy way for the following work?
for i in range(len(x)): if x[i] > 0: y[i] = v[i] z[i] = u[i]+2
Daehyok Shin (Peter)
Numpydiscussion mailing list Numpydiscussion@lists.sourceforge.net http://lists.sourceforge.net/mailman/listinfo/numpydiscussion
There is (in CVS) a new function, putmask:
c = greater(x, 0) putmask(y, c, v) putmask(z, c, u+2)
The documentation is now online. Briefly: putmask(a, m, v) sets a to v where m is true.
a must be a contiguous array m must be the same total size as a (shape ignored) v will be repeated as needed to that size
The underlying work is done in C.
Original Message From: numpydiscussionadmin@lists.sourceforge.net [mailto:numpydiscussionadmin@lists.sourceforge.net]On Behalf Of Daehyok Shin Sent: Friday, October 13, 2000 5:26 PM To: Numpy Discussion Subject: [Numpydiscussion] [Q]Best way for an array operation?
What is the best Numpy way for the following work?
for i in range(len(x)): if x[i] > 0: y[i] = v[i] z[i] = u[i]+2
Daehyok Shin (Peter)
_______________________________________________ Numpydiscussion mailing list Numpydiscussion@lists.sourceforge.net http://lists.sourceforge.net/mailman/listinfo/numpydiscussion
What is the difference of putmask and where? As it seems the only difference is the inplace behavior. This becomes more and more complicated as we have this subtle difference at many places (ravel vs. flat) and in future also the augmented assignment stuff, which also works for arrays now, although I do not know, if it's really an inplace assignment. What are the next functions for which a spacesaving variant is introduced? Would it be better to come to another convention for this type of optimization? As a side note the order of arguments is different for putmask and where.
__Janko
Paul F. Dubois writes:
There is (in CVS) a new function, putmask:
c = greater(x, 0) putmask(y, c, v) putmask(z, c, u+2)
The documentation is now online. Briefly: putmask(a, m, v) sets a to v where m is true.
a must be a contiguous array m must be the same total size as a (shape ignored) v will be repeated as needed to that size
The underlying work is done in C.
Original Message From: numpydiscussionadmin@lists.sourceforge.net [mailto:numpydiscussionadmin@lists.sourceforge.net]On Behalf Of Daehyok Shin Sent: Friday, October 13, 2000 5:26 PM To: Numpy Discussion Subject: [Numpydiscussion] [Q]Best way for an array operation?
What is the best Numpy way for the following work?
for i in range(len(x)): if x[i] > 0: y[i] = v[i] z[i] = u[i]+2
Daehyok Shin (Peter)
Numpydiscussion mailing list Numpydiscussion@lists.sourceforge.net http://lists.sourceforge.net/mailman/listinfo/numpydiscussion _______________________________________________ Numpydiscussion mailing list Numpydiscussion@lists.sourceforge.net http://lists.sourceforge.net/mailman/listinfo/numpydiscussion
I have a problem that I can't figure otu a nonkludgey way to solve:
Given a (MXN) array, a, I want to compute the minimum value in each row, excluding the values that are zero. example:
for this array, a:
[[10, 0, 5, 0,] [ 0, 0, 0, 0,] [ 0, 5,15, 0,] [ 0, 0, 0, 0,] [ 0, 3, 1,25,] [ 0, 0, 0, 0,] [ 4, 7, 2,12,] [ 0, 0, 0, 0,]]
I want:
[[5,] [0,] [5,] [0,] [1,] [0,] [2,] [0,]]
what I want is minimum.reduce(a,1), except that the zeros would be ignored. At first glace, Masked arrays seemed the perfect option (as that's what the zero means, no real data), but there doesn't seem to be a minimum.reduce() in MA.
By the way, what is the downside to Masked Arrays? they seem to be a really powerful option. Is there any performance hit to using a masked array over a regular onw if there is no mask? If not, would it make sense to move toward just using Masked arrays for everything?
thanks,
Chris
Original Message ...snip
what I want is minimum.reduce(a,1), except that the zeros would be ignored. At first glace, Masked arrays seemed the perfect option (as that's what the zero means, no real data), but there doesn't seem to be a minimum.reduce() in MA.
> I'll add this to my list. Meanwhile you could do something like this, where x is your array: y = MA.masked_equal(x,0,copy=0) Numeric.minimum.reduce(y.filled(100000), 1) If x is floating point use masked_value rather than masked_equal.  By the way, what is the downside to Masked Arrays? they seem to be a really powerful option. Is there any performance hit to using a masked array over a regular onw if there is no mask? If not, would it make sense to move toward just using Masked arrays for everything?
thanks,
Chris
> There are two problems. There is a hit, even in the case of no mask. It is hard to quantify because it depends on what operations you do. Much of that could be eliminated if MA was in C but it is Python. It would have driven me mad to write it in C, however.
More seriously, there are a few semantic issues where it isn't clear what you mean if a mask is present.
participants (5)

Chris Barker

Daehyok Shin

Janko Hauser

Paul F. Dubois

Paul F. Dubois