Building a Neural Network Using the Iris Data Set: Exercises

Neural Networks is one of the most common machine learning algorithms and with good reason. Neural networks are particularly good when applied to problems, such as image recognition and natural language processing, where there is a large amount of input data.

Through an input layer, one or more hidden layers, and an output layer, a neural network works by connecting up a series of neurons with weights assigned to each connection. As each connection is activated, a calculation is performed on the connection before passing through an activation function at each level of the hidden layers. Commonly, these activation functions are either the RELU, sigmoid or tanh. Their purpose is usually to determine whether the next layer should be activated.

Within this tutorial, we’re going to develop a very simple classification neural network on the commonly used iris dataset. Before starting, you should install the neuralnet, ggplot2, dplyr, and reshape2 libraries. Solutions to these exercises are available here.
Exercise 1
Load the neuralnet, ggplot2, and dplyr libraries, along with the iris dataset. Set the seed to 123.

Exercise 2
Explore the distributions of each feature present in the iris dataset. Feel free to get creative here. I myself opted for a violin plot.

Exercise 3
Convert your observation class and Species into one hot vector.

Exercise 4
Write a generic function to standardize a column of data.

Exercise 5
Now standardize the predictors. Hint: lapply will be useful here.

Exercise 6
Combine your one hot labels and standardized predictors.

Learn more about neural networks in the online course Machine Learning A-Z™: Hands-On Python & R In Data Science. In this course you will learn how to:

  • Work with Deep Learning networks and related packages in R
  • Create Natural Language Processing models
  • And much more

Exercise 7
Define your formula that your neuralnet will be run on. You’ll need to use the as.formula function here.

Exercise 8
Create a neural network object, now using the tanh function and two hidden layers of size 16 and 12. You will also need to tell the neural network that you’re performing a classification algorithm here, not regression. You’ll want to refer to the neuralnet documentation as to how to define this.

Exercise 9
Plot your neural network.

Exercise 10
Using the compute function and your neural network object’s net.result attribute, calculate the overall accuracy of your neural network.