{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "c824167b",
            "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": "ca4b5687",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.spatial.distance import pdist, squareform",
            "outputs": []
        },
        {
            "id": "23f660a7",
            "cell_type": "markdown",
            "source": "``x`` is an array of five points in three-dimensional space.\n",
            "metadata": {}
        },
        {
            "id": "25f4c97d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = np.array([[2, 0, 2], [2, 2, 3], [-2, 4, 5], [0, 1, 9], [2, 2, 4]])",
            "outputs": []
        },
        {
            "id": "deef0762",
            "cell_type": "markdown",
            "source": "``pdist(x)`` computes the Euclidean distances between each pair of\npoints in ``x``.  The distances are returned in a one-dimensional\narray with length ``5*(5 - 1)/2 = 10``.\n",
            "metadata": {}
        },
        {
            "id": "49b6497b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "distvec = pdist(x)\ndistvec",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([2.23606798, 6.40312424, 7.34846923, 2.82842712, 4.89897949,\n       6.40312424, 1.        , 5.38516481, 4.58257569, 5.47722558])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e77cf74e",
            "cell_type": "markdown",
            "source": "``squareform(distvec)`` returns the 5x5 distance matrix.\n",
            "metadata": {}
        },
        {
            "id": "0b831371",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "m = squareform(distvec)\nm",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0.        , 2.23606798, 6.40312424, 7.34846923, 2.82842712],\n       [2.23606798, 0.        , 4.89897949, 6.40312424, 1.        ],\n       [6.40312424, 4.89897949, 0.        , 5.38516481, 4.58257569],\n       [7.34846923, 6.40312424, 5.38516481, 0.        , 5.47722558],\n       [2.82842712, 1.        , 4.58257569, 5.47722558, 0.        ]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "4ea61e89",
            "cell_type": "markdown",
            "source": "When given a square distance matrix ``m``, ``squareform(m)`` returns\nthe one-dimensional condensed distance vector associated with the\nmatrix.  In this case, we recover ``distvec``.\n",
            "metadata": {}
        },
        {
            "id": "d636c414",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "squareform(m)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([2.23606798, 6.40312424, 7.34846923, 2.82842712, 4.89897949,\n       6.40312424, 1.        , 5.38516481, 4.58257569, 5.47722558])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}