A Beginner’s Guide to Neural Networks with R!
In this article we will learn how Neural Networks work and how to implement them with the R programming language! We will see how we can easily create Neural Networks with R and even visualize them. Basic understanding of R is necessary to understand this article.
Train and Test Split
Let us now split our data into a training set and a test set. We will run our neural network on the training set and then see how well it performed on the test set.
We will use the caTools to randomly split the data into a training set and test set.
Neural Network Function
Before we actually call the neuralnetwork() function we need to create a formula to insert into the machine learning model. The neuralnetwork() function won't accept the typical decimal R format for a formula involving all features (e.g. y ~.). However, we can use a simple script to create the expanded formula and save us some typing:
Predictions and Evaluations
Now let's see how well we performed! We use the compute() function with the test data (jsut the features) to create predicted values. This returns a list from which we can call net.result off of.
Notice we still have results between 0 and 1 that are more like probabilities of belonging to each class. We'll use sapply() to round these off to either 0 or 1 class so we can evaluate them against the test labels.
Now let's create a simple confusion matrix:
Visualizing the Neural Net
We can visualize the Neural Network by using the plot(nn) command. The black lines represent the weighted vectors between the neurons. The blue line represents the bias added. Unfortunately, even though the model is clearly a very powerful predictor, it is not easy to directly interpret the weights. This means that we usually have to treat Neural Network models more like black boxes.
Hopefully you've enjoyed this brief discussion on Neural Networks! Try playing around with the number of hidden layers and neurons and see how they effect the results!
Want to learn more? You can check out my Data Science and Machine Learning Bootcamp with R course on Udemy! Get it for 50% off at this link:
If you are looking for corporate in-person training, feel free to contact me at: training AT pieriandata.com
Bio: Jose Portilla is a Data Science consultant and trainer who currently teaches online courses on Udemy. He also conducts training as the Head of Data Science for Pierian Data Inc.
Editor: Thanks to Cesar Cordova for finding an error in the original version of this post where "data" was used in place of "train" and thanks for Jose Portilla for quickly fixing it.
Top Stories Past 30 Days