使用cKDTree进行数据近邻查询

 1 import pickle
 2 from scipy.spatial import cKDTree
 3 
 4 output = open('busstop_X.pkl', 'rb')
 5 X = pickle.load(output)
 6 output.close()
 7 
 8 # X.shape
 9 m, n = X.shape
10 
11 # show
12 print(X.shape)# (42161, 2)
13 print(X[[0, 1, 2]])# [[ 451389.26206748 4425262.84838121] .... ]
14 
15 # cKDTree
16 tree = cKDTree(data=X, leafsize=8, compact_nodes=True, balanced_tree=True)
17 # Y query
18 X_query = X[[0, 1, 2]]
19 # query out
20 dis, index = tree.query(x=X_query, k=1, n_jobs=-1)
21 # X query out
22 X_query_out = X[index]
23 
24 print(dis, index, sep='\n')# [0. 0. 0.] \n [0   1   6931]
25 print(X_query)# [[ 451389.26206748 4425262.84838121] ... ]
26 print(X_query_out)# [[ 451389.26206748 4425262.84838121] ... ]

 

posted @ 2021-08-03 08:46  土博姜山山  阅读(632)  评论(0编辑  收藏  举报