hierarchical clustering we can use the same data to perform hierarchical clustering and see if the results change much as compared to -means clustering and the actual labels in scikit-learn we have multitude of interfaces like the agglomerativeclustering class to perform hierarchical clustering based on what we discussed earlier in this as well as in agglomerative clustering is hierarchical clustering using bottom up approach each observation starts in its own cluster and clusters are successively merged together the merging criteria can be used from candidate set of linkagesthe selection of linkage governs the merge strategy some examples of linkage criteria are wardcomplete linkageaverage linkage and so on we will leverage low-level functions from scipy however because we still need to mention the number of clusters in the agglomerativeclustering interface which we want to avoid since we already have the breast cancer feature set in variable xthe following code helps us compute the linkage matrix using ward' minimum variance criterion from scipy cluster hierarchy import dendrogramlinkage import numpy as np np set_printoptions(suppress=truez linkage( 'ward'print( [ ]on seeing the preceding outputyou might think what does this linkage matrix indicateyou can think of the linkage matrix as complete historical mapkeeping track of which data points were merged into which cluster during each iteration if you have data pointsthe linkage matrixz will be having shape of ( where [iwill tell us which clusters were merged at the th iteration each row has four elementsthe first two elements are either data point identifiers or cluster labels (in the later parts of the matrix once multiple data points are merged)the third element is the cluster distance between the first two elements (either data points or clusters)and the last element is the total number of elements\data points in the cluster once the merge is complete we recommend you refer to reference/generated/scipy cluster hierarchy linkage htmlwhich explains this in detail the best way to visualize these distance-based merges is to use dendrogramas shown in figure - plt figure(figsize=( )plt title('hierarchical clustering dendrogram'plt xlabel('data point'plt ylabel('distance'dendrogram(zplt axhline( = =' 'ls='--'lw= plt show(