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

Keith Goodman kwgoodman@gmail....
Wed Dec 2 21:42:33 CST 2009


On Wed, Dec 2, 2009 at 7:31 PM, Neal Becker <ndbecker2@gmail.com> wrote:
> Keith Goodman wrote:
>
>> On Wed, Dec 2, 2009 at 7:15 PM, Neal Becker <ndbecker2@gmail.com>
> 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 pyublas.  Here's the main pieces:
>>>>
>>>> template<typename out_t, typename in_t>
>>>> inline out_t partial_sum (in_t const& in) {
>>>> out_t out (boost::size (in));
>>>> std::partial_sum (boost::begin (in), boost::end (in), boost::begin
>>>> (out));
>>>> return out;
>>>> }
>>>> ...
>>>> def ("partial_sum",
>>>>
>>>
> &partial_sum<pyublas::numpy_vector<T>,pyublas::numpy_strided_vector<T>
>>>>>);
>>>
>>> Oops, sorry, that's the wrong one (that was partial_sum, not
>>> partial_sort).  I don't have a wrapper for that one, but it would
>>> probably be easy enough to do with the same tools as above.
>>
>> Is a partial sum a cumsum? How does the speed of your code above
>> compare to numpy's cumsum?
>>
>>>> y = np.random.rand(250000)
>>>> timeit y.cumsum()
>> 1000 loops, best of 3: 1.05 ms per loop
>  timeit y.cumsum()
> 1000 loops, best of 3: 1.08 ms per loop
> from numpy_fncs import partial_sum
> : timeit partial_sum(y)
> 1000 loops, best of 3: 554 us per loop

Nice.


More information about the NumPy-Discussion mailing list