How to evaluate unsupervised learning

Every time we build a machine learning model or any predictive model, the first thing we ask is how to evaluate it? What’s the best metric for each model? For supervised machine learning problem,  there are usually pre-set or well-known metrics. But for unsupervised learning, what should we do?

Let’s first look at what’s the typical unsupervised learning algorithms and its corresponding application scenes.

Typical unsupervised learning includes:

  • Hierarchical clustering: builds a multilevel hierarchy of clusters by creating a cluster tree
  • k-means clustering: partitions data into k distinct clusters based on distance to the centroid of a cluster
  • Gaussian mixture models: models clusters as a mixture of multivariate normal density components
  • Self-organizing maps: uses neural network that learns the topology and distribution of the data
  • Hidden Markov models: uses observed data to recover the sequence of states
  • Generative model such as Boltzmann machine to generate the distribution of outputs similar to input

Unsupervised learning methods are sued in bioinformatics for sequence analysis and genetic clustering; in data mining for sequence and pattern mining; in medical imaging for image segmentation; and in computer vision for object recognition, dimensionality reduction techniques for reducing dimensions.

Let’s go back to our original question: how to evaluate unsupervised learning?

Obviously, the answer depends on the class of unsupervised algorithms you use.

  1. Dimensionality reduction algorithms

For this type of algorithms, we can use methods similar to supervised learning by looking at its reconstructing error with test dataset or by applying a k-fold cross-validation procedure.

2.  Clustering algorithms

It is difficult to evaluate a clustering if you don’t have labeled test data. Typically there are two types of metrics: I. internal metrics, use only information on the computed clusters to evaluate if clusters are compact and well-separated[3]; II. external metrics that perform a statistical testing on the structure of your data [1].

For external indices, we evaluate the results of a clustering algorithm based on a known cluster structure of a data set (or cluster labels).

For internal indices, we evaluate the results using quantities and features inherent in the data set. The optimal number of clusters is usually determined based on an internal validity index.

A very good resource for clustering evaluation is from sklearn’s documentation page where it listed methods like adjusted rand index, mutual information based scores,  homogeneity,, completeness and V-measure, Fowlkes-Mallows scores and etc. With one method not covered: the Silhouette Coefficient which assumes ground truth labels are available.

Sometimes, an extrinsic performance function can be defined to evaluate it. For instance, if clustering is used to create meaningful classes (e.g. documents classification), it is possible to create an external dataset by hand-labeling and test the accuracy (gold standard). Another way of evaluating clustering is using high-dimension visualization tools like t-sne to visually check. For example, for feature learning in images, visualization of the learned features can be useful.

Screen Shot 2018-01-27 at 9.48.47 PM.png

3. Generative models

This type of method is stochastic, the actual value achieved after a given amount of training may depend on random seeds. So we can vary these and compare several runs to see if there is significant different performance. Also, visualizing the constructed output along with input can be a good metric too. For example, reconstructed hand-written digits with RBM can be compared with training samples.



[1] Halkidi, Maria, Yannis Batistakis, and Michalis Vazirgiannis. “On clustering validation techniques.” Journal of Intelligent Information Systems 17.2-3 (2001): 107-145.
[2] Hall, Peter, Jeff Racine, and Qi Li. “Cross-validation and the estimation of conditional probability densities.” Journal of the American Statistical Association 99.468 (2004).
[3] Yanchi Liu, Zhongmou Li, and Hui Xiong. “Understanding of Internal Clustering Validation Measures” IEEE International Conference on Data Mining 2010.







How to approach a time series forecast problem?

Time series prediction is the first type of forecast that we attempted to do, starting from forecasting the tide, temperature, stock price etc. There are many traditional and also advanced predicting methods. Sometimes, the traditional simple model (AR, MA, ARMA) will give you a baseline or first-peek into understanding the data and the underlining problem it represents.

While approaching time series analysis, there are typically two components necessary for understanding it: 1. decomposition 2, forecast.

  • Decomposition

The general starting approach for time series decomposition analysis follows three steps:

  1. Identify whether the data has seasonality or trends.
  2. Identify whether the decomposition technique required is additive or multiplicative.
  3. Test appropriate additive algorithm
 suitable mode\treats Seasonal Trend Correlation
Simple Moving Average Smooth NO YES NO
Seasonal Adjustment YES YES NO
Simple Exponential Smoothing NO YES NO
Holts Exponential Smoothing NO YES NO
Holt-Winters Exponential Smoothing YES YES NO
  • Forecast

The biggest challenging for predicting time series without other external features is autocorrelation with its error terms. This is a reference chart to determine which model will be most suitable based on ACP/PACF analysis with your data alone.

Reference for model approach
Reference for model approach in time series

