我正在尝试使用非平凡函数遍历np.array,并采用数组的n元素和其他一些参数。但是我已经注意到,到目前为止,最有效的方法是使用列表理解,还有其他更快的方法可以执行此操作吗? “地图”功能似乎并没有更快,而且我不敢相信显式循环是最快的方法... 下面是一些测试代码和玩具数据。
import numpy as np
from sklearn.neighbors import NearestNeighbors
from functools import partial
input_data = np.array([[207843.5, 468331.5],
[209827.5, 469081.5],
[209572.5, 468870.5],
[209327.5, 468720.5],
[207722.5, 468209.5]])
coordinates = np.array([
[194072.430, 440034.24 ],
[194172.74 , 440054.48 ],
[209828.5, 469083.5],
[194166.670, 440062.43 ],
[194163.64 , 440066.4 ],
[209327.5, 468720.5],
[194145.431, 440090.24 ],
[194195.43, 440092.24 ]])
nbrs = NearestNeighbors(n_neighbors=1,
algorithm='ball_tree', leaf_size = 15).fit(coordinates)
def fun(measure,
nbrs,
coordinates
):
distances, indices = nbrs.kneighbors(measure.reshape(1,-1))
idx_closest = indices[0,0]
idx_right = idx_closest + 1
idx_left = idx_closest - 1
lowest, highest = coordinates[idx_left], coordinates[idx_right]
return lowest , highest
# 2.01 ms
%timeit [fun(measure,nbrs, coordinates) for measure in input_data]