{
  "id": 12336,
  "title": "Really low accuracy using R, caret, and nnet",
  "url": "/competitions/digit-recognizer/discussion/12336",
  "author_name": "blackfist",
  "post_date": "2015-01-26T16:30:12.347000",
  "votes": 0,
  "comment_count": 2,
  "views": null,
  "content": "<p>I have gone through the data using KNN and came out with an accuracy of around 96%. I decided to try experimenting with neural networks because I saw in one of the threads someone said that those are typically better for handwriting analysis. However, my resulting code is only about 36% accurate. I was wondering if someone could&nbsp;identify what I might be doing wrong.</p>\n<p>library(&quot;caret&quot;)<br>library(randomForest)</p>\n<p>library(&quot;doMC&quot;)</p>\n<p># I want to leave some cores for other things.<br>doMC::registerDoMC(cores=detectCores() -3 )</p>\n<p>inputData &lt;- read.csv(&quot;train.csv&quot;)<br>testData &lt;- read.csv(&quot;test.csv&quot;)</p>\n<p># Cleaning and tweaking<br>inputData$label &lt;- as.factor(inputData$label)</p>\n<p># Partition the training data<br>randomSelect &lt;- createDataPartition(inputData$label, p=0.7, list=F)<br>modelTrain &lt;- inputData[randomSelect, ]<br>modelTest &lt;- inputData[-randomSelect, ]</p>\n<p># Consider removing columns with near zero variance<br>nzv &lt;- nearZeroVar(modelTrain)<br>modelTrain &lt;- modelTrain[,-nzv]</p>\n<p>if(!file.exists(&quot;nnModel.Rda&quot;)) {<br> nnStart &lt;- Sys.time()<br> nnModel &lt;- train(label ~ ., data=modelTrain, method=&quot;nnet&quot;)<br> nnEnd &lt;- Sys.time()<br> save(nnModel, file=&quot;nnModel.Rda&quot;)<br>}</p>\n<p>nnPredictions &lt;- predict(nnModel, modelTest)</p>\n<p>confusionMatrix(modelTest$label, nnPredictions)$overall</p>\n<p><br> Accuracy Kappa AccuracyLower AccuracyUpper AccuracyNull AccuracyPValue McnemarPValue <br> 0.3655129 0.2913060 0.3570951 0.3739928 0.4526040 1.0000000 NaN <br><br></p>",
  "messages": [
    {
      "id": 110620,
      "postDate": "2016-03-07T02:08:01.477Z",
      "content": "<p>Try to normalize the data before you train the nerual network.</p>",
      "rawMarkdown": "Try to normalize the data before you train the nerual network."
    },
    {
      "id": 63224,
      "postDate": "2015-01-27T03:48:27.423Z",
      "content": "<p>I don't know the package you used. From what I know, there are two common neural network packages in R: neuralnet and nnet.&nbsp;</p>\n<p>I used nnet and got around 98%. I recommend&nbsp;you understand how neural net works before applying them.&nbsp;</p>\n<p>I wrote a blog about recourses I find. You can take it look <a href=\"http://qiweili.com/2015/01/25/learning-artificial-neural-network/\">here</a>.</p>"
    },
    {
      "id": 63192,
      "postDate": "2015-01-26T16:30:12.347Z",
      "content": "<p>I have gone through the data using KNN and came out with an accuracy of around 96%. I decided to try experimenting with neural networks because I saw in one of the threads someone said that those are typically better for handwriting analysis. However, my resulting code is only about 36% accurate. I was wondering if someone could&nbsp;identify what I might be doing wrong.</p>\n<p>library(&quot;caret&quot;)<br>library(randomForest)</p>\n<p>library(&quot;doMC&quot;)</p>\n<p># I want to leave some cores for other things.<br>doMC::registerDoMC(cores=detectCores() -3 )</p>\n<p>inputData &lt;- read.csv(&quot;train.csv&quot;)<br>testData &lt;- read.csv(&quot;test.csv&quot;)</p>\n<p># Cleaning and tweaking<br>inputData$label &lt;- as.factor(inputData$label)</p>\n<p># Partition the training data<br>randomSelect &lt;- createDataPartition(inputData$label, p=0.7, list=F)<br>modelTrain &lt;- inputData[randomSelect, ]<br>modelTest &lt;- inputData[-randomSelect, ]</p>\n<p># Consider removing columns with near zero variance<br>nzv &lt;- nearZeroVar(modelTrain)<br>modelTrain &lt;- modelTrain[,-nzv]</p>\n<p>if(!file.exists(&quot;nnModel.Rda&quot;)) {<br> nnStart &lt;- Sys.time()<br> nnModel &lt;- train(label ~ ., data=modelTrain, method=&quot;nnet&quot;)<br> nnEnd &lt;- Sys.time()<br> save(nnModel, file=&quot;nnModel.Rda&quot;)<br>}</p>\n<p>nnPredictions &lt;- predict(nnModel, modelTest)</p>\n<p>confusionMatrix(modelTest$label, nnPredictions)$overall</p>\n<p><br> Accuracy Kappa AccuracyLower AccuracyUpper AccuracyNull AccuracyPValue McnemarPValue <br> 0.3655129 0.2913060 0.3570951 0.3739928 0.4526040 1.0000000 NaN <br><br></p>"
    }
  ],
  "comments": [
    {
      "id": 110620,
      "author_name": "Fangyu Ding",
      "author_url": "",
      "post_date": "2016-03-07T02:08:01.477000",
      "content": "<p>Try to normalize the data before you train the nerual network.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 63224,
      "author_name": "",
      "author_url": "",
      "post_date": "2015-01-27T03:48:27.423000",
      "content": "<p>I don't know the package you used. From what I know, there are two common neural network packages in R: neuralnet and nnet.&nbsp;</p>\n<p>I used nnet and got around 98%. I recommend&nbsp;you understand how neural net works before applying them.&nbsp;</p>\n<p>I wrote a blog about recourses I find. You can take it look <a href=\"http://qiweili.com/2015/01/25/learning-artificial-neural-network/\">here</a>.</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "110620": "Try to normalize the data before you train the nerual network.",
    "63224": "",
    "63192": ""
  }
}