# How to plot a graph using Knn algorithm?

#1
``````      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

#2

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

#3

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.

#4

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
Alain

#5

Thanks @Lesaffrea

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

#6

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 I think it help.
Decision Boundary
Now back to contour. This time I hope it helps.
Alain

#7

Look below Kawai is still running and I do not think it will help
Alain

#8

Decision boundary was secondary. First i need a plot.

Thanks for the link though. I will go through it

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

#9

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

#10

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

#11

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
Alain

#12

Thanks for the code.
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 ?

#13

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?

Alain

#14

Hi @Lesaffrea,

problem solved

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 ?

#15

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

Alain

#16

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

#17

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.

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

Alain

#18

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

Sure… When you get time send me U can mail me also if u dont want to post images here … rohitn220@gmail.com

Thanks alot

#19

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

#20

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.

Alain