{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "4e9cf9ca",
            "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": "a7f6498c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.cluster.hierarchy import median, inconsistent, maxRstat\nfrom scipy.spatial.distance import pdist",
            "outputs": []
        },
        {
            "id": "208d4c33",
            "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": "069c6992",
            "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": "f28bac76",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z = median(pdist(X))\nR = inconsistent(Z)\nR",
            "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.05901699, 0.08346263, 2.        , 0.70710678],\n       [1.05901699, 0.08346263, 2.        , 0.70710678],\n       [1.05901699, 0.08346263, 2.        , 0.70710678],\n       [1.05901699, 0.08346263, 2.        , 0.70710678],\n       [1.74535599, 1.08655358, 3.        , 1.15470054],\n       [1.91202266, 1.37522872, 3.        , 1.15470054],\n       [3.25      , 0.25      , 3.        , 0.        ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "0956c0d1",
            "cell_type": "markdown",
            "source": "`scipy.cluster.hierarchy.maxRstat` can be used to compute\nthe maximum value of each column of ``R``, for each non-singleton\ncluster and its children:\n",
            "metadata": {}
        },
        {
            "id": "33fe7822",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "maxRstat(Z, R, 0)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1.        , 1.        , 1.        , 1.        , 1.05901699,\n       1.05901699, 1.05901699, 1.05901699, 1.74535599, 1.91202266,\n       3.25      ])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "d8ddf2f4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "maxRstat(Z, R, 1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.        , 0.        , 0.        , 0.        , 0.08346263,\n       0.08346263, 0.08346263, 0.08346263, 1.08655358, 1.37522872,\n       1.37522872])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "86b7d29b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "maxRstat(Z, R, 3)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([0.        , 0.        , 0.        , 0.        , 0.70710678,\n       0.70710678, 0.70710678, 0.70710678, 1.15470054, 1.15470054,\n       1.15470054])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}