<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/pca-revisited-using-principal-components-for-classification-of-faces/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://firsttimeprogrammer.blogspot.com/2018/08/pca-revisited-using-principal.html"> The Beginner Programmer</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is a short post following the previous one (<a rel="nofollow" href="https://firsttimeprogrammer.blogspot.com/2018/08/pca-revisited.html" target="_blank">PCA revisited</a>).</p>
<p>In this post I’m going to apply PCA to a toy problem: the classification of faces. Again I’ll be working on the Olivetti faces dataset. Please visit the previous post <a rel="nofollow" href="https://firsttimeprogrammer.blogspot.com/2018/08/pca-revisited.html" target="_blank">PCA revisited</a> to read how to download it.</p>
<p>The goal of this post is to fit a simple classification model to predict, given an image, the label to which it belongs. I’m goint to fit two support vector machine models and then compare their accuracy.</p>
<p>The</p>
<ol>
<li>The first model uses as input data the raw (scaled) pixels (all 4096 of them). Let’s call this model “the data model”.</li>
<li>The second model uses as input data only some principal components. Let’s call this model “the PCA model”.</li>
</ol>
<p></p>
<p><a name="more"></a></p>
<p>This will not be an in-depth and detailed fit and subsequent evaluation of the model, but rather a simple proof of concept to prove that a limited number of principal components can be used to perform a classification task instead of using the actual data.</p>
<h2>The setup of the two models</h2>
<p>In order to get an idea of what accuracy our models can achieve, I decided to do a simple 20 fold crossvalidation  of each model.</p>
<p>The data model is a simple support vector machine that is fitted using all the faces in the training set: it’s input data is a vector of 4096 components.</p>
<p>The PCA model is again the same support vector machine (with the same hyperparameters, which however may need some tweaking) fitted using 30 PCs.</p>
<p>By performing PCA on the dataset I transformed the data and, according to the analysis, 30 PCs account for about 82% of the total variance in the dataset. Indeed you can see from the plots below that the first 30 eigenvalues are those with the highest magnitude.</p>
<p><a rel="nofollow" href="https://lh3.googleusercontent.com/-qHv6tmhzfuU/W3MqKmZX0mI/AAAAAAAACDk/yvoe662b3ngkTX5KASoVrLlf-H76RTKPACHMYCw/s1600-h/Rplot01%255B3%255D" target="_blank"><img border="0" src="https://lh3.googleusercontent.com/-2AlA71rr2MI/W3MqLQ8qDLI/AAAAAAAACDo/tKpWtxymDIcDHNgfvt_J6IZOeuP8_uNVQCHMYCw/Rplot01_thumb%255B1%255D?imgmax=800" width="337" alt="Rplot01" title="Rplot01" height="263"></a> </p>
<p><a rel="nofollow" href="https://lh3.googleusercontent.com/-HSW155BaYvo/W3MqL3O4UFI/AAAAAAAACDs/jS97IN0NpRYgpdl5Rpo4hQ5_L7FTszWuACHMYCw/s1600-h/Rplot%255B4%255D" target="_blank"><img border="0" src="https://lh3.googleusercontent.com/-ltq5aYsOIlk/W3MqMlQIPzI/AAAAAAAACDw/40U5069xx781uAXbN6KbVr8C7oxJ-Q9qQCHMYCw/Rplot_thumb%255B2%255D?imgmax=800" width="335" alt="Rplot" title="Rplot" height="274"></a></p>
<h2>Results</h2>
<p>After running  the two crossvalidations on the two different models, the main results I found are the following:</p>
<ol>
<li>The average accuracy of the two models is about the same (PCA model has a slightly better average accuracy): 93% vs 94%. Standard deviation seems to be again in favour of the PCA model: 3.2% vs 2.3%. The accuracy isn’t great, I obtained 98% on this dataset using other models, but of course this is just a test “out of the box” with no hyperparameter tuning and all the other fine tuning operations.</li>
<li>The second model fits the data much faster than the first one.</li>
</ol>
<p>The results are summarised in the boxplot below</p>
<p><a rel="nofollow" href="https://lh3.googleusercontent.com/-MJ3qbp7Fan4/W3MqNCkzTxI/AAAAAAAACD0/wx1BM7j71SwZTf1y3qJFp0utMCOmXegKwCHMYCw/s1600-h/boxplot%255B4%255D" target="_blank"><img border="0" src="https://lh3.googleusercontent.com/-mtdFjX2dsNY/W3MqNtVYo_I/AAAAAAAACD4/0vjzwpGaqNgGFEAAWt2owYhReFKD0wqMQCHMYCw/boxplot_thumb%255B2%255D?imgmax=800" width="369" alt="boxplot" title="boxplot" height="296"></a></p>
<p>Note that the PCA model has less information available compared to the model using the original data. However, the information is condensed in a fewer variables (the 30 PCs) in a more compact way. This technique is great when you are using models sensitive to correlation in the data: since the PCs are not correlated, you can transform your original data (which might be correlated and causing problems in the model) and use the principal components instead.</p>
<p>Another nice advantage is that since the PCA model uses fewer variables it is faster. Note that I decided to apply PCA to the whole dataset and not only to the training set of each round of crossvalidation. It could have been argued that in practice you have access to only the training set (of course). Then I used the eigenvectors to obtain the PCs for both the training and testing set of each crossvalidation iteration. </p>
<p>The code I used is here below</p>
<p></p>
	<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://firsttimeprogrammer.blogspot.com/2018/08/pca-revisited-using-principal.html"> The Beginner Programmer</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/r_PaPWCUbMg" height="1"></a></div>