Book Image

Neural Networks with R

By : Balaji Venkateswaran, Giuseppe Ciaburro
Book Image

Neural Networks with R

By: Balaji Venkateswaran, Giuseppe Ciaburro

Overview of this book

Neural networks are one of the most fascinating machine learning models for solving complex computational problems efficiently. Neural networks are used to solve wide range of problems in different areas of AI and machine learning. This book explains the niche aspects of neural networking and provides you with foundation to get started with advanced topics. The book begins with neural network design using the neural net package, then you’ll build a solid foundation knowledge of how a neural network learns from data, and the principles behind it. This book covers various types of neural network including recurrent neural networks and convoluted neural networks. You will not only learn how to train neural networks, but will also explore generalization of these networks. Later we will delve into combining different neural network models and work with the real-world use cases. By the end of this book, you will learn to implement neural network models in your applications with the help of practical examples in the book.
Table of Contents (14 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Implementation using nnet() library


To improve our practice with the nnet library, we look at another example. This time we will use the data collected at a restaurant through customer interviews. The customers were asked to give a score to the following aspects: service, ambience, and food. They were also asked whether they would leave the tip on the basis of these scores. In this case, the number of inputs is 2 and the output is a categorical value (Tip=1 and No-tip=0).

The input file to be used is shown in the following table:

No

CustomerWillTip

Service

Ambience

Food

TipOrNo

1

1

4

4

5

Tip

2

1

6

4

4

Tip

3

1

5

2

4

Tip

4

1

6

5

5

Tip

5

1

6

3

4

Tip

6

1

3

4

5

Tip

7

1

5

5

5

Tip

8

1

5

4

4

Tip

9

1

7

6

4

Tip

10

1

7

6

4

Tip

11

1

6

7

2

Tip

12

1

5

6

4

Tip

13

1

7

3

3

Tip

14

1

5

1

4

Tip

15

1

7

5

5

Tip

16

0

3

1

3

No-tip

17

0

4

6

2

No-tip

18

0

2

5

2

No-tip

19

0

5

2

4

No-tip

20

0

4

1

3

No-tip

21

0

3

3

4

No-tip

22

0

3

4

5

No-tip

23

0

3

6

3

No-tip

24

0

4

4

2

No-tip

25

0

6

3

6

No-tip

26

0

3

6

3

No-tip

27

0

4

3

2

No-tip

28

0

3

5

2

No-tip

29

0

5

5

3

No-tip

30

0

1

3

2

No-tip

 

This is a classification problem with three inputs and one categorical output. We will address the problem with the following code:

######################################################################## 
##Chapter 1 - Introduction to Neural Networks - using R ################ 
###Simple R program to build, train and test neural networks ########### 
### Classification based on 3 inputs and 1 categorical output ########## 
######################################################################## 

###Choose the libraries to use
library(NeuralNetTools)
library(nnet)

###Set working directory for the training data
setwd("C:/R")
getwd()

###Read the input file
mydata=read.csv('RestaurantTips.csv',sep=",",header=TRUE)
mydata
attach(mydata)
names(mydata)

##Train the model based on output from input
model=nnet(CustomerWillTip~Service+Ambience+Food, 
           data=mydata, 
           size =5, 
           rang=0.1, 
           decay=5e-2, 
           maxit=5000)
print(model)
plotnet(model)
garson(model)

########################################################################

Let us go through the code line-by-line

To understand all the steps in the code just proposed, we will look at them in detail. First, the code snippet will be shown, and the explanation will follow.

library(NeuralNetTools)
library(nnet)

This includes the libraries NeuralNetTools and nnet() for our program.

###Set working directory for the training data
setwd("C:/R")
getwd()
###Read the input file
mydata=read.csv('RestaurantTips.csv',sep=",",header=TRUE)
mydata
attach(mydata)
names(mydata)

This sets the working directory and reads the input CSV file.

##Train the model based on output from input
model=nnet(CustomerWillTip~Service+Ambience+Food, 
 data=mydata, 
 size =5, 
 rang=0.1, 
 decay=5e-2, 
 maxit=5000)
print(model)

This calls the nnet() function with the arguments passed. The output is as follows. nnet() processes the forward and backpropagation until convergence:

> model=nnet(CustomerWillTip~Service+Ambience+Food,data=mydata, size =5, rang=0.1, decay=5e-2, maxit=5000)
# weights:  26
initial  value 7.571002 
iter  10 value 5.927044
iter  20 value 5.267425
iter  30 value 5.238099
iter  40 value 5.217199
iter  50 value 5.216688
final  value 5.216665 
converged

A brief description of the nnet package, extracted from the official documentation, is shown in the following table:

nnet-package: Feed-forward neural networks and multinomial log-linear models

Description:

Software for feed-forward neural networks with a single hidden layer, and for multinomial log-linear models.

Details:

Package: nnet Type: Package Version: 7.3-12 Date: 2016-02-02 License: GPL-2 | GPL-3

Author(s):

Brian RipleyWilliam Venables

Usage:

nnet(formula, data, weights,subset, na.action, contrasts = NULL)

Meaning of the arguments:

Formula: A formula of the form class ~ x1 + x2 + ...data: Dataframe from which variables specified in formula are preferentially to be takenweights: (Case) weights for each example; if missing, defaults to 1subset: An index vector specifying the cases to be used in the training samplena.action: A function to specify the action to be taken if NAs are foundcontrasts: A list of contrasts to be used for some or all of the factors appearing as variables in the model formula

 

After giving a brief glimpse into the package documentation, let's review the remaining lines of the proposed in the following code sample:

print(model)

This command prints the details of the net() as follows:

> print(model)
a 3-5-1 network with 26 weights
inputs: Service Ambience Food 
output(s): CustomerWillTip 
options were - decay=0.05

To plot the model, use the following command:

plotnet(model)

The plot of the model is as follows; there are five nodes in the single hidden layer:

Using NeuralNetTools, it's possible to obtain the relative importance of input variables in neural networks using garson algorithm:

garson(model)

This command prints the various input parameters and their importance to the output prediction, as shown in the following figure:

From the chart obtained from the application of the Garson algorithm, it is possible to note that, in the decision to give the tip, the service received by the customers has the greater influence.

We have seen two neural network libraries in R and used them in simple examples. We would deep dive with several practical use cases throughout this book.