<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/machine-learning-explained-dimensionality-reduction/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://enhancedatascience.com/2017/07/31/machine-learning-explained-dimensionality-reduction/"> Enhance Data Science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Dealing with a lot of dimensions can be painful for machine learning algorithms. High dimensionality will increase the computational complexity, increase the risk of overfitting (as your algorithm has more degrees of freedom) and the sparsity of the data will grow. Hence, dimensionality reduction will project the data in a space with less dimension to limit these phenomena.<br>
In this post, we will first try to get an intuition of what is dimensionality reduction, then we will focus on the most widely used techniques.</p>
<h2>Spaces and variables with many dimensions</h2>
<p>Let’s say we own a shop and want to collect some data on our clients. We can collect their ages, how frequently they come to our shop, how much they spend on average and when was the last time they came to our shop. Hence each of our clients can be represented by four variables (ages, frequency, spendings and last purchase date) and can be seen as a point in a four dimension space. Later on, variables and dimensions will have the same meaning.</p>
<p>Now, let’s add some complexity and some variables by using images. How many dimensions are used to represent the image below?</p>
<p><img data-orig-file="https://i1.wp.com/enhancedatascience.com/wp-content/uploads/2017/05/0_center.png?fit=362%2C256" data-orig-size="362,256" data-medium-file="https://i1.wp.com/enhancedatascience.com/wp-content/uploads/2017/05/0_center.png?fit=300%2C212" data-attachment-id="1882" data-image-meta="{" data-image-description="" src="https://i1.wp.com/enhancedatascience.com/wp-content/uploads/2017/05/0_center.png?resize=238%2C168" data-permalink="http://enhancedatascience.com/2017/05/06/python-basics-kmeans-with-python/0_center/" data-large-file="https://i1.wp.com/enhancedatascience.com/wp-content/uploads/2017/05/0_center.png?fit=362%2C256" alt="" data-image-title="0_center" sizes="(max-width: 238px) 100vw, 238px" data-comments-opened="1" class="wp-image-1882" data-recalc-dims="1"></p>
<p>There are 8 by 8 pixels, hence 64 pixels are needed and each of these pixels is represented by a quantitative variable. 64 variables are needed!<br>
Even bigger, how many variables do you need to represent the picture below?</p>
<p><img data-orig-file="https://i2.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ExempleImage.jpg?fit=640%2C426" data-orig-size="640,426" data-medium-file="https://i2.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ExempleImage.jpg?fit=300%2C200" data-attachment-id="2144" data-image-meta="{" data-image-description="" src="https://i2.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ExempleImage.jpg?resize=530%2C353" data-permalink="http://enhancedatascience.com/2017/07/31/machine-learning-explained-dimensionality-reduction/exempleimage/" data-large-file="https://i2.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ExempleImage.jpg?fit=640%2C426" alt="ExempleImageDimension" data-image-title="ExempleImageDimension" sizes="(max-width: 530px) 100vw, 530px" data-comments-opened="1" class="wp-image-2144" data-recalc-dims="1"></p>
<p>Its resolution is 640 by 426 and you need three color channels (Red, Green, and Blue). So this picture requires no more than … 640*426*3= 817,920 variables. That’s how you go from four to almost one million of dimensions (and you can go well above!)</p>
<h2>Dimensionality reduction in everyday life</h2>
<p>Before seeing any algorithm, everyday life provides us a great example of dimensionality reduction.</p>
<p><img data-orig-file="https://i0.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ShadowProjection.jpg?fit=640%2C423" data-orig-size="640,423" data-medium-file="https://i0.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ShadowProjection.jpg?fit=300%2C198" data-attachment-id="2145" data-image-meta="{" data-image-description="" src="https://i0.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ShadowProjection.jpg?resize=416%2C275" data-permalink="http://enhancedatascience.com/2017/07/31/machine-learning-explained-dimensionality-reduction/shadowprojection/" data-large-file="https://i0.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ShadowProjection.jpg?fit=640%2C423" alt="ShadowProjection" data-image-title="ShadowProjection" sizes="(max-width: 416px) 100vw, 416px" data-comments-opened="1" class="wp-image-2145" data-recalc-dims="1"></p>
<p>Each of these people can be represented as points in a 3 Dimensional space. With a gross approximation, each people is in a 50*50*200 (cm) cube. If we use a resolution of 1cm and three color channels, then can be represented by 1,000,000 variables.<br>
On the other hand, the shadow is only in 2 dimensions and in black and white, so each shadow only needs 50*200=10,000 variables.<br>
The number of variables was divided by 100 ! And if your goal is to detect human vs cat, or even men vs women, the data from the shadow may be enough.</p>
<h2>Why should you reduce the number of dimensions?</h2>
<p>Dimensionality reduction has several advantages from a machine learning point of view.</p>
<ul>
<li>Since your model has fewer degrees of freedom, the likelihood of <a rel="nofollow" href="http://enhancedatascience.com/2017/06/29/machine-learning-explained-overfitting/" target="_blank">overfitting</a> is lower. The model will generalize more easily on new data.</li>
<li>If you use feature selection or linear methods (such as PCA), the reduction will promote the most important variables which will improve the interpretability of your model.</li>
<li>Most of features extraction techniques are unsupervised. You can train your autoencoder or fit your PCA on unlabeled data. This can be really helpful is you have a lot of unlabeled data (for instance text or images) and labeling is time-consuming and expensive.</li>
</ul>
<h2>Features selection as a basic reduction</h2>
<p>The most obvious way to reduce dimensionality is to remove some dimensions and to select the more suitable variables for the problem.</p>
<p>Here are some ways to select variables:</p>
<ul>
<li>Greedy algorithms which add and remove variables until some criterion is met. For instance, the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Stepwise_regression" target="_blank">stepwise regression</a> with forward selection will add at each step the variable which improve the fit in the most significant way</li>
<li>Shrinking and penalization methods, which will add cost for having too many variables. For instance, <a rel="nofollow" href="http://enhancedatascience.com/2017/07/04/machine-learning-explained-regularization/" target="_blank">L1 regularizatio</a>n will cut some variables’ coefficient to zero. Regularization limits the space where the coefficients can live in.</li>
<li>Selection of models based on criteria taking the number of dimensions into accounts such as the adjusted R², AIC or BIC. Contrary to regularization, the model is not trained to optimize these criteria. The criteria are computed after fitting to compare several models.</li>
<li>Filtering of variables using <a rel="nofollow" href="https://en.wikipedia.org/wiki/Correlation_and_dependence" target="_blank">correlation</a>, <a rel="nofollow" href="https://en.wikipedia.org/wiki/Variance_inflation_factor" target="_blank">VIF</a> or some “distance measure” between the features.</li>
</ul>
<h2>Features Engineering and extraction to reduce dimensionality</h2>
<p>While features selection is efficient, it is brutal since variables are either kept or removed. However in some interval or for some modalities, removed variable may be useful while kept variable may be redundant. Features extraction (or engineering) seeks to keep only the intervals or modalities of the features which contain information.</p>
<h3>PCA and Kernel PCA</h3>
<p>Principal component analysis (or PCA), is a linear transformation of the data which looks for the axis where the data has the most variance. PCA will create new variables which are linear combinations of the original ones, these new variables will be orthogonal (i.e. correlation equals to zero). PCA can be seen as a rotation of the initial space to find more suitable axis to express the variability in the data.<br>
On the new variables have been created, you can select the most important ones. The threshold is up-to-you and depends on how much variance you want to keep. You can check the tutorial below to see a working R example:</p>
<p><a rel="nofollow" href="http://enhancedatascience.com/2017/05/07/r-basics-pca-r/" target="_blank">R Basics: PCA with R</a></p>
<p>Since PCA is linear, it mostly works on linearly separable data. Hence, if you want to perform classification on other data (Like the donut below), linear PCA will probably make you lose a lot of information.</p>
<p>Example of Kernel PCA from <a rel="nofollow" href="http://scikit-learn.org/" target="_blank">Scikit-Learn</a></p>
<p><img data-orig-file="https://i1.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ExempleDonutScikitLearn.png?fit=800%2C600" data-orig-size="800,600" data-medium-file="https://i1.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ExempleDonutScikitLearn.png?fit=300%2C225" data-attachment-id="2151" data-image-meta="{" data-image-description="" src="https://i1.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ExempleDonutScikitLearn.png?resize=324%2C243" data-permalink="http://enhancedatascience.com/2017/07/31/machine-learning-explained-dimensionality-reduction/exempledonutscikitlearn/" data-large-file="https://i1.wp.com/enhancedatascience.com/wp-content/uploads/2017/07/ExempleDonutScikitLearn.png?fit=640%2C480" alt="Exemple Donut Scikit Learn" data-image-title="Exemple Donut Scikit Learn" sizes="(max-width: 324px) 100vw, 324px" data-comments-opened="1" class="wp-image-2151" data-recalc-dims="1"></p>
<p>On the other hand, kernel PCA can work on nonlinearly separable data. The trick is simple:</p>
<ul>
<li>before applying PCA, a function is applied project the initial space in a bigger space where the data are separable. The function can be a polynomial, radial or some activation function.</li>
<li>Then a PCA is applied in this new space. (NB: It is not exactly PCA but close to it).</li>
</ul>
<p>Hence you can easily separate the two rings of the donut, which will improve the performance of classifiers. Kernel PCA raises two problems, you need to find the right kernel and the new variables are hard to understand for humans.</p>
<h3>LDA and Kernel LDA</h3>
<p>Linear discriminant analysis is similar to PCA but is supervised (while PCA does not require labels). The goal of the LDA is not to maximize variance but to create linear surfaces to separate the different groups. The new features will be axes that separate the data when the data are projected on them.</p>
<p>As with PCA, you can also apply the kernel trick to apply LDA on non linearly separable data.</p>
<h2>ICA and Kernel ICA</h2>
<p>Independant component analysis aims at creating independent variables from the original variables.<br>
The typical example Cocktail party problem: you are in a room with lots of different people having different conversations and your goal is to separate the different conversations. If you have a lot of microphones in the room, each and every of them will get a linear combination of all the conversation (some kind of noise). The goal of ICA is to disentangle these conversations from the noise.<br>
This can be seen as dimensionality reduction if you have 200 microphones but only ten independent conversations, you should be able to represent them with ten independant variables from the ICA.</p>
<h2>Autoencoder</h2>
<p>Autoencoder is a powerful method to reduce the dimensionality of data. It is composed of a neural network (it can be feed-forward, convolutional or recurrent, most of the architecture can be adapted into an autoencoder) which will try to learn its input. For instance, an autoencoder trained on images will try to reconstruct these images.</p>
<p>In addition to this, the autoencoder has a bottleneck, the number of neurons in the autoencoder’s hidden layers will be smaller than the number of input variables. Hence, the autoencoder has to learn a compressed representation of the data where the number of dimensions is the number of neuron in the smallest hidden layer.</p>
<p>The main advantages of autoencoder are their non-linearity and how flexible they can be.</p>
<h2>T-SNE</h2>
<p><a rel="nofollow" href="https://lvdmaaten.github.io/tsne/" target="_blank">T-SNE or t-distributed stochastic neighbor embedding</a> is mainly used to fit data from large dimensions in 2D or 3D space. That is the technique we used to fit and visualize the twitter data in our <a rel="nofollow" href="http://enhancedatascience.com/2017/07/17/twitter-analysis-using-r/" target="_blank">analysis of French election</a>. The main idea behind T-SNE is that nearby point in the original space should be close in the low dimensional space while distant point should also be distant in the smaller space.<br>
T-sne is highly non-linear, is originally non-parametric, dependant on the random seed and does not keep distance alike. Hence, I mainly use it to plot high dimension and to visualise cluster and similarity.</p>
<p>NB: There is also a parametric variant which seems less widely used than the original T-SNE.<br>
https://lvdmaaten.github.io/publications/papers/AISTATS_2009.pdf</p>
<p>Here ends our presentation of the most widely used dimensionality reduction techniques.</p>
<p> </p>
<p> </p>
<p> </p>
<p>The post <a rel="nofollow" href="http://enhancedatascience.com/2017/07/31/machine-learning-explained-dimensionality-reduction/" target="_blank">Machine Learning Explained: Dimensionality Reduction</a> appeared first on <a rel="nofollow" href="http://enhancedatascience.com/" target="_blank">Enhance Data Science</a>.</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://enhancedatascience.com/2017/07/31/machine-learning-explained-dimensionality-reduction/"> Enhance Data Science</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/2xS9oK-Rp8U" height="1"></a></div>