1
赤い点(T-SNE2)から青い点(T-SNE1)の各点に対して5つの最近傍を探したいと思います。だから、私はこのコードを書いて、それを行う正しい方法を見つけましたが、そうするのが正しいか間違っているのか分かりません。5 KDツリーを使用する最近のネイバー
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)
[[9 5 4 6 0]]
[[ 0.21261486 0.32859024 0.41598597 0.42960146 0.43793039]]
the nearest index is: 9 with distance: 0.212614862956 for Y 0
[[0 3 2 6 1]]
[[ 0.10907128 0.11378059 0.13984741 0.18000197 0.27475481]]
the nearest index is: 0 with distance: 0.109071275144 for Y 1
[[8 2 3 0 1]]
[[ 0.21621245 0.30543878 0.40668179 0.4370689 0.49372232]]
the nearest index is: 8 with distance: 0.216212445449 for Y 2
[[8 3 2 6 0]]
[[ 0.16648482 0.2989508 0.40967709 0.42511931 0.46589575]]
the nearest index is: 8 with distance: 0.166484820786 for Y 3
[[1 2 5 0 4]]
[[ 0.15331281 0.25121761 0.29305736 0.30173474 0.44291615]]
the nearest index is: 1 with distance: 0.153312811422 for Y 4
[[2 3 8 0 6]]
[[ 0.20441037 0.20917797 0.25121628 0.2903253 0.33914051]]
the nearest index is: 2 with distance: 0.204410367254 for Y 5
[[2 1 0 3 5]]
[[ 0.08400022 0.1484925 0.17356156 0.32387147 0.33789602]]
the nearest index is: 2 with distance: 0.0840002184199 for Y 6
[[8 2 3 7 0]]
[[ 0.2149891 0.40584999 0.50054235 0.53307269 0.5389266 ]]
the nearest index is: 8 with distance: 0.21498909502 for Y 7
[[1 0 2 5 9]]
[[ 0.07265268 0.11687068 0.19065327 0.20004392 0.30269591]]
the nearest index is: 1 with distance: 0.0726526838766 for Y 8
[[5 9 4 1 0]]
[[ 0.21563204 0.25067242 0.29904262 0.36745386 0.39634179]]
the nearest index is: 5 with distance: 0.21563203953 for Y 9
[9, 0, 8, 8, 1, 2, 2, 8, 1, 5]