Total Pageviews

Wednesday 9 October 2013

Classification using neural net in r

This is mostly for my students and myself for future reference.

Classification is a supervised task , where we need preclassified data and then on new data , I can predict.
Generally we holdout a % from the data available for testing and we call them training and testing data respectively.  So it's like this , if we know which emails are spam , then only using classification we can predict the emails as spam.

I used the dataset http://archive.ics.uci.edu/ml/datasets/seeds# .  The data set has 7 real valued attributes and 1 for predicting .  http://www.jeffheaton.com/2013/06/basic-classification-in-r-neural-networks-and-support-vector-machines/ has influenced many of the writing , probably I am making it more obvious.

The library to be used is library(nnet) , below are the list of commands for your reference



1.       Read from dataset

seeds<-read.csv('seeds.csv',header=T)

2.       Setting training set index ,  210 is the dataset size, 147 is 70 % of that

   seedstrain<- sample(1:210,147)

3.       Setting test set index

   seedstest <- setdiff(1:210,seedstrain)
 
4.       Normalize the value to be predicted , use that attribute of the dataset , that you want to predict

   ideal <- class.ind(seeds$Class)

5.       Train the model, -8 because you want to leave out the class attribute , the dataset had a total of 8 attributes with the last one as the predicted one

   seedsANN = nnet(seeds[seedstrain,-8], ideal[seedstrain,], size=10, softmax=TRUE)

6.       Predict on training set

   predict(seedsANN, seeds[seedstrain,-8], type="class")

7.       Calculate Classification accuracy


   table(predict(seedsANN, seeds[seedstest,-8], type="class"),seeds[seedstest,]$Class)

Happy Coding !

8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This is something they were supposed to do long time ago. Listen, there are so many research papers available online for students, so why not taking advantage of those. Not only students can use them by the way. What I mean is that the majority researches have been done already; we just need to work with their results to build a very important data that public can get an access to.

    ReplyDelete
  3. replace "irisdata" with "seeds" and it should work better.

    ReplyDelete
  4. It would be very nice and helpful if you could extend your article with a paragraph about parameter tuning.

    ReplyDelete
  5. Writing essays is a very time-consuming and difficult task. By writing should not be taken very seriously, but did not get to relax. Not all people get to do this, especially, when the time to write a good essay not at all. I use gonerdify review. It has never let me down. Quickly, high quality, without plagiarism - the dream of every student.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Usually, I use a https://paidpaper.net/ for writing an essay ot reseach paper. I really like the quality of their writings and the price is also good! Useful article, thanks.

    ReplyDelete
  8. This is the most correct and right way, but it will not suit everyone. I can not work on something that I'm not interested in, so I order all the research here AcademicConsultants. This saves my time and gives me the opportunity to study more useful questions for me without harming my curriculum.

    ReplyDelete