Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Hand-Written Digit Recognition in R using Neuralnet

It is the high time now that I slide my feet in the vast ocean of Neural Network. I was avoiding it for some time.At the first place, I wanted to use Neural Network for the Hand Written Digit Recognition problem. Instead, I tried the below:
1. Decision Tree
2. Random Forest
3. Multinomial Logistic Regression
In this post, I will implement a simple  Neural Network to recognize the handwritten digits. I will use the same dataset from the Neural Network Exercise of the Andrew Ng Course of Machine Learning. I converted the .mat file to a .csv file. The steps are mentioned in another post here. The inputs are pixel values of digit images. Since the images are of size 20×20, this gives us 400 input layer.
The first few steps are same as in my last few posts and thus copy pasted from before. 
So, Let's divide the whole process into the below steps:
1. Load Data
2. Divide Data into Training and Test Set
3. Exploratory Data Analysis
4. Create a Model
5. Prediction

1. Load Data:

I have used the Dataset from "machine-learning-ex4" from Andrew Ng 's Machine Learning Course. I converted the .mat files to a .csv files. Let's load the data first and take a look.

Now, let's see what we are doing in the above piece of code.

  • Load 2 data files: We have 2 data files. The first data file has 5000 training examples, where each training example is a 20 * 20 (400) pixel grayscale image of a digit. Thus, each pixel is represented by a floating point number indicating the grayscale intensity at that location. The 20 by 20 grid of pixels is “unrolled” into a 400-dimensional vector. The second data file is a 5000-dimensional vector y that contains labels for the training set.  
  • Merge the 2 Datafiles: I have loaded both the datasets and merged them into a single file. Let's check the dimension of each file.
  • Rename the "output" column: The output file column name is "V1". Also, the pixel value dataset has a column named "V1". So, I renamed the output file column to "Output" to avoid any confusion.
  • Create the Output Matrix: This step is new to this particular Neural Network Model. I am adding a 10 new columns (For number 1 to 10) and named them as "out.1", "out.2", "out.3" etc. and putting "1" for the value in the "output" column and "0" for the rest. i.e. If the "output" column has a value "5", we will have a "1" in "out.5" and "0" in  "out.1","out.2" , "out.3" , "out.4", "out.6" , "out.7",  "out.8",  "out.9"  and "out.10". Now, if we check the dimension of the "digits" data frame, it has 411 columns.
    If we check the column names, below is the list of columns:
  [1] "output" "V1"     "V2"     "V3"     "V4"     "V5"     "V6"    
  [8] "V7"     "V8"     "V9"     "V10"    "V11"    "V12"    "V13"   
 [15] "V14"    "V15"    "V16"    "V17"    "V18"    "V19"    "V20"   
 [22] "V21"    "V22"    "V23"    "V24"    "V25"    "V26"    "V27"   
 [29] "V28"    "V29"    "V30"    "V31"    "V32"    "V33"    "V34"   
 [36] "V35"    "V36"    "V37"    "V38"    "V39"    "V40"    "V41"   
 [43] "V42"    "V43"    "V44"    "V45"    "V46"    "V47"    "V48"   
 [50] "V49"    "V50"    "V51"    "V52"    "V53"    "V54"    "V55"   
 [57] "V56"    "V57"    "V58"    "V59"    "V60"    "V61"    "V62"   
 [64] "V63"    "V64"    "V65"    "V66"    "V67"    "V68"    "V69"   
 [71] "V70"    "V71"    "V72"    "V73"    "V74"    "V75"    "V76"   
 [78] "V77"    "V78"    "V79"    "V80"    "V81"    "V82"    "V83"   
 [85] "V84"    "V85"    "V86"    "V87"    "V88"    "V89"    "V90"   
 [92] "V91"    "V92"    "V93"    "V94"    "V95"    "V96"    "V97"   
 [99] "V98"    "V99"    "V100"   "V101"   "V102"   "V103"   "V104"  
