How to plot a graph using Knn algorithm?


      I tried running this code : 

nng(prc_test_pred_df, dx = NULL, k = 11, mutual = T, method = NULL)

Its running for more than an hour. Stll didint give me the plot. Genrally it takes so long ?

No of obs = 60K
no of var - 127

prc_test_pred is the predicted test data using knn algorithm.

@shuvayan @Lesaffrea @Aarshay
Can u help me with this


You have way to many features try referencing articles on feature Engineering & Selection. Meanwhile this could be helpful to learn kNN


Thanks akash. But my doubt was for plotting a graph. Knn i understood. I want to plot the decision boundary of a k-nearest neighbor …I need help for that.


Hi Rohit

nng() is slow after you reach a certain size but one hour for this size is surprising I just built a simulation on my system and it took less than 3 minutes to compute nng(). With this size you will have difficulty to plot as well I used the kawai (!) layout and then 10 minutes later still running (:

Not of great help tonight


Thanks @Lesaffrea :slightly_smiling:

So what is your suggestion ? what should i do ? Will kawai help me out in this case ?


Hi Rohit

me again I noticed you wanted the decision boundary well with nng() you will build a graph, therefore the relation. Check this for decision boundary :slight_smile: I think it help.
Decision Boundary
Now back to contour. This time I hope it helps.


Look below :slight_smile: Kawai is still running :frowning: and I do not think it will help


Decision boundary was secondary. First i need a plot. :stuck_out_tongue:

Thanks for the link though. I will go through it :slightly_smiling:

Ohh…Then i l not try kawai. Thanks for help :slightly_smiling:


Hi Rohit
20 minutes later I have got this :slight_smile: very visible kawai :slight_smile:
Good luck


ohh great :slight_smile: This looks good. Can u just send me snippet of the code you used.


Hi Rohit
here we go R in the evening
x <- matrix(runif(12000000),ncol=120)
G1 <- nng(x,k=10, mutual=T)
plot(G1, layout= layout.kamada.kawai,vertex.color=“green”) # I like Japanese !!!

And that is it
Have a good evening


Hi @Lesaffrea

Thanks for the code. :slightly_smiling:
But before running kawai we have to run nng() first. Then only we can run kawai. So nng() itself is taking more time… What to do then ?


Hi Rohit,

you are in the chicken an eggs situation, which computer do you have to take so long, the code I gave for the nng() part took 3 minutes, I shall have as many observations and and feature as you.
Are you one window with low memory?



Hi @Lesaffrea,

problem solved :slight_smile:

I took a sample of what i was running. It ran within 5mins and also gavae me the plot But when i m taking the whole dataset (60K train,20k test,128 var) it takes more than a hour. I guess nng() will not work for these these obs with 128 vars,…

Can u suggest me any other plotting technique whicch might handle this much data ?


HI Rohit
I managed nng() with 120 variables, ok they were simulation.
For plotting I check I received a book recently paper sorry I can not share with good example the author has a good web site as well short but give the codethis one, I used most of its methods and they work extremely well.
Do you want a kind of graph like ? Do you know the “R graphic Cookbook” from W. Chang? If yes look chapter 13.4 page 274, (he uses the library(igraph)). You can find this book in India for quite a good price.

Have fun



Thanks @Lesaffrea I Have that book. I will go through it… Thanks once again for the help :slightly_smiling:


Hi @Rohit_Nair

your question about graph gave me an idea for one problem I try to solve at the moment, I find this link and pdf I am sure it can help you have a look, they explain kawai layout pretty well. It complement R cookbook well.
About graph

Have fun and I shall send you some graph in coming days



hi @Lesaffrea

Thanks fot the link. Superb link. They have explained everything thing pretty well with R codes :smiley:
It will be fun to play aound with these techniques :wink:

Sure… When you get time send me :slight_smile: U can mail me also if u dont want to post images here …

Thanks alot :slight_smile: :slight_smile: :slight_smile:


Hi @Rohit_Nair
I played with this this week end I send in few days.


Hi @Rohit_Nair
to give view a first view, this is a network of routers with different states let say 1,2,3 each color is s state. One problem as soon as you put more than 1000 edge the graph is not visible. I have to work on this.