3

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.)

Mark Pundurs
  • 45
  • 1
  • 6

1 Answers1

4

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

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Thanks, that's very helpful! (It would be incrementally more helpful if the order of truncations was reversed for the subplots so the least severe truncation was next to the untruncated results.) And to go from the example to an answer to my question: The "last" merge referred to in the docs is the final merge, after which there is only one cluster containing all data. I was reading "last" as "previous." – Mark Pundurs Feb 15 '21 at 15:26
  • 1
    FYI, I created a doc change pull request, which has been merged into scipy:master - https://github.com/scipy/scipy/commit/e53f57783989a6311b9ec50242010569e1bde2a1 – Mark Pundurs Feb 18 '21 at 15:46
  • I don't quite understand the chart truncate_mode='level', p=2. It seems that the small yellow split is 3 level from the final split, but it isn't truncated? – LNQ Jun 12 '23 at 23:19