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

Neal Becker ndbecker2@gmail....
Wed Dec 2 21:26:29 CST 2009


Neal Becker wrote:

> Keith Goodman wrote:
> ...
>> Oh, I thought he meant there was a numpy function for partial 
sorting.
>> 
Try this one:

template<typename in_t>
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 ("partial_sort", 
&partial_sort<pyublas::numpy_strided_vector<int32_t> >);
  def ("partial_sort", 
&partial_sort<pyublas::numpy_strided_vector<int64_t> >);
  def ("partial_sort", 
&partial_sort<pyublas::numpy_strided_vector<double> >);
---------
import pyublas
import numpy as np
u = np.arange (20)[::-1]
from numpy_fncs import partial_sort
partial_sort (u, 4)
In [2]: u
Out[2]: 
array([ 0,  1,  2,  3, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,  
7,
        6,  5,  4])



More information about the NumPy-Discussion mailing list