We need to look carefully at the ACF and PACF correlograms and search examine its pattern to determine which model approach AR(p), MA(q) or ARMA(p,q), makes the most sense for the GLM approach. The reference chart can be used as a general guideline for this evaluation.

Simple is more sometimes.


How to choose the number of hidden layers and nodes in a feedforward neural network?

Whiling building neural networks, it takes a lot of time to fine-tune the hyperparameters from the number of layers, the number of nodes each layer, learning rate, momentum etc. How to best choose the most important starting parameters while setting up a  neural network? Especially the number of layers and number of nodes. Is there any rule of thumb?

Before jumping to the conclusions, let’s review the key parts of a feedforward neural network and its intrinsic properties. A typical feedforward neural network has three parts: the input layer, hidden layer, and the output layer.

The input layer:  

Simple – every NN has only one layer (also referred as activation layer of zero) and the number of neurons equals to the number of features in the input (columns in the input dataset).

The output layer:

Like the input layer, each neural network only has one output layer. The size is totally determined by the model configuration. For example, it is one for regression output, and number of class for classification (10 for example in hand-written digits recognition).

The hidden layer:

This is what we need to figure out. How many layers do we need?

First, look at your data problem, if it is linearly separable, then you’ll need none hidden layer.

If it’s not linearly separable, there are numerous comments on this question.  One issue within this subject on which there is a consensus is the performance difference from adding additional hidden layers: the situations in which performance improves with a second (or third, etc.) hidden layer are very few. One hidden layer is sufficient for the large majority of problems. (ref:

Here is the excerpt from Jeff Heaton’s book?

 There is currently no theoretical reason to use neural networks with any more than two hidden layers. In fact, for many practical problems, there is no reason to use any more than one hidden layer. Table 5.1 summarizes the capabilities of neural network architectures with various hidden layers.

Table 5.1: Determining the Number of Hidden Layers

| Number of Hidden Layers | Result |

 0 - Only capable of representing linear separable functions or decisions.

 1 - Can approximate any function that contains a continuous mapping
from one finite space to another.

 2 - Can represent an arbitrary decision boundary to arbitrary accuracy
with rational activation functions and can approximate any smooth
mapping to any accuracy.

How about the number of neurons?

According to the Jeff Heaton, “the optimal size of the hidden layer is usually between the size of the input and size of the output layers.” — “Introduction to Neural Networks in Java”

From Jeff Heaton’s book.

Using too few neurons in the hidden layers will result in something called underfitting. Underfitting occurs when there are too few neurons in the hidden layers to adequately detect the signals in a complicated data set.

Using too many neurons in the hidden layers can result in several problems. First, too many neurons in the hidden layers may result in overfitting. Overfitting occurs when the neural network has so much information processing capacity that the limited amount of information contained in the training set is not enough to train all of the neurons in the hidden layers. A second problem can occur even when the training data is sufficient. An inordinately large number of neurons in the hidden layers can increase the time it takes to train the network. The amount of training time can increase to the point that it is impossible to adequately train the neural network. Obviously, some compromise must be reached between too many and too few neurons in the hidden layers.

There are many rule-of-thumb methods for determining the correct number of neurons to use in the hidden layers, such as the following:

  • The number of hidden neurons should be between the size of the input layer and the size of the output layer.
  • The number of hidden neurons should be 2/3 the size of the input layer, plus the size of the output layer.
  • The number of hidden neurons should be less than twice the size of the input layer.

These three rules provide a starting point for you to consider. Ultimately, the selection of an architecture for your neural network will come down to trial and error. But what exactly is meant by trial and error? You do not want to start throwing random numbers of layers and neurons at your network. To do so would be very time consuming. Chapter 8, “Pruning a Neural Network” will explore various ways to determine an optimal structure for a neural network.

Empirical equations for determining number of neurons:

A. For supervised learning problems, there are some empirical formulas to determine the size of neurons:

Screen Shot 2018-01-02 at 10.51.32 AM.png

This concept is explained in excellent NN design, where you want to limit the number of free parameters in your model (its degree or number of nonzero weights) to a small portion of the degrees of freedom in your data. The data’s degree of freedom is Ns * (Ni+No) assuming all independent. Alpha in this equation is a way to indicate how general you want to prevent overfitting.

B. A rough approximation can be obtained by the geometric pyramid rule proposed by Masters (1993). For a three layer network with n input and m output neurons, the hidden layer would have sqrt(N * M) neurons.  — Masters, Timothy. Pratical neural network recipes in C++. Morgan Kaufmann, 1993.

In the end, it involves trial and errors. There are many optimization methods to address this problem after NN initialization. Most popular ones are like pruning and up-front approach like genetic algorithms. While using pruning, you can look at the weights that it learned. Usually, when the weights are close to zero, it means that neuron is not important. And you can also build automatically pruning algorithms by iteratively reducing the neuron size and compare the model performance.