<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/neural-networks-exercises-part-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/06/08/neural-networks-exercises-part-1/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-1"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div class="wp-caption"><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/300px-Colored_neural_network.svg_.png" target="_blank"><img sizes="(max-width: 249px) 100vw, 249px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/300px-Colored_neural_network.svg_-249x300.png?resize=249%2C300" alt="" class="wp-image-6305" data-recalc-dims="1"></a>
<p class="wp-caption-text">Source: <a rel="nofollow" href="https://en.wikipedia.org/wiki/File:Colored_neural_network.svg" target="_blank">Wikipedia</a></p>
</div>
<p>Neural network have become a corner stone of machine learning in the last decade. Created in the late 1940s with the intention to create computer programs who mimics the way neurons process information, those kinds of algorithm have long been believe to be only an academic curiosity, deprived of practical use since they require a lot of processing power and other machine learning algorithm outperform them. However since the mid 2000s, the creation of new neural network types and techniques, couple with the increase availability of fast computers made the neural network a powerful tool that every data analysts or programmer must know.</p>
<p>In this series of articles, we’ll see how to fit a neural network with R, we’ll learn the core concepts we need to know to well apply those algorithms and how to evaluate if our model is appropriate to use in production. This set of exercises is an introduction to neural networks where we’ll use them to create two simple regression and clustering model. Doing so, we’ll use a lot of basic concepts we’ll explore further in future sets. If you want more informations about neural network, your can <a rel="nofollow" href="https://www.tutorialspoint.com/artificial_intelligence/artificial_intelligence_neural_networks.htm" target="_blank"> see this page.</a></p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/06/08/neural-networks-solutions-part-1/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
We’ll start by creating the data set on which we want to do a simple regression. Set the seed to 42, generate 200 random points between -10 and 10 and store them in a vector named X. Then, create a vector named Y containing the value of <code>sin(x)</code>. Neural network are a lot more flexible than most regression algorithms and can fit complex function with ease. The biggest challenge is to find the appropriate network function appropriate to the situation.</p>
<p><strong>Exercise 2</strong><br>
A network function is made of three components: the network of neurons, the weight of each connection between neuron and the activation function of each neuron. For this example, we’ll use a feed-forward neural network and the logistic activation which are the defaults for the package <code>nnet</code>. We take one number as input of our neural network and we want one number as the output so the size of the input and output layer are both of one. For the hidden layer, we’ll start with three neurons. It’s good practice to randomize the initial weights, so create a vector of 10 random values, picked in the interval <code>[-1,1]</code>.</p>
<p><strong>Exercise 3</strong><br>
Neural networks have a strong tendency of overfitting your data, meaning they become really good at describing the relationship between the values in your data set, but are not effective with data that wasn’t used to train your model. As a consequence, we need to cross-validate our model. Set the seed to 42, then create a training set containing 75% of the values in your initial data set and a test set containing the rest of your data.</p>
<p><strong>Exercise 4</strong><br>
Load the <code>nnet</code> package and use the function of the same name to create your model. Pass your weights via the <code>Wts</code> argument and set the <code>maxit</code> argument to 50. We want to fit a function which can have for output multiple possible values. To do so, set the <code>linout</code> argument to <code>true</code>. Finally, take the time to look at the structure of your model.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/08/neural-networks-exercises-part-1/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-1"></aside>
<div><strong>Learn more</strong> about neural networks in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/machine-learning-a-z-hands-on-python-r-in-data-science/" target="_blank">Machine Learning A-Z™: Hands-On Python &amp; R In Data Science</a>. In this course you will learn how to:
<ul>
<li>Work with Deep Learning networks and related packagse in R</li>
<li>Create Natural Language Processing models</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 5</strong><br>
Predict the output for the test set and compute the RMSE of your predictions. Plot the function sin(x) and then plot your predictions.</p>
<p><strong>Exercise 6</strong><br>
The number of neurons in the hidden layer, as well as the number of hidden layer used, has a great influence on the effectiveness of your model. Repeat the exercises three to five, but this time use a hidden layer with seven neurons and initiate randomly 22 weights.</p>
<p><strong>Exercise 7</strong><br>
Now let us use neural networks to solve a clustering problems, so let’s load the <code>iris</code> data set! It is good practice to normalize your input data to uniformize the behavior of your model over different range of value and have a faster training. Normalize each factor so that they have a mean of zero and a standard deviation of 1, then create your train and test set.</p>
<p><strong>Exercise 8</strong><br>
Use the <code>nnet()</code> and use a hidden layer of ten neurons to create your model. We want to fit a function which have a finite amount of value as output. To do so, set the code>linout argument to <code>true</code>. Look at the structure of your model. With clustering problem, the output is usually a factor that is coded as multiple dummy variables, instead of a single numeric value. As a consequence, the output layer have as one less neuron than the number of levels of the output factor.</p>
<p><strong>Exercise 9</strong><br>
Make prediction with the values of the test set.</p>
<p><strong>Exercise 10</strong><br>
Create the confusion table of your prediction and compute the accuracy of the model.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/31/evaluate-your-model-with-r-exercises/" title="Evaluate your model with R Exercises" target="_blank">Evaluate your model with R Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/01/10/getting-started-with-plotly-basic-plots/" title="Getting started with Plotly: basic Plots" target="_blank">Getting started with Plotly: basic Plots </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/10/20/network-analysis-part-3-exercises/" title="Network Analysis Part 3 Exercises" target="_blank">Network Analysis Part 3 Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/06/08/neural-networks-exercises-part-1/?utm_source=rss&utm_medium=rss&utm_campaign=neural-networks-exercises-part-1"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...

	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/0DAmuLAE5dY" height="1"></a></div>