From: Nadav H. <na...@vi...> - 2004-06-01 13:31:54
|
I am currently working on a simulation that makes a heavy use of searchsorted. But it does not precisely fit to what I need --- if a value v is between p and q searchsorted returns the index of q, while what I need is the index of p. Currently my solution is to turn to floating points numbers: ====================================== Python 2.3.4 (#1, May 31 2004, 09:13:03) [GCC 3.4.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. from numarray import * bins = array((0,10,20,30)) val = array((10, 15)) searchsorted(bins, val) array([1, 2]) # I really would like to get array([1, 1]) # Here is the trick: fb = bins - 0.1 fb array([ -0.1, 9.9, 19.9, 29.9]) searchsorted(fb, val) - 1 array([1, 1]) # That's it! ============================================ My questions are: 1. Is there a more elegant solution? 2. I am thinking of letting "searchsorted" return a second boolean array which has the value True for every exact match: >>> searchsorted(bins, val) >>> [array([1, 2]), array([1, 0], type=Bool)] Any comments? Nadav. |
From: Jonathan T. <jon...@st...> - 2006-06-02 18:08:08
Attachments:
jonathan.taylor.vcf
|
I was wondering if there was an easy way to get searchsorted to be "right-continuous" instead of "left-continuous". By continuity, I am talking about the continuity of the function "count" below... >>> import numpy as N >>> >>> x = N.arange(20) >>> x.searchsorted(9) 9 >>> import numpy as N >>> >>> x = N.arange(20) >>> >>> def count(u): ... return x.searchsorted(u) ... >>> count(9) 9 >>> count(9.01) 10 >>> Thanks, Jonathan -- ------------------------------------------------------------------------ I'm part of the Team in Training: please support our efforts for the Leukemia and Lymphoma Society! http://www.active.com/donate/tntsvmb/tntsvmbJTaylor GO TEAM !!! ------------------------------------------------------------------------ Jonathan Taylor Tel: 650.723.9230 Dept. of Statistics Fax: 650.725.8977 Sequoia Hall, 137 www-stat.stanford.edu/~jtaylo 390 Serra Mall Stanford, CA 94305 |
From: Charles R H. <cha...@gm...> - 2006-06-03 03:30:10
|
Jonathan, I had a patch for this that applied to numarray way back when. If folks feel there is a need, I could probably try to get it running on numpy. Bit of a learning curve (for me), though. Chuck On 6/2/06, Jonathan Taylor <jon...@st...> wrote: > > I was wondering if there was an easy way to get searchsorted to be > "right-continuous" instead of "left-continuous". > > By continuity, I am talking about the continuity of the function "count" > below... > > >>> import numpy as N > >>> > >>> x = N.arange(20) > >>> x.searchsorted(9) > 9 > >>> import numpy as N > >>> > >>> x = N.arange(20) > >>> > >>> def count(u): > ... return x.searchsorted(u) > ... > >>> count(9) > 9 > >>> count(9.01) > 10 > >>> > > Thanks, > > Jonathan > > -- > ------------------------------------------------------------------------ > I'm part of the Team in Training: please support our efforts for the > Leukemia and Lymphoma Society! > > http://www.active.com/donate/tntsvmb/tntsvmbJTaylor > > GO TEAM !!! > > ------------------------------------------------------------------------ > Jonathan Taylor Tel: 650.723.9230 > Dept. of Statistics Fax: 650.725.8977 > Sequoia Hall, 137 www-stat.stanford.edu/~jtaylo > 390 Serra Mall > Stanford, CA 94305 > > > > > > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > > > > |
From: Perry G. <pe...@st...> - 2004-06-01 16:16:12
|
Nadav Horesh writes > I am currently working on a simulation that makes a heavy use of > searchsorted. But it does not precisely fit to what I need --- if a > value v is between p and q searchsorted returns the index of q, while > what I need is the index of p. > > Currently my solution is to turn to floating points numbers: > > ====================================== > > Python 2.3.4 (#1, May 31 2004, 09:13:03) > [GCC 3.4.0] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > from numarray import * > > bins = array((0,10,20,30)) > val = array((10, 15)) > searchsorted(bins, val) > array([1, 2]) # I really would like to get array([1, 1]) > > # Here is the trick: > > fb = bins - 0.1 > fb > array([ -0.1, 9.9, 19.9, 29.9]) > > searchsorted(fb, val) - 1 > array([1, 1]) # That's it! > This is only approximate, right? If val = array([9.95, 15]) you will get the wrong answer won't you? > ============================================ > > My questions are: > > 1. Is there a more elegant solution? > 2. I am thinking of letting "searchsorted" return a second boolean > array which has the value True for every exact match: > >>> searchsorted(bins, val) > >>> [array([1, 2]), array([1, 0], type=Bool)] > Any comments? > > Nadav. > To get the latter, you could so something like ind = searchsorted(bins, val) neq_mask = bins[ind]-val ind[neq_mask] -= 1. # well, you need to handle where ind = 0 and # is not equal as well Would that suffice? Perry |