From: Daehyok S. <sd...@em...> - 2000-10-13 21:24:18
|
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) |
From: Janko H. <jh...@if...> - 2000-10-13 23:14:14
|
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) > > > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > http://lists.sourceforge.net/mailman/listinfo/numpy-discussion |
From: Paul F. D. <pau...@ho...> - 2000-10-14 16:01:36
|
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: num...@li... [mailto:num...@li...]On Behalf Of Daehyok Shin Sent: Friday, October 13, 2000 5:26 PM To: Numpy Discussion Subject: [Numpy-discussion] [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) _______________________________________________ Numpy-discussion mailing list Num...@li... http://lists.sourceforge.net/mailman/listinfo/numpy-discussion |
From: Janko H. <jh...@if...> - 2000-10-14 18:33:14
|
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: num...@li... > [mailto:num...@li...]On Behalf Of > Daehyok Shin > Sent: Friday, October 13, 2000 5:26 PM > To: Numpy Discussion > Subject: [Numpy-discussion] [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) > > > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > http://lists.sourceforge.net/mailman/listinfo/numpy-discussion > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > http://lists.sourceforge.net/mailman/listinfo/numpy-discussion |
From: Chris B. <chr...@ho...> - 2001-05-04 18:37:05
|
I have a problem that I can't figure otu a non-kludgey 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 -- Christopher Barker, Ph.D. Chr...@ho... --- --- --- http://members.home.net/barkerlohmann ---@@ -----@@ -----@@ ------@@@ ------@@@ ------@@@ Oil Spill Modeling ------ @ ------ @ ------ @ Water Resources Engineering ------- --------- -------- Coastal and Fluvial Hydrodynamics -------------------------------------- ------------------------------------------------------------------------ |
From: Paul F. D. <pa...@pf...> - 2001-05-04 19:15:03
|
-----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. |
From: Chris B. <chr...@ho...> - 2001-05-04 21:27:38
|
Janko and Paul, Thanks for your suggestions. I had come up with something like Janko's solution, but not as cleanly. It seems that Paul's suggestion is essentially the same thing. Maybe I'll do some tests to see which is faster. By the way, I tried Janko's code, and got an error on "a == 0" (I'm running Python 2.0). I looked at it again, and then looked at the release notes for Python 2.1, and saw that rich comparisons are working..Glory be! I'll be upgrading real soon, just for that! thanks again, -Chris PS: anyone have a response for my Int32 question? Why isn't it a "long" on Linux or windows on Intel? Janko Hauser wrote: > truely_max=100 > > a=array([[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,]]) > > am=minimum.reduce(where(a==0,truely_max,a),1) > am=where(am==truely_max,0,am) > print am > [5 0 5 0 1 0 2 0] > "Paul F. Dubois" wrote: > but there doesn't seem to be > a minimum.reduce() in MA. > > --> I'll add this to my list. That will be great if you get a chance. > 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. > --- > --> 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. WOW, I'm impressed, I had assumed that some C was involved. Nice job. > More seriously, there are a few semantic issues where it isn't clear > what you mean if a mask is present. I'm sure these could be resolved. It would be nice if we could have in mind to move in that direction. I'm not going ot be able to do any of the coding, so that's the last you'll here from me about it. -Chris -- Christopher Barker, Ph.D. Chr...@ho... --- --- --- http://members.home.net/barkerlohmann ---@@ -----@@ -----@@ ------@@@ ------@@@ ------@@@ Oil Spill Modeling ------ @ ------ @ ------ @ Water Resources Engineering ------- --------- -------- Coastal and Fluvial Hydrodynamics -------------------------------------- ------------------------------------------------------------------------ |