Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Neural Networks with R

You're reading from  Neural Networks with R

Product type Book
Published in Sep 2017
Publisher Packt
ISBN-13 9781788397872
Pages 270 pages
Edition 1st Edition
Languages
Authors (2):
Balaji Venkateswaran Balaji Venkateswaran
Profile icon Balaji Venkateswaran
Giuseppe Ciaburro Giuseppe Ciaburro
Profile icon Giuseppe Ciaburro
View More author details
Toc

Table of Contents (14) Chapters close

Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. Neural Network and Artificial Intelligence Concepts 2. Learning Process in Neural Networks 3. Deep Learning Using Multilayer Neural Networks 4. Perceptron Neural Network Modeling – Basic Models 5. Training and Visualizing a Neural Network in R 6. Recurrent and Convolutional Neural Networks 7. Use Cases of Neural Networks – Advanced Topics

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.

You have been reading a chapter from
Neural Networks with R
Published in: Sep 2017 Publisher: Packt ISBN-13: 9781788397872
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime