2

1[enter image description here]2

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 :

[enter image description here]

Srh
  • 21
  • 2
  • What exactly do you mean connecting data test with KNN? are you inferring to find neighbors on test Data using KNN? – Surya Tej Jan 03 '20 at 21:26
  • yes, i want to find neighbors on test Data and to connect them with matplotlib – Srh Jan 03 '20 at 21:27
  • i want to do something like the picture in the post – Srh Jan 03 '20 at 21:31
  • Hope this helps out https://stackoverflow.com/questions/35363444/plotting-lines-connecting-points. Identify the points near to your neighbor and connect them – Surya Tej Jan 03 '20 at 21:35
  • First of all thank you for your answer, and i apologize for my english. I want to graphically represent for each training variable, the test variables that will use it as a prediction and connect them. – Srh Jan 03 '20 at 21:39
  • Can you include the plot that your code is producing in addition to the one showing the desired output? – William Miller Jan 03 '20 at 22:37
  • i add it, take a look please. – Srh Jan 04 '20 at 13:18

0 Answers0