I recently got asked about an "arg" version of searchsorted, basically a version that could take a sorter as an additional argument. For instance: In [13]: a = np.array([5,6,8,1,6,9,0]) In [14]: s = np.argsort(a) In [17]: s Out[17]: array([6, 3, 0, 1, 4, 2, 5]) In [18]: np.searchsorted(a, [1.5, 7.2], sorter=sorter) Out[18]: array([2, 5]) In [19]: np.searchsorted(a, -1.5, sorter=s) Out[19]: 0 In [20]: np.searchsorted(a, 11.5, sorter=s) Out[20]: 7 In [21]: np.searchsorted(a, 8.5, sorter=s) Out[21]: 6 In [32]: np.searchsorted(a, 6, side='left', sorter=sorter) Out[32]: 3 In [33]: np.searchsorted(a, 6, side='right', sorter=sorter) Out[33]: 5 I've already implemented this, I wanted to ping the list and see if there were any thoughts about this small feature. Or should I just submit a PR for consideration? Thanks, Bryan
On Mon, Mar 26, 2012 at 3:34 PM, Bryan Van de Ven <bryanv@continuum.io>wrote:
I recently got asked about an "arg" version of searchsorted, basically a version that could take a sorter as an additional argument. For instance:
In [13]: a = np.array([5,6,8,1,6,9,0])
In [14]: s = np.argsort(a)
In [17]: s Out[17]: array([6, 3, 0, 1, 4, 2, 5])
In [18]: np.searchsorted(a, [1.5, 7.2], sorter=sorter) Out[18]: array([2, 5])
In [19]: np.searchsorted(a, -1.5, sorter=s) Out[19]: 0
In [20]: np.searchsorted(a, 11.5, sorter=s) Out[20]: 7
In [21]: np.searchsorted(a, 8.5, sorter=s) Out[21]: 6
In [32]: np.searchsorted(a, 6, side='left', sorter=sorter) Out[32]: 3
In [33]: np.searchsorted(a, 6, side='right', sorter=sorter) Out[33]: 5
I've already implemented this, I wanted to ping the list and see if there were any thoughts about this small feature. Or should I just submit a PR for consideration?
An alternate API would be to pass a pair (a, sorter) as the first argument. Chuck
On 3/26/12 4:57 PM, Charles R Harris wrote:
On Mon, Mar 26, 2012 at 3:34 PM, Bryan Van de Ven <bryanv@continuum.io <mailto:bryanv@continuum.io>> wrote:
I recently got asked about an "arg" version of searchsorted, basically a version that could take a sorter as an additional argument. For instance:
In [13]: a = np.array([5,6,8,1,6,9,0])
In [14]: s = np.argsort(a)
In [17]: s Out[17]: array([6, 3, 0, 1, 4, 2, 5])
In [18]: np.searchsorted(a, [1.5, 7.2], sorter=sorter) Out[18]: array([2, 5])
An alternate API would be to pass a pair (a, sorter) as the first argument.
Chuck
Sure, that would be easy enough to implement. I don't really have a preference, is there a reason you would prefer that API? Bryan
On Mon, Mar 26, 2012 at 4:32 PM, Bryan Van de Ven <bryanv@continuum.io>wrote:
On 3/26/12 4:57 PM, Charles R Harris wrote:
On Mon, Mar 26, 2012 at 3:34 PM, Bryan Van de Ven <bryanv@continuum.io>wrote:
I recently got asked about an "arg" version of searchsorted, basically a version that could take a sorter as an additional argument. For instance:
In [13]: a = np.array([5,6,8,1,6,9,0])
In [14]: s = np.argsort(a)
In [17]: s Out[17]: array([6, 3, 0, 1, 4, 2, 5])
In [18]: np.searchsorted(a, [1.5, 7.2], sorter=sorter) Out[18]: array([2, 5])
An alternate API would be to pass a pair (a, sorter) as the first argument.
Chuck
Sure, that would be easy enough to implement. I don't really have a preference, is there a reason you would prefer that API?
No, just exploring possibilities. Another would be a different name, searchargsorted or some such. I actually think that is a better alternative than the pair, but it would add another method to ndarray. Chuck
Sure, that would be easy enough to implement. I don't really have a preference, is there a reason you would prefer that API?
No, just exploring possibilities. Another would be a different name, searchargsorted or some such. I actually think that is a better alternative than the pair, but it would add another method to ndarray.
I guess after thinking about it my own personal preference is for just adding the sorter argument, perhaps others can weigh in. Just to summarize, the proposals fo far: np.searchsorted(a, 12, sorter=s) np.searchsorted((a,s), 12) np.searchargsorted(a, s, 12) Bryan
participants (2)
-
Bryan Van de Ven -
Charles R Harris