我想在 python 中实现 Vantage Point Tree,但它使用 C++ 中的 std::nth_element 。
所以我想在 Python 或 numpy 中找到等效的“nth_element”函数。
请注意,nth_element 只会对数组进行部分排序,并且它是 O(N)。
int the_array[10] = {4,5,7,3,6,0,1,2,9,8};
std::vector<int> the_v(the_array,the_array+10);
std::nth_element (the_v.begin()+0, the_v.begin()+5, the_v.begin()+10);
现在向量可能是:
3,0,2,1,4,5,6,7,9,8
而且我不仅想获得第n个元素,还想重新排列列表的两个部分,[3,0,2,1,4]和[6,7,9,8]。
而且,nth_element 支持接受一个可以比较两个元素的函数,比如,在下面的as中,vector是一个vector op DataPoint,DistanceComparator函数会用the_v.begin()比较两个点的距离:
vector<DataPoint> the_v;
for(int n = 0; n < N; n++) the_v[n] = DataPoint(D, n, X + n * D);
std::nth_element (the_v.begin()+0, the_v.begin()+5, the_v.begin()+10,
DistanceComparator(the_v.begin()));
编辑:
我使用了 bhuvan-venkatesh 的答案,并编写了一些代码进行测试。
partition_timer = timeit.Timer("numpy.partition(a, 10000)",
"import numpy;numpy.random.seed(2);"+
"a = numpy.random.rand(10000000)")
print(partition_timer.timeit(10))
sort_timer = timeit.Timer("numpy.sort(a)",
"import numpy;numpy.random.seed(2);"+
"a = numpy.random.rand(10000000)")
print(sort_timer.timeit(10))
sorted_timer = timeit.Timer("sorted(a)",
"import numpy;numpy.random.seed(2);"+
"a = numpy.random.rand(10000000)")
print(sorted_timer.timeit(10))
结果:
2.2217168808
17.0386350155
281.301710844
然后,我将使用 C++ 代码进行更多测试。
但是有一个问题,当使用numpy时,它总是会返回一个新的数组,当我的数组很大时,它会浪费很多内存。我该如何处理。或者我只需要为 python 编写一个 C++ 扩展。
编辑2:
@bhuvan-venkatesh 感谢您推荐分区功能。
我使用如下分区:
import numpy
@profile
def for_numpy():
numpy.random.seed(2)
a = numpy.random.rand(1e7)
for i in range(100):
a.partition(numpy.random.randint(1e6))
if __name__ == '__main__':
for_numpy()
并像这样运行探查器:
python -m memory_profiler profiler_test.py
结果是:
Line # Mem usage Increment Line Contents
================================================
25 23.613 MiB 0.000 MiB @profile
26 def for_numpy():
27 23.613 MiB 0.000 MiB numpy.random.seed(2)
28 99.934 MiB 76.320 MiB a = numpy.random.rand(1e7)
29 100.004 MiB 0.070 MiB for i in range(100):
30 100.004 MiB 0.000 MiB a.partition(numpy.random.randint(1e6))
它不会像这样复制整个数组:numpy.partition(a, 3)
结论: numpy.ndarray.partition 是我想要找到的。