Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-03 Thread Charles R Harris
On Thu, Dec 3, 2009 at 10:24 AM, Howard Chong wrote: > Thanks for all the help and suggestions. I think the partial sort is > exactly what I need. > > I thought of doing it as a full sort with argsort(), but that would be much > slower if I just need a small number (maybe 7) from a large array, >

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-03 Thread Howard Chong
you are wondering, I am doing this to find the 7 nearest neighbors in a GIS. For a list of zipcodes in America, find the 7 nearest weather stations. > Message: 8 > Date: Wed, 02 Dec 2009 22:26:29 -0500 > From: Neal Becker > Subject: Re: [Numpy-discussion] Find the N maximu

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Keith Goodman
On Wed, Dec 2, 2009 at 7:31 PM, Neal Becker wrote: > Keith Goodman wrote: > >> On Wed, Dec 2, 2009 at 7:15 PM, Neal Becker > wrote: >>> Neal Becker wrote: >>> Keith Goodman wrote: ... > Oh, I thought he meant there was a numpy function for partial >>> sorting. > Actually, I

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Neal Becker
Keith Goodman wrote: > On Wed, Dec 2, 2009 at 7:15 PM, Neal Becker wrote: >> Neal Becker wrote: >> >>> Keith Goodman wrote: >>> ... Oh, I thought he meant there was a numpy function for partial >> sorting. >>> Actually, I do use this myself. My code is a boost::python wrapper >> or >

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Neal Becker
Neal Becker wrote: > Keith Goodman wrote: > ... >> Oh, I thought he meant there was a numpy function for partial sorting. >> Try this one: template inline void partial_sort (in_t in, int n_el) { std::partial_sort (boost::begin (in), boost::begin(in) + n_el, boost::end (in)); } ... def ("pa

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Keith Goodman
On Wed, Dec 2, 2009 at 7:15 PM, Neal Becker wrote: > Neal Becker wrote: > >> Keith Goodman wrote: >> ... >>> Oh, I thought he meant there was a numpy function for partial > sorting. >>> >> Actually, I do use this myself.  My code is a boost::python wrapper > or >> the std::partial_sum using pyubla

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Neal Becker
Neal Becker wrote: > Keith Goodman wrote: > ... >> Oh, I thought he meant there was a numpy function for partial sorting. >> > Actually, I do use this myself. My code is a boost::python wrapper or > the std::partial_sum using pyublas. Here's the main pieces: > > template > inline out_t parti

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Neal Becker
Keith Goodman wrote: ... > Oh, I thought he meant there was a numpy function for partial sorting. > Actually, I do use this myself. My code is a boost::python wrapper or the std::partial_sum using pyublas. Here's the main pieces: template inline out_t partial_sum (in_t const& in) { out_t out

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Keith Goodman
On Wed, Dec 2, 2009 at 5:44 PM, Charles R Harris wrote: > > > On Wed, Dec 2, 2009 at 6:32 PM, Anne Archibald > wrote: >> >> 2009/12/2 David Warde-Farley : >> > On 2-Dec-09, at 8:09 PM, Neal Becker wrote: >> > >> >> Not bad, although I wonder whether a partial sort could be faster. >> > >> > Proba

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread David Warde-Farley
On 2-Dec-09, at 8:32 PM, Anne Archibald wrote: > 2009/12/2 David Warde-Farley : >> On 2-Dec-09, at 8:09 PM, Neal Becker wrote: >> >>> Not bad, although I wonder whether a partial sort could be faster. >> >> Probably (if the array is large) but depending on n, not if it's in >> Python. Ideal probl

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Keith Goodman
On Wed, Dec 2, 2009 at 5:52 PM, Anne Archibald wrote: > 2009/12/2 Keith Goodman : >> On Wed, Dec 2, 2009 at 5:27 PM, Anne Archibald >> wrote: >>> 2009/12/2 Keith Goodman : On Wed, Dec 2, 2009 at 5:09 PM, Neal Becker wrote: > David Warde-Farley wrote: > >> On 2-Dec-09, at 6:55 PM

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Anne Archibald
2009/12/2 Keith Goodman : > On Wed, Dec 2, 2009 at 5:27 PM, Anne Archibald > wrote: >> 2009/12/2 Keith Goodman : >>> On Wed, Dec 2, 2009 at 5:09 PM, Neal Becker wrote: David Warde-Farley wrote: > On 2-Dec-09, at 6:55 PM, Howard Chong wrote: > >> def myFindMaxA(myList): >

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Charles R Harris
On Wed, Dec 2, 2009 at 6:32 PM, Anne Archibald wrote: > 2009/12/2 David Warde-Farley : > > On 2-Dec-09, at 8:09 PM, Neal Becker wrote: > > > >> Not bad, although I wonder whether a partial sort could be faster. > > > > Probably (if the array is large) but depending on n, not if it's in > > Python.

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Keith Goodman
On Wed, Dec 2, 2009 at 5:27 PM, Anne Archibald wrote: > 2009/12/2 Keith Goodman : >> On Wed, Dec 2, 2009 at 5:09 PM, Neal Becker wrote: >>> David Warde-Farley wrote: >>> On 2-Dec-09, at 6:55 PM, Howard Chong wrote: > def myFindMaxA(myList): >    """implement finding maximum valu

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Anne Archibald
2009/12/2 David Warde-Farley : > On 2-Dec-09, at 8:09 PM, Neal Becker wrote: > >> Not bad, although I wonder whether a partial sort could be faster. > > Probably (if the array is large) but depending on n, not if it's in > Python. Ideal problem for Cython, though. How is Cython support for generic

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Neal Becker
Keith Goodman wrote: ... >> Not bad, although I wonder whether a partial sort could be faster. > > I'm doing a lot of sorting right now. I only need to sort the lowest > 30% of values in a 1d array (about 250k elements), the rest I don't > need to sort. How do I do a partial sort? I only know of

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Anne Archibald
2009/12/2 Keith Goodman : > On Wed, Dec 2, 2009 at 5:09 PM, Neal Becker wrote: >> David Warde-Farley wrote: >> >>> On 2-Dec-09, at 6:55 PM, Howard Chong wrote: >>> def myFindMaxA(myList):    """implement finding maximum value with for loop iteration"""    maxIndex=0    maxVal=m

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread David Warde-Farley
On 2-Dec-09, at 8:09 PM, Neal Becker wrote: > Not bad, although I wonder whether a partial sort could be faster. Probably (if the array is large) but depending on n, not if it's in Python. Ideal problem for Cython, though. David ___ NumPy-Discussion

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Keith Goodman
On Wed, Dec 2, 2009 at 5:09 PM, Neal Becker wrote: > David Warde-Farley wrote: > >> On 2-Dec-09, at 6:55 PM, Howard Chong wrote: >> >>> def myFindMaxA(myList): >>>    """implement finding maximum value with for loop iteration""" >>>    maxIndex=0 >>>    maxVal=myList[0] >>>    for index, item in e

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Neal Becker
David Warde-Farley wrote: > On 2-Dec-09, at 6:55 PM, Howard Chong wrote: > >> def myFindMaxA(myList): >>"""implement finding maximum value with for loop iteration""" >>maxIndex=0 >>maxVal=myList[0] >>for index, item in enumerate(myList): >>if item[0]>maxVal: >>

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread David Warde-Farley
On 2-Dec-09, at 6:55 PM, Howard Chong wrote: > def myFindMaxA(myList): >"""implement finding maximum value with for loop iteration""" >maxIndex=0 >maxVal=myList[0] >for index, item in enumerate(myList): >if item[0]>maxVal: >maxVal=item[0] >maxIndex=i

Re: [Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Pierre GM
On Dec 2, 2009, at 6:55 PM, Howard Chong wrote: > > My question is: how can I make the latter version run faster? I think the > answer is that I have to do the iteration in C. > > If that's the case, can anyone point me to where np.array.argmax() is > implemented so I can write np.array.argmaxN

[Numpy-discussion] Find the N maximum values and corresponding indexes in an array

2009-12-02 Thread Howard Chong
I will need to find the N largest numbers and corresponding indexes in an 1-D array. If N==1, I can easily do: def myFindMaxC(myList): """implement finding maximum value with using numpy.array()""" myA=np.array(myList) maxIndex=myA.argmax() maxVal=myA[maxIndex] return [maxInde