[106] "V105"   "V106"   "V107"   "V108"   "V109"   "V110"   "V111"  
[113] "V112"   "V113"   "V114"   "V115"   "V116"   "V117"   "V118"  
[120] "V119"   "V120"   "V121"   "V122"   "V123"   "V124"   "V125"  
[127] "V126"   "V127"   "V128"   "V129"   "V130"   "V131"   "V132"  
[134] "V133"   "V134"   "V135"   "V136"   "V137"   "V138"   "V139"  
[141] "V140"   "V141"   "V142"   "V143"   "V144"   "V145"   "V146"  
[148] "V147"   "V148"   "V149"   "V150"   "V151"   "V152"   "V153"  
[155] "V154"   "V155"   "V156"   "V157"   "V158"   "V159"   "V160"  
[162] "V161"   "V162"   "V163"   "V164"   "V165"   "V166"   "V167"  
[169] "V168"   "V169"   "V170"   "V171"   "V172"   "V173"   "V174"  
[176] "V175"   "V176"   "V177"   "V178"   "V179"   "V180"   "V181"  
[183] "V182"   "V183"   "V184"   "V185"   "V186"   "V187"   "V188"  
[190] "V189"   "V190"   "V191"   "V192"   "V193"   "V194"   "V195"  
[197] "V196"   "V197"   "V198"   "V199"   "V200"   "V201"   "V202"  
[204] "V203"   "V204"   "V205"   "V206"   "V207"   "V208"   "V209"  
[211] "V210"   "V211"   "V212"   "V213"   "V214"   "V215"   "V216"  
[218] "V217"   "V218"   "V219"   "V220"   "V221"   "V222"   "V223"  
[225] "V224"   "V225"   "V226"   "V227"   "V228"   "V229"   "V230"  
[232] "V231"   "V232"   "V233"   "V234"   "V235"   "V236"   "V237"  
[239] "V238"   "V239"   "V240"   "V241"   "V242"   "V243"   "V244"  
[246] "V245"   "V246"   "V247"   "V248"   "V249"   "V250"   "V251"  
[253] "V252"   "V253"   "V254"   "V255"   "V256"   "V257"   "V258"  
[260] "V259"   "V260"   "V261"   "V262"   "V263"   "V264"   "V265"  
[267] "V266"   "V267"   "V268"   "V269"   "V270"   "V271"   "V272"  
[274] "V273"   "V274"   "V275"   "V276"   "V277"   "V278"   "V279"  
[281] "V280"   "V281"   "V282"   "V283"   "V284"   "V285"   "V286"  
[288] "V287"   "V288"   "V289"   "V290"   "V291"   "V292"   "V293"  
[295] "V294"   "V295"   "V296"   "V297"   "V298"   "V299"   "V300"  
[302] "V301"   "V302"   "V303"   "V304"   "V305"   "V306"   "V307"  
[309] "V308"   "V309"   "V310"   "V311"   "V312"   "V313"   "V314"  
[316] "V315"   "V316"   "V317"   "V318"   "V319"   "V320"   "V321"  
[323] "V322"   "V323"   "V324"   "V325"   "V326"   "V327"   "V328"  
[330] "V329"   "V330"   "V331"   "V332"   "V333"   "V334"   "V335"  
[337] "V336"   "V337"   "V338"   "V339"   "V340"   "V341"   "V342"  
[344] "V343"   "V344"   "V345"   "V346"   "V347"   "V348"   "V349"  
[351] "V350"   "V351"   "V352"   "V353"   "V354"   "V355"   "V356"  
[358] "V357"   "V358"   "V359"   "V360"   "V361"   "V362"   "V363"  
[365] "V364"   "V365"   "V366"   "V367"   "V368"   "V369"   "V370"  
[372] "V371"   "V372"   "V373"   "V374"   "V375"   "V376"   "V377"  
[379] "V378"   "V379"   "V380"   "V381"   "V382"   "V383"   "V384"  
[386] "V385"   "V386"   "V387"   "V388"   "V389"   "V390"   "V391"  
[393] "V392"   "V393"   "V394"   "V395"   "V396"   "V397"   "V398"  
[400] "V399"   "V400"   "out.1"  "out.2"  "out.3"  "out.4"  "out.5" 
[407] "out.6"  "out.7"  "out.8"  "out.9"  "out.10"

2. Divide Data into Training and Test Set:
At this step, we will divide the dataset into Training and Test set. We will use 70% of the data for training our Model and the other 30% to test the accuracy of our Model.

3. Exploratory Data Analysis:
This is the fun and colorful part. Let's plot!
It's time to find out how our Training Set looks like.

Hmm, all the digits are almost equally distributed in the Training Dataset. Now, we will see how the handwritten Digits in our training set looks like. We have created a function for the same and passed a few records randomly and below is the output.


4. Create a Model: We will use Neuralnet for this classification problem. Let's build a Model!

Now, let's understand the Model First.
Arguments
Meaning
formula
a symbolic description of the model to be fitted.
hidden
integer specifying the number of hidden neurons (vertices) in each layer.
data
a data frame containing the variables specified in the formula.
linear.output
logical. specify whether we want to do regression i.e. linear.output=TRUE or classification i.e. linear.output=FALSE
algorithm
a string containing the algorithm type to calculate the neural network. 'rprop+' refers to the resilient backpropagation with weight backtracking,
learningrate
a numeric value specifying the learning rate used by traditional backpropagation.
rep
the number of repetitions for the neural network's training.

For some reason, neuralnet does not like the formula y~.. That's why we have to build a formula where output variables are on the left-hand side of ~ and the predictor variables are on the right-hand side. Also, the Model will take some time to build for the first time.
The plot with 10 hidden Units was taking some time to load and also became quite extensive. So, I changed the Hidden =1 and built the model again and plot the results.
5. Prediction:
Let's see how the Neural Network Model performs on the test data.

It looks like the prediction is almost 85% accurate which is not bad!  Now, finally let's take a look at the confusion Matrix.
The Neural Network Model performed well. But for me, Random Forest is still the winner!


This post first appeared on What The Data Says, please read the originial post: here

Share the post

Hand-Written Digit Recognition in R using Neuralnet

×

Subscribe to What The Data Says

Get updates delivered right to your inbox!

Thank you for your subscription

×