{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "a00d8ab0",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "5b394588",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.cluster.hierarchy import median, maxdists\nfrom scipy.spatial.distance import pdist",
            "outputs": []
        },
        {
            "id": "23dca37e",
            "cell_type": "markdown",
            "source": "Given a linkage matrix ``Z``, `scipy.cluster.hierarchy.maxdists`\ncomputes for each new cluster generated (i.e., for each row of the linkage\nmatrix) what is the maximum distance between any two child clusters.\n\nDue to the nature of hierarchical clustering, in many cases this is going\nto be just the distance between the two child clusters that were merged\nto form the current one - that is, Z[:,2].\n\nHowever, for non-monotonic cluster assignments such as\n`scipy.cluster.hierarchy.median` clustering this is not always the\ncase: There may be cluster formations were the distance between the two\nclusters merged is smaller than the distance between their children.\n\nWe can see this in an example:\n",
            "metadata": {}
        },
        {
            "id": "c9ef3328",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "X = [[0, 0], [0, 1], [1, 0],\n     [0, 4], [0, 3], [1, 4],\n     [4, 0], [3, 0], [4, 1],\n     [4, 4], [3, 4], [4, 3]]",
            "outputs": []
        },
        {
            "id": "52ae3a36",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z = median(pdist(X))\nZ",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.        ,  1.        ,  1.        ,  2.        ],\n       [ 3.        ,  4.        ,  1.        ,  2.        ],\n       [ 9.        , 10.        ,  1.        ,  2.        ],\n       [ 6.        ,  7.        ,  1.        ,  2.        ],\n       [ 2.        , 12.        ,  1.11803399,  3.        ],\n       [ 5.        , 13.        ,  1.11803399,  3.        ],\n       [ 8.        , 15.        ,  1.11803399,  3.        ],\n       [11.        , 14.        ,  1.11803399,  3.        ],\n       [18.        , 19.        ,  3.        ,  6.        ],\n       [16.        , 17.        ,  3.5       ,  6.        ],\n       [20.        , 21.        ,  3.25      , 12.        ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "3d418631",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "maxdists(Z)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1.        , 1.        , 1.        , 1.        , 1.11803399,\n       1.11803399, 1.11803399, 1.11803399, 3.        , 3.5       ,\n       3.5       ])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "05189a00",
            "cell_type": "markdown",
            "source": "Note that while the distance between the two clusters merged when creating the\nlast cluster is 3.25, there are two children (clusters 16 and 17) whose distance\nis larger (3.5). Thus, `scipy.cluster.hierarchy.maxdists` returns 3.5 in\nthis case.",
            "metadata": {}
        }
    ]
}