{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "f57c1019",
            "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": "49a6a2bf",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.cluster.hierarchy import ward, fcluster\nfrom scipy.spatial.distance import pdist",
            "outputs": []
        },
        {
            "id": "4c9d1480",
            "cell_type": "markdown",
            "source": "All cluster linkage methods - e.g., `scipy.cluster.hierarchy.ward`\ngenerate a linkage matrix ``Z`` as their output:\n",
            "metadata": {}
        },
        {
            "id": "c1c3e36d",
            "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": "cecb6266",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z = ward(pdist(X))",
            "outputs": []
        },
        {
            "id": "35d63e34",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z",
            "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": "df90572f",
            "cell_type": "markdown",
            "source": "This matrix represents a dendrogram, where the first and second elements\nare the two clusters merged at each step, the third element is the\ndistance between these clusters, and the fourth element is the size of\nthe new cluster - the number of original data points included.\n\n`scipy.cluster.hierarchy.fcluster` can be used to flatten the\ndendrogram, obtaining as a result an assignation of the original data\npoints to single clusters.\n\nThis assignation mostly depends on a distance threshold ``t`` - the maximum\ninter-cluster distance allowed:\n",
            "metadata": {}
        },
        {
            "id": "44ad8487",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fcluster(Z, t=0.9, criterion='distance')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "9439f5a2",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fcluster(Z, t=1.1, criterion='distance')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8], dtype=int32)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "c1766518",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fcluster(Z, t=3, criterion='distance')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4], dtype=int32)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "286508fe",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fcluster(Z, t=9, criterion='distance')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "131699f3",
            "cell_type": "markdown",
            "source": "In the first case, the threshold ``t`` is too small to allow any two\nsamples in the data to form a cluster, so 12 different clusters are\nreturned.\n\nIn the second case, the threshold is large enough to allow the first\n4 points to be merged with their nearest neighbors. So, here, only 8\nclusters are returned.\n\nThe third case, with a much higher threshold, allows for up to 8 data\npoints to be connected - so 4 clusters are returned here.\n\nLastly, the threshold of the fourth case is large enough to allow for\nall data points to be merged together - so a single cluster is returned.",
            "metadata": {}
        }
    ]
}