Chapter 2: Hierarchical Clustering
Activity 2: Applying Linkage Criteria
Solution:
- Visualize the
x
dataset that we created in Exercise 7, Building a Hierarchy:from scipy.cluster.hierarchy import linkage, dendrogram, fcluster from sklearn.datasets import make_blobs import matplotlib.pyplot as plt %matplotlib inline # Generate a random cluster dataset to experiment on. X = coordinate points, y = cluster labels (not needed) X, y = make_blobs(n_samples=1000, centers=8, n_features=2, random_state=800) # Visualize the data plt.scatter(X[:,0], X[:,1]) plt.show()
The output is as follows:
Figure 2.20: A scatter plot of the generated cluster dataset
- Create a list with all the possible linkage method hyperparameters:
methods = ['centroid', 'single', 'complete', 'average', 'weighted']
- Loop through each of the methods in the list that you just created and display the effect that they have on the same dataset:
for method in methods...