I am a student, and I try to connect each data test with KNN of data train ?
Anyone have an idea ?
X = datatest
Y = datatrain
k = 1
distmat = squareform(pdist(X, 'euclidean'))
neighbors = np.sort(np.argsort(distmat, axis=1)[:, 0:k])
plt.figure(figsize = (8, 8))
plt.scatter(Y[:,0],Y[:,1],c='red')
plt.scatter(X[:,0], X[:,1], c = 'black')
for i in np.arange(15):
for j in np.arange(k):
x1 = np.array([X[i,:][0], X[neighbors[i, j], :][0]])
x2 = np.array([X[i,:][1], X[neighbors[i, j], :][1]])
plt.plot(x1, x2, color = 'black')
This doesn't work like I want, I want every datatest point to be connected with their nearest neighbor.
this is what my code give me :
[]