Deep Learning in R Programming - GeeksforGeeks

20 Aug, 2020
Deep Learning is a type of Artificial Intelligence or AI function that tries to imitate or mimic the working principle of a human brain for data processing and pattern creation for decision-making purposes. It is a subset of ML or machine learning in an AI that owns or have networks that are capable of unsupervised learning from data that are unlabeled or unstructured. Deep learning can also be called as deep neural learning or deep neural network. Deep Learning AI is capable of learning without human supervision, drawing from any kind of data. Being a subset of machine learning, deep learning utilizes an artificial neural network of a hierarchical level in order to carry out the machine learning procedures or processes. With the help of deep learning, we can unravel a huge amount of data that is unstructured in nature. For humans, that would take decades normally to understand and process it. 
R Language has been decorated with many deep learning packages in CRAN in the due course of time. Some of these packages are as follows :
R Package Name
Description
Used for feed-forward NN having a single hidden layer 
or for a multinomial log-linear model. 
It is a package for the Deep Architectures and 
Restricted Boltzmann Machines.
Based on darch and deepnet, it is a package to 
enhance the streamline the training, predicting process
and fine-tuning for deep learning.
Recently Keras, kerasR, and keras are also used for deep learning purposes. Here we will be using the deepnet package for implementing deep learning. Let’s proceed with the step-by-step procedure of the implementation. The entire implementation process can be divided into the following steps:
Step 1: Installing and Loading the Packages 
Before proceeding with the implementations, install the required packages. For our implementation, we will require the deepnet and mlbench packages. To install these packages from the R Console use the install.packages() command. On successful installation of these packages, load them in the R Script using the library() command as follows:
R
# Deep Learning in R# loading the required packageslibrary(mlbench)library(deepnet)
Step 2: Choosing Dataset  
Now the task is to select a proper dataset for the implementation. Here let’s work with the Breast Cancer Dataset under the mlbench package. Include the data set in the R Script as follows:
R
# Deep learning in R# loading the required packageslibrary(mlbench)library(deepnet)data("BreastCancer")  # Clean off rows with missing dataBreastCancer = BreastCancer[which(complete.cases(BreastCancer)                                  == TRUE),]head(BreastCancer)names(BreastCancer)
Output:
> head(BreastCancer)
       Id Cl.thickness Cell.size Cell.shape Marg.adhesion Epith.c.size Bare.nuclei Bl.cromatin Normal.nucleoli Mitoses     Class
1 1000025            5         1          1             1            2           1           3               1       1    benign
2 1002945            5         4          4             5            7          10           3               2       1    benign
3 1015425            3         1          1             1            2           2           3               1       1    benign
4 1016277            6         8          8             1            3           4           3               7       1    benign
5 1017023            4         1          1             3            2           1           3               1       1    benign
6 1017122            8        10         10             8            7          10           9               7       1 malignant
> names(BreastCancer)
 [1] "Id"              "Cl.thickness"    "Cell.size"       "Cell.shape"      "Marg.adhesion"   "Epith.c.size"    "Bare.nuclei"    
 [8] "Bl.cromatin"     "Normal.nucleoli" "Mitoses"         "Class" 

Step 3: Applying the deepnet package to the dataset
Apply the deep learning package on the chosen dataset. Here, create a set of features for independent variables, and create the dependent variable.
R
# Deep learning in R# Loading the required packageslibrary(mlbench)library(deepnet)data("BreastCancer")  # Clean off rows with missing dataBreastCancer = BreastCancer[which(complete.cases(BreastCancer)                                  == TRUE),]  head(BreastCancer)names(BreastCancer)y = as.matrix(BreastCancer[, 11])y[which(y == "benign")] = 0y[which(y == "malignant")] = 1y = as.numeric(y)x = as.numeric(as.matrix(BreastCancer[, 2:10]))x = matrix(as.numeric(x), ncol = 9)
Step 4: Modeling NN
Apply nn.train() function under the deepnet package in order to model the neural network.
R
# Deep learning in R# Loading packages in Rlibrary(mlbench)library(deepnet)data("BreastCancer")  # Clean off rows with missing dataBreastCancer = BreastCancer[which(complete.cases(BreastCancer)                                  == TRUE),]  head(BreastCancer)names(BreastCancer)y = as.matrix(BreastCancer[, 11])y[which(y == "benign")] = 0y[which(y == "malignant")] = 1y = as.numeric(y)x = as.numeric(as.matrix(BreastCancer[, 2:10]))x = matrix(as.numeric(x), ncol = 9)  # Applying nn.train() functionnn <- nn.train(x, y, hidden = c(5))yy = nn.predict(nn, x)print(head(yy))
Output:
> print(head(yy))
          [,1]
[1,] 0.2743838
[2,] 0.4130857
[3,] 0.2892783
[4,] 0.4232022
[5,] 0.2817078
[6,] 0.4526502

Step 5: Creating a confusion matrix
Retrieve the neural network’s output and then convert it into class. In order to create a confusion matrix, use the table() function. Also check the accuracy of the confusion matrix by dividing the sum of the diagonal elements with the total count or sum of all the numbers.
R
# Deep Learning in R# Loading required packageslibrary(mlbench)library(deepnet)data("BreastCancer")  # Clean off rows with missing dataBreastCancer = BreastCancer[which(complete.cases(BreastCancer)                                  == TRUE),]  head(BreastCancer)names(BreastCancer)y = as.matrix(BreastCancer[, 11])y[which(y == "benign")] = 0y[which(y == "malignant")] = 1y = as.numeric(y)x = as.numeric(as.matrix(BreastCancer[, 2:10]))x = matrix(as.numeric(x), ncol = 9)  # Applying nn.train() methodnn <- nn.train(x, y, hidden = c(5))yy = nn.predict(nn, x)print(head(yy))yhat = matrix(0,length(yy), 1)yhat[which(yy > mean(yy))] = 1yhat[which(yy <= mean(yy))] = 0  # Applying table() functioncm = table(y, yhat)print(cm)print(sum(diag(cm))/sum(cm))
Output:
> print(cm)
   yhat
y     0   1
  0 425  19
  1   4 235
> print(sum(diag(cm))/sum(cm))
[1] 0.966325

Deep-Learning
R Data-science
R Machine-Learning
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change column name of a given DataFrame in R
How to Replace specific values in column in R DataFrame ?
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Adding elements in a vector in R programming - append() method
How to change Row Names of DataFrame in R ?
Change Color of Bars in Barchart using ggplot2 in R
Convert Factor to Numeric and Numeric to Factor in R Programming
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?