{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "ab433941",
            "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": "85253b3b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.cluster.hierarchy import ward, is_valid_linkage\nfrom scipy.spatial.distance import pdist",
            "outputs": []
        },
        {
            "id": "10d6020b",
            "cell_type": "markdown",
            "source": "All linkage matrices generated by the clustering methods in this module\nwill be valid (i.e., they will have the appropriate dimensions and the two\nrequired expressions will hold for all the rows).\n\nWe can check this using `scipy.cluster.hierarchy.is_valid_linkage`:\n",
            "metadata": {}
        },
        {
            "id": "d6ca7631",
            "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": "5b80d31a",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z = ward(pdist(X))\nZ",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.        ,  1.        ,  1.        ,  2.        ],\n       [ 3.        ,  4.        ,  1.        ,  2.        ],\n       [ 6.        ,  7.        ,  1.        ,  2.        ],\n       [ 9.        , 10.        ,  1.        ,  2.        ],\n       [ 2.        , 12.        ,  1.29099445,  3.        ],\n       [ 5.        , 13.        ,  1.29099445,  3.        ],\n       [ 8.        , 14.        ,  1.29099445,  3.        ],\n       [11.        , 15.        ,  1.29099445,  3.        ],\n       [16.        , 17.        ,  5.77350269,  6.        ],\n       [18.        , 19.        ,  5.77350269,  6.        ],\n       [20.        , 21.        ,  8.16496581, 12.        ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2525714b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "is_valid_linkage(Z)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f6d3daa5",
            "cell_type": "markdown",
            "source": "However, if we create a linkage matrix in a wrong way - or if we modify\na valid one in a way that any of the required expressions don't hold\nanymore, then the check will fail:\n",
            "metadata": {}
        },
        {
            "id": "6f078ad7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z[3][1] = 20    # the cluster number 20 is not defined at this point\nis_valid_linkage(Z)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "False"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}