{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c360f95b",
            "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": "5e69d344",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.cluster.hierarchy import single, fcluster\nfrom scipy.spatial.distance import pdist",
            "outputs": []
        },
        {
            "id": "65909b45",
            "cell_type": "markdown",
            "source": "First, we need a toy dataset to play with\n```\n\nx x    x x\nx        x\n\nx        x\nx x    x x\n\n```",
            "metadata": {}
        },
        {
            "id": "3e565c43",
            "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": "9d8e1571",
            "cell_type": "markdown",
            "source": "Then, we get a condensed distance matrix from this dataset:\n",
            "metadata": {}
        },
        {
            "id": "14cda2ca",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "y = pdist(X)",
            "outputs": []
        },
        {
            "id": "ee44fad8",
            "cell_type": "markdown",
            "source": "Finally, we can perform the clustering:\n",
            "metadata": {}
        },
        {
            "id": "3c63f09b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "Z = single(y)\nZ",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[ 0.,  1.,  1.,  2.],\n       [ 2., 12.,  1.,  3.],\n       [ 3.,  4.,  1.,  2.],\n       [ 5., 14.,  1.,  3.],\n       [ 6.,  7.,  1.,  2.],\n       [ 8., 16.,  1.,  3.],\n       [ 9., 10.,  1.,  2.],\n       [11., 18.,  1.,  3.],\n       [13., 15.,  2.,  6.],\n       [17., 20.,  2.,  9.],\n       [19., 21.,  2., 12.]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "961c6cc2",
            "cell_type": "markdown",
            "source": "The linkage matrix ``Z`` represents a dendrogram - see\n`scipy.cluster.hierarchy.linkage` for a detailed explanation of its\ncontents.\n\nWe can use `scipy.cluster.hierarchy.fcluster` to see to which cluster\neach initial point would belong given a distance threshold:\n",
            "metadata": {}
        },
        {
            "id": "28ccffb0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fcluster(Z, 0.9, criterion='distance')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 7,  8,  9, 10, 11, 12,  4,  5,  6,  1,  2,  3], dtype=int32)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "f67fbce7",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fcluster(Z, 1, criterion='distance')",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([3, 3, 3, 4, 4, 4, 2, 2, 2, 1, 1, 1], dtype=int32)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "84239efd",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "fcluster(Z, 2, 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": "cfc4685d",
            "cell_type": "markdown",
            "source": "Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a\nplot of the dendrogram.",
            "metadata": {}
        }
    ]
}