{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Showing clusters\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A cluster is displayed as a subtree in the dendrogram. The :func:`~pdendro.attach_dendrogram`\nfunction can display clusters by specifying their root nodes. Let us assume that we have\nthe following linkage matrix:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import scipy\n\nX = [[0.0], [1.0], [3.0], [6.0]]\nZ = scipy.cluster.hierarchy.linkage(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data points can be assigned to clusters using the :func:`scipy.cluster.hierarchy.fcluster`\nfunction, and their root nodes can be obtained using the :func:`scipy.cluster.hierarchy.leaders`\nfunction:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "T = scipy.cluster.hierarchy.fcluster(Z, 1.5, criterion=\"distance\")\nL, _ = scipy.cluster.hierarchy.leaders(Z, T)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, the clusters can be shown with root node markers and different colors:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import plotly.graph_objects as go\nimport plotly.io as pio\n\nfrom pdendro import attach_dendrogram\n\nfig = go.Figure()\nattach_dendrogram(fig, Z, subtrees=L)\npio.show(fig)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.4" } }, "nbformat": 4, "nbformat_minor": 0 }