Note
Click here to download the full example code or to run this example in your browser via Binder
This example plots the corresponding dendrogram of a hierarchical clustering using AgglomerativeClustering and the dendrogram method available in scipy.
import numpy as np from matplotlib import pyplot as plt from scipy.cluster.hierarchy import dendrogram from sklearn.datasets import load_iris from sklearn.cluster import AgglomerativeClustering def plot_dendrogram[model, **kwargs]: # Create linkage matrix and then plot the dendrogram # create the counts of samples under each node counts = np.zeros[model.children_.shape[0]] n_samples = len[model.labels_] for i, merge in enumerate[model.children_]: current_count = 0 for child_idx in merge: if child_idx 1\] original observation are labeled with the number of observations they contain in parentheses.no_plotbool, optionalWhen True, the final rendering is not performed. This is useful if only the data structures computed for the rendering are needed or if matplotlib is not available.
no_labelsbool, optionalWhen True, no labels appear next to the leaf nodes in the rendering of the dendrogram.
leaf_rotationdouble, optionalSpecifies the angle [in degrees] to rotate the leaf labels. When unspecified, the rotation is based on the number of nodes in the dendrogram [default is 0].
leaf_font_sizeint, optional Specifies the font size [in points] of the leaf labels. When unspecified, the size based on the number of nodes in the dendrogram.
leaf_label_funclambda or function, optionalWhen
leaf_label_func
is a callable function, for each leaf with cluster index \[k < 2n-1\]. The function is expected to return a string with the label for the leaf.Indices \[k < n\] correspond to original observations while indices \[k \geq n\] correspond to non-singleton clusters.
For example, to label singletons with their node id and non-singletons with their id, count, and inconsistency coefficient, simply do:
# First define the leaf label function. def llf[id]: if id >> from scipy.cluster import hierarchy >>> import matplotlib.pyplot as pltA very basic example:
>>> ytdist = np.array[[662., 877., 255., 412., 996., 295., 468., 268., ... 400., 754., 564., 138., 219., 869., 669.]] >>> Z = hierarchy.linkage[ytdist, 'single'] >>> plt.figure[] >>> dn = hierarchy.dendrogram[Z]Now, plot in given axes, improve the color scheme and use both vertical and horizontal orientations:
>>> hierarchy.set_link_color_palette[['m', 'c', 'y', 'k']] >>> fig, axes = plt.subplots[1, 2, figsize=[8, 3]] >>> dn1 = hierarchy.dendrogram[Z, ax=axes[0], above_threshold_color='y', ... orientation='top'] >>> dn2 = hierarchy.dendrogram[Z, ax=axes[1], ... above_threshold_color='#bcbddc', ... orientation='right'] >>> hierarchy.set_link_color_palette[None] # reset to default after use >>> plt.show[]