Basics of hierarchical clustering

1. Basics of hierarchical clustering

Hello everyone! In the previous chapter, you were introduced to the basics of two clustering algorithms. This chapter focuses on performing hierarchical clustering with SciPy. This video looks at the various parameters of the hierarchical clustering algorithm.

2. Creating a distance matrix using linkage

A critical step is to compute the distance matrix at each stage. This is achieved through the linkage method available in scipy-dot-cluster-dot-hierarchy. This process computes the distances between clusters as we go from N clusters to 1 cluster, where N is the number of points. There are four parameters for this method. The first parameter is the observations. The second parameter, method, tells the algorithm how to calculate proximity between two clusters. The metric is the function that decides the distance between two objects. Euclidean distance is a straight line distance between two points on a 2D plane. You can use your own function here. The optimal_ordering is an optional argument that changes the order of linkage matrix. We will not use this argument. Let us explore the method argument.

3. Which method should use?

The second parameter, method, decides how clusters are separated at each step. This is the parameter that we will tweak in this lesson and see the differences. The single method decides the proximity of clusters based on their two closest objects. On the other extreme end, the complete method decides the proximity of cluster centers based on their two farthest objects. The average and centroid methods decide cluster proximities based on arithmetic and geometric means, respectively. The median method uses the median of cluster objects. Finally, the ward method that we used earlier computes cluster proximity using the difference between summed squares of their joint clusters minus the individual summed squares. The ward method focuses on clusters more concentric towards its center.

4. Create cluster labels with fcluster

Once you have created the distance matrix, you can create the cluster labels through the fcluster method, which takes three arguments -the distance matrix, the number of clusters and the criteria to form the clusters based on certain thresholds. We will use the value of maxclust in the criterion argument.

5. Hierarchical clustering with ward method

Let us try to understand the differences between various methods to perform hierarchical clustering on a list of points on a 2D plane. This is the result using the ward method. Notice that clusters are generally dense towards the centers.

6. Hierarchical clustering with single method

Next, we will use the single method to see how the clusters change. Recall the single method used the two closest objects between clusters to determine the inter-cluster proximity. Naturally, the clusters formed when performing clustering through this method are more dispersed. Although the top cluster, labeled 1, is roughly the same, most objects from cluster 3 have shifted to cluster 2.

7. Hierarchical clustering with complete method

In the next and final iteration, we look at the clusters formed by the complete method. This method uses the two farthest objects among clusters to determine inter-cluster proximity. Coincidentally, though, the results of the complete method on the same data points that we used is similar to that of the ward method.

8. Final thoughts on selecting a method

Here are a few thoughts before we complete this lesson. First, there is no right method that you can apply to all problems that you face. You would need to carefully study the data that you are going to handle to decide which method is right for your case, which falls outside the scope of this course.

9. Let's try some exercises

It is now time for you to try some exercises.