{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "f2ebff6b",
            "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": "325c8bec",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.cluster.hierarchy import ward, inconsistent, is_valid_im\nfrom scipy.spatial.distance import pdist",
            "outputs": []
        },
        {
            "id": "603cc48d",
            "cell_type": "markdown",
            "source": "Given a data set ``X``, we can apply a clustering method to obtain a\nlinkage matrix ``Z``. `scipy.cluster.hierarchy.inconsistent` can\nbe also used to obtain the inconsistency matrix ``R`` associated to\nthis clustering process:\n",
            "metadata": {}
        },
        {
            "id": "a96673fd",
            "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": "c486d56d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z = ward(pdist(X))\nR = inconsistent(Z)\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": "9cd6da51",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "R",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[1.        , 0.        , 1.        , 0.        ],\n       [1.        , 0.        , 1.        , 0.        ],\n       [1.        , 0.        , 1.        , 0.        ],\n       [1.        , 0.        , 1.        , 0.        ],\n       [1.14549722, 0.20576415, 2.        , 0.70710678],\n       [1.14549722, 0.20576415, 2.        , 0.70710678],\n       [1.14549722, 0.20576415, 2.        , 0.70710678],\n       [1.14549722, 0.20576415, 2.        , 0.70710678],\n       [2.78516386, 2.58797734, 3.        , 1.15470054],\n       [2.78516386, 2.58797734, 3.        , 1.15470054],\n       [6.57065706, 1.38071187, 3.        , 1.15470054]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4afcf745",
            "cell_type": "markdown",
            "source": "Now we can use `scipy.cluster.hierarchy.is_valid_im` to verify that\n``R`` is correct:\n",
            "metadata": {}
        },
        {
            "id": "3d57d500",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "is_valid_im(R)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9e663a9f",
            "cell_type": "markdown",
            "source": "However, if ``R`` is wrongly constructed (e.g., one of the standard\ndeviations is set to a negative value), then the check will fail:\n",
            "metadata": {}
        },
        {
            "id": "adbbddb7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "R[-1,1] = R[-1,1] * -1\nis_valid_im(R)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "False"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}