Mark Pundurs
Mark Pundurs

Reputation: 55

scipy.cluster.hierarchy.dendrogram(): exactly what does truncate_mode='level' do?

The documentation says, "No more than p levels of the dendrogram tree are displayed. A “level” includes all nodes with p merges from the last merge." (p is another parameter) I can't figure out what "p merges from the last merge" means. Can anyone explain it?

(I've created dendrograms of the same data with truncate_mode='level' and truncate_mode='lastp'; I didn't find the comparison illuminating.)

Upvotes: 3

Views: 3511

Answers (1)

JohanC
JohanC

Reputation: 80329

The following code demonstrates truncate_mode='level' and truncate_mode='lastp':

from scipy.cluster import hierarchy
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(1729)
ytdist = np.random.randint(1, 1000, 36)

Z = hierarchy.linkage(ytdist, 'single')
fig, ax_rows = plt.subplots(ncols=6, nrows=2, sharey=True, figsize=(16, 5))

for ax_row, truncate_mode in zip(ax_rows, ['level', 'lastp']):
    hierarchy.dendrogram(Z, ax=ax_row[0])
    ax_row[0].set_title('default, no truncation')
    for ind, ax in enumerate(ax_row[1:]):
        if truncate_mode == 'level':
            p = len(ax_row) - ind - 1
        else:
            p = len(ax_row) - ind
        hierarchy.dendrogram(Z, truncate_mode=truncate_mode, p=p, ax=ax)
        ax.set_title(f"truncate_mode='{truncate_mode}', p={p}")
plt.tight_layout()
plt.show()

comparison plot

Upvotes: 4

Related Questions