PCA vs Autoencoders for Dimensionality Reduction

There are a few ways to reduce the dimensions of large data sets to ensure computational efficiency such as backwards selection, removing variables exhibiting high correlation, high number of missing values but by far the most popular is principal components analysis. A relatively new method of dimensionality reduction is the autoencoder. Autoencoders are a branch of neural network which attempt to compress the information of the input variables into a reduced dimensional space and then recreate the input data set. Typically the autoencoder is trained over number of iterations using gradient descent, minimising the mean squared error. The key component is the “bottleneck” hidden layer. This is where the information from the input has been compressed. By extracting this layer from the model, each node can now be treated as a variable in the same way each chosen principal component is used as a variable in following models.

This post will compare the performance of the autoencoder and PCA. For this analysis the Australian Institute of Sport data set will be used which can be found in the DAAG package.

Principal Components Analysis

PCA reduces the data frame by orthogonally transforming the data into a set of principal components. The first principal component explains the most amount of the variation in the data in a single component, the second component explains the second most amount of the variation, etc. By choosing the top k principal components that explain say 80-90% of the variation, the other components can be dropped since they do not significantly benefit the model. This procedure retains some of the latent information in the principal components which can help to build better models.

A PCA procedure will be applied to the continuous variables on this data set.

plot of chunk PCA

This suggests the first 6 components account for approximately 90% of the variation in the data. The first 2 components will be visualised in a scatter graph. Just for some context male and females will be highlighted.

plot of chunk PCA plot

By only visualising 2 principal components were are not seeing all of the variation in the data. Plotting the data points in 3 dimensions gives a better indication of the structure of the data. However, there are still 8 dimensions which explain some of the variation that are not visualised. To do so they would all need to be plotted in their various combinations. This is a draw back of PCA.


This shows how the third dimension separates the males from the females.


The same variables will be condensed into 2 and 3 dimensions using an autoencoder. The autoencoder will be constructed using the keras package. As with any neural network there is a lot of flexibility in how autoencoders can be constructed such as the number of hidden layers and the number of nodes in each. With each hidden layer the network will attempt to find new structures in the data. In general autoencoders are symmetric with the middle layer being the bottleneck. The first half of the autoencoder is considered the encoder and the second half is considered the decoder.

plot of chunk AE plot

The autoencoder is still separating the males from the females in this example however it picks up on structure in the data that PCA does not. Given this is a small example data set with only 11 variables the autoencoder does not pick up on too much more than the PCA. Highly complex data with perhaps thousands of dimensions the autoencoder has a better chance of unpacking the structure and storing it in the hidden nodes by finding hidden features. In contrast to PCA the autoencoder has all the information from the original data compressed in to the reduced layer. To view the data in 3 dimensions the model will need to be fit again with the bottleneck layer with 3 nodes.


There is a slight difference between the autoencoder and PCA plots and perhaps the autoencoder does slightly better at differentiating between male and female athletes. Again, with a larger data set this will be more pronounced.

Comparison of reconstruction error

To compare the the results of PCA and the autoencoder we observe the reconstruction error. This is the mean squared error of the reconstructed input data from it’s reduced state and the true input data. The PCA reconstruction is done by the following

    \[ \mathbf{\hat{X}} = \mathbf{P}_k\mathbf{V}^T_k + \mathbf{\mu} \]

where \mathbf{P}_k is the principal component matrix keeping only the first k principal components, \mathbf{V}^T_k is the rotation matrix retaining only the first k components and \mathbf{\mu} is the matrix of means.

Reconstruction of the original data by the autoencoder is essentially already done. It is this error which was minimised to construct the reduced set. As mentioned, the first half of the autoencoder is the encoder and the second is the decoder. Since the autoencoder encodes all the information available into the reduced layer, in turn the decoder is better equipped to reconstruct the original data set. The decoder is also a more sophisticated and optimised process than PCA.

The original data set will be reconstructed for the PCA and autoencoder for values k={1,…10}.

plot of chunk reconstruction

What is interesting is the autoencoder is better at reconstructing the original data set than PCA when k is small, however the error converges as k increases. For very large data sets this difference will be larger and means a smaller data set could be used for the same error as PCA. When dealing with big data this is an important property.

Differences between PCA and autoencoders

To summarise, the key differences for consideration between PCA and autoencoders are:

  • There are no guidelines to choose the size of the bottleneck layer in the autoencoder unlike PCA. With PCA, the top k components can be chosen to factor in x% of the variation. Often PCA can be used as a guide to choose k.
  • The autoencoder tends to perform better when k is small when compared to PCA, meaning the same accuracy can be achieved with less components and hence a smaller data set. This is important when dealing with very large data sets.
  • When visualising the PCA output, in general the first 2 or 3 components are used. The drawback is the other components are not visable on the plot and therefore not seeing all the information. Different combinations of dimensions will need to be plotted. Autoencoders can be constructed to reduce the full data down to 2 or 3 dimensions retaining all the information which can save time.
  • Autoencoders require more computation than PCA. Although, for very large data sets that can’t be stored in memory, PCA will not be able to be performed. The autoencoder construction using keras can easily be batched resolving memory limitations.

Final thoughts

Autoencoders are my new favorite dimensionality reduction technique, they perform very well and retain all the information of the original data set. They do have draw backs with computation and tuning, but the trade off is higher accuracy. They are great at visualising the data since all the information is retained in 2 or 3 dimensions contrary to PCA. In future posts I’ll look at building an autoencoder for dimensionality reduction from scratch and also look at the maths behind PCA.

Follow me on social media:

Leave a Reply

Your email address will not be published. Required fields are marked *