Machine learning 使用KD树的5个最近邻
我想从红点(T-SNE2)中找到蓝点(T-SNE1)的每个点的5个最近邻。所以我写这段代码只是为了找到正确的方法,但我不确定这样做是对还是错Machine learning 使用KD树的5个最近邻,machine-learning,scikit-learn,nearest-neighbor,kdtree,Machine Learning,Scikit Learn,Nearest Neighbor,Kdtree,我想从红点(T-SNE2)中找到蓝点(T-SNE1)的每个点的5个最近邻。所以我写这段代码只是为了找到正确的方法,但我不确定这样做是对还是错 X = np.random.random((10, 2)) # 10 points in 3 dimensions Y = np.random.random((10, 2)) # 10 points in 3 dimensions NNlist=[] treex = KDTree(X, leaf_size=2) for i in range(len(Y
X = np.random.random((10, 2)) # 10 points in 3 dimensions
Y = np.random.random((10, 2)) # 10 points in 3 dimensions
NNlist=[]
treex = KDTree(X, leaf_size=2)
for i in range(len(Y)):
dist, ind = treex.query([Y[i]], k=5)
NNlist.append(ind[0][0])
print(ind) # indices of 5 closest neighbors
print(dist)
print("the nearest index is:" ,ind[0][0],"with distance:",dist[0][0], "for Y",i)
print(NNlist)
输出
你可以得到
index [[10 5 8 9 1 2]]
distance [[ 0. 0.3393312 0.38565112 0.40120109 0.44200758 0.47675255]]
5 nearest neighbors
[ 0.6298789 0.18283264]
[ 0.42952574 0.83918788]
[ 0.26258905 0.4115705 ]
[ 0.61789523 0.96261285]
[ 0.92417172 0.13276541]
index [[10 1 3 8 4 9]]
distance [[ 0. 0.09176157 0.18219064 0.21845335 0.28876942 0.60082231]]
5 nearest neighbors
[ 0.61789523 0.96261285]
[ 0.51031835 0.99761715]
[ 0.42952574 0.83918788]
[ 0.3744326 0.97577322]
[ 0.26258905 0.4115705 ]
index [[10 7 0 9 5 6]]
distance [[ 0. 0.15771386 0.2751765 0.3457175 0.49918935 0.70597498]]
5 nearest neighbors
[ 0.19803817 0.23495888]
[ 0.41293849 0.05585981]
[ 0.26258905 0.4115705 ]
[ 0.6298789 0.18283264]
[ 0.04527532 0.78806495]
index [[10 0 5 7 9 2]]
distance [[ 0. 0.09269963 0.20597988 0.24505542 0.31104979 0.49743673]]
5 nearest neighbors
[ 0.41293849 0.05585981]
[ 0.6298789 0.18283264]
[ 0.19803817 0.23495888]
[ 0.26258905 0.4115705 ]
[ 0.92417172 0.13276541]
index [[10 9 5 7 0 8]]
distance [[ 0. 0.20406876 0.26125464 0.30645317 0.33369641 0.45509834]]
5 nearest neighbors
[ 0.26258905 0.4115705 ]
[ 0.6298789 0.18283264]
[ 0.19803817 0.23495888]
[ 0.41293849 0.05585981]
[ 0.42952574 0.83918788]
index [[10 5 2 0 7 9]]
distance [[ 0. 0.13641503 0.17524716 0.34224271 0.56393988 0.56893897]]
5 nearest neighbors
[ 0.6298789 0.18283264]
[ 0.92417172 0.13276541]
[ 0.41293849 0.05585981]
[ 0.19803817 0.23495888]
[ 0.26258905 0.4115705 ]
index [[10 7 9 0 5 6]]
distance [[ 0. 0.04152391 0.22807566 0.25709252 0.43421854 0.61332497]]
5 nearest neighbors
[ 0.19803817 0.23495888]
[ 0.26258905 0.4115705 ]
[ 0.41293849 0.05585981]
[ 0.6298789 0.18283264]
[ 0.04527532 0.78806495]
index [[10 5 1 2 8 3]]
distance [[ 0. 0.40641681 0.43652515 0.44861766 0.45186271 0.51705369]]
5 nearest neighbors
[ 0.6298789 0.18283264]
[ 0.61789523 0.96261285]
[ 0.92417172 0.13276541]
[ 0.42952574 0.83918788]
[ 0.51031835 0.99761715]
index [[10 6 9 7 8 4]]
distance [[ 0. 0.17568369 0.2841519 0.40184611 0.43110847 0.47835169]]
5 nearest neighbors
[ 0.04527532 0.78806495]
[ 0.26258905 0.4115705 ]
[ 0.19803817 0.23495888]
[ 0.42952574 0.83918788]
[ 0.3744326 0.97577322]
index [[10 9 7 5 0 8]]
distance [[ 0. 0.11723769 0.2275565 0.32111803 0.32446146 0.4643181 ]]
5 nearest neighbors
[ 0.26258905 0.4115705 ]
[ 0.19803817 0.23495888]
[ 0.6298789 0.18283264]
[ 0.41293849 0.05585981]
[ 0.42952574 0.83918788]
import numpy as np
from scipy.spatial import KDTree
X = np.random.random((10, 2)) # 10 points in 3 dimensions
Y = np.random.random((10, 2)) # 10 points in 3 dimensions
NNlist=[]
for i in range(len(X)):
treey = KDTree(np.concatenate([Y.tolist(), np.expand_dims(X[i], axis=0)], axis=0))
dist, ind = treey.query([X[i]], k=6)
print('index', ind) # indices of 5 closest neighbors
print('distance', dist)
print('5 nearest neighbors')
for j in ind[0][1:]:
print(Y[j])
print()
index [[10 5 8 9 1 2]]
distance [[ 0. 0.3393312 0.38565112 0.40120109 0.44200758 0.47675255]]
5 nearest neighbors
[ 0.6298789 0.18283264]
[ 0.42952574 0.83918788]
[ 0.26258905 0.4115705 ]
[ 0.61789523 0.96261285]
[ 0.92417172 0.13276541]
index [[10 1 3 8 4 9]]
distance [[ 0. 0.09176157 0.18219064 0.21845335 0.28876942 0.60082231]]
5 nearest neighbors
[ 0.61789523 0.96261285]
[ 0.51031835 0.99761715]
[ 0.42952574 0.83918788]
[ 0.3744326 0.97577322]
[ 0.26258905 0.4115705 ]
index [[10 7 0 9 5 6]]
distance [[ 0. 0.15771386 0.2751765 0.3457175 0.49918935 0.70597498]]
5 nearest neighbors
[ 0.19803817 0.23495888]
[ 0.41293849 0.05585981]
[ 0.26258905 0.4115705 ]
[ 0.6298789 0.18283264]
[ 0.04527532 0.78806495]
index [[10 0 5 7 9 2]]
distance [[ 0. 0.09269963 0.20597988 0.24505542 0.31104979 0.49743673]]
5 nearest neighbors
[ 0.41293849 0.05585981]
[ 0.6298789 0.18283264]
[ 0.19803817 0.23495888]
[ 0.26258905 0.4115705 ]
[ 0.92417172 0.13276541]
index [[10 9 5 7 0 8]]
distance [[ 0. 0.20406876 0.26125464 0.30645317 0.33369641 0.45509834]]
5 nearest neighbors
[ 0.26258905 0.4115705 ]
[ 0.6298789 0.18283264]
[ 0.19803817 0.23495888]
[ 0.41293849 0.05585981]
[ 0.42952574 0.83918788]
index [[10 5 2 0 7 9]]
distance [[ 0. 0.13641503 0.17524716 0.34224271 0.56393988 0.56893897]]
5 nearest neighbors
[ 0.6298789 0.18283264]
[ 0.92417172 0.13276541]
[ 0.41293849 0.05585981]
[ 0.19803817 0.23495888]
[ 0.26258905 0.4115705 ]
index [[10 7 9 0 5 6]]
distance [[ 0. 0.04152391 0.22807566 0.25709252 0.43421854 0.61332497]]
5 nearest neighbors
[ 0.19803817 0.23495888]
[ 0.26258905 0.4115705 ]
[ 0.41293849 0.05585981]
[ 0.6298789 0.18283264]
[ 0.04527532 0.78806495]
index [[10 5 1 2 8 3]]
distance [[ 0. 0.40641681 0.43652515 0.44861766 0.45186271 0.51705369]]
5 nearest neighbors
[ 0.6298789 0.18283264]
[ 0.61789523 0.96261285]
[ 0.92417172 0.13276541]
[ 0.42952574 0.83918788]
[ 0.51031835 0.99761715]
index [[10 6 9 7 8 4]]
distance [[ 0. 0.17568369 0.2841519 0.40184611 0.43110847 0.47835169]]
5 nearest neighbors
[ 0.04527532 0.78806495]
[ 0.26258905 0.4115705 ]
[ 0.19803817 0.23495888]
[ 0.42952574 0.83918788]
[ 0.3744326 0.97577322]
index [[10 9 7 5 0 8]]
distance [[ 0. 0.11723769 0.2275565 0.32111803 0.32446146 0.4643181 ]]
5 nearest neighbors
[ 0.26258905 0.4115705 ]
[ 0.19803817 0.23495888]
[ 0.6298789 0.18283264]
[ 0.41293849 0.05585981]
[ 0.42952574 0.83918788]