{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "BulletList",
          "__tag": 4053,
          "ordered": true,
          "start": 1,
          "children": [
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "For method 'single', an optimized algorithm based on minimum spanning    tree is implemented. It has time complexity "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "O(n^2)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ".    For methods 'complete', 'average', 'weighted' and 'ward', an algorithm    called nearest-neighbors chain is implemented. It also has time    complexity "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "O(n^2)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ".    For other methods, a naive algorithm is implemented with "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "O(n^3)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "    time complexity.    All algorithms use "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "O(n^2)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " memory.    Refer to "
                    },
                    {
                      "__type": "FootnoteReference",
                      "__tag": 4066,
                      "label": "1"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " for details about the algorithms."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Methods 'centroid', 'median', and 'ward' are correctly defined only if    Euclidean pairwise metric is used. If "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "y"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is passed as precomputed    pairwise distances, then it is the user's responsibility to assure that    these distances are in fact Euclidean, otherwise the produced result    will be incorrect."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "Z",
              "annotation": "ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The hierarchical clustering encoded as a linkage matrix."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Perform hierarchical/agglomerative clustering."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "y",
              "annotation": "ndarray",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "A condensed distance matrix. A condensed distance matrix is a flat array containing the upper triangular of the distance matrix. This is the form that "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "pdist"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " returns. Alternatively, a collection of "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "m"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " observation vectors in "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "n"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " dimensions may be passed as an "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "m"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " by "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "n"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " array. All elements of the condensed distance matrix must be finite, i.e., no NaNs or infs."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "method",
              "annotation": "str, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The linkage algorithm to use. See the "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "Linkage Methods"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " section below for full descriptions."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "metric",
              "annotation": "str or function, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The distance metric to use in the case that y is a collection of observation vectors; ignored otherwise. See the "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "pdist"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " function for a list of valid distance metrics. A custom distance function can also be used."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "optimal_ordering",
              "annotation": "bool, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "If True, the linkage matrix will be reordered so that the distance between successive leaves is minimal. This results in a more intuitive tree structure when the data are visualized. defaults to False, because this algorithm can be slow, particularly on large datasets "
                    },
                    {
                      "__type": "FootnoteReference",
                      "__tag": 4066,
                      "label": "2"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". See also the "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "optimal_leaf_ordering",
                      "reference": {
                        "__type": "LocalRef",
                        "__tag": 4022,
                        "kind": "module",
                        "path": "scipy.cluster.hierarchy._hierarchy_impl:optimal_leaf_ordering"
                      },
                      "kind": "module"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " function."
                    }
                  ]
                },
                {
                  "__type": "Admonition",
                  "__tag": 4056,
                  "kind": "versionadded",
                  "base_type": "neutral",
                  "children": [
                    {
                      "__type": "AdmonitionTitle",
                      "__tag": 4055,
                      "children": [
                        {
                          "__type": "Text",
                          "__tag": 4046,
                          "value": "versionadded 1.0.0"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The input y may be either a 1-D condensed distance matrix or a 2-D array of observation vectors."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "If y is a 1-D condensed distance matrix, then y must be a "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "\\binom{n}{2}"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " sized vector, where n is the number of original observations paired in the distance matrix. The behavior of this function is very similar to the MATLAB linkage function."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "A "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "(n-1)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " by 4 matrix "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "Z"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is returned. At the "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "i"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "-th iteration, clusters with indices "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "Z[i, 0]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "Z[i, 1]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are combined to form cluster "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "n + i"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". A cluster with an index less than "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "n"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " corresponds to one of the "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "n"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " original observations. The distance between clusters "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "Z[i, 0]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "Z[i, 1]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is given by "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "Z[i, 2]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". The fourth value "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "Z[i, 3]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " represents the number of original observations in the newly formed cluster. Note that the dtype of "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "Z"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "float64"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " even though columns 0, 1 and 3 contain integer data and should be interpreted as such. This is for historical reasons."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The following linkage methods are used to compute the distance "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "d(s, t)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " between two clusters "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "s"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "t"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". The algorithm begins with a forest of clusters that have yet to be used in the hierarchy being formed. When two clusters "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "s"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "t"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " from this forest are combined into a single cluster "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "u"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "s"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "t"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are removed from the forest, and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "u"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is added to the forest. When only one cluster remains in the forest, the algorithm stops, and this cluster becomes the root."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "A distance matrix is maintained at each iteration. The "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "d[i,j]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " entry corresponds to the distance between cluster "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "i"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "j"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " in the original forest."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "At each iteration, the algorithm must update the distance matrix to reflect the distance of the newly formed cluster u with the remaining clusters in the forest."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Suppose there are "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "|u|"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " original observations "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "u[0], \\ldots, u[|u|-1]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " in cluster "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "u"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "|v|"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " original objects "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "v[0], \\ldots, v[|v|-1]"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " in cluster "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "v"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Recall, "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "s"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "t"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are combined to form cluster "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "u"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ". Let "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "v"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " be any remaining cluster in the forest that is not "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "u"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The following are methods for calculating the distance between the newly formed cluster "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "u"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and each "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "v"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "BulletList",
          "__tag": 4053,
          "ordered": false,
          "start": 1,
          "children": [
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "method='single' assigns"
                    }
                  ]
                },
                {
                  "__type": "Math",
                  "__tag": 4058,
                  "value": "d(u,v) = \\min(dist(u[i],v[j]))"
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "for all points "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "i"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " in cluster "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "u"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and   "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "j"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " in cluster "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "v"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". This is also known as the   Nearest Point Algorithm."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "method='complete' assigns"
                    }
                  ]
                },
                {
                  "__type": "Math",
                  "__tag": 4058,
                  "value": "d(u, v) = \\max(dist(u[i],v[j]))"
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "for all points "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "i"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " in cluster u and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "j"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " in   cluster "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "v"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". This is also known by the Farthest Point   Algorithm or Voor Hees Algorithm."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "method='average' assigns"
                    }
                  ]
                },
                {
                  "__type": "Math",
                  "__tag": 4058,
                  "value": "d(u,v) = \\sum_{ij} \\frac{d(u[i], v[j])}\n                          {(|u|*|v|)}"
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "for all points "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "i"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "j"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " where "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "|u|"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "   and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "|v|"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " are the cardinalities of clusters "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "u"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "   and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "v"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", respectively. This is also called the UPGMA   algorithm."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "method='weighted' assigns"
                    }
                  ]
                },
                {
                  "__type": "Math",
                  "__tag": 4058,
                  "value": "d(u,v) = (dist(s,v) + dist(t,v))/2"
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "where cluster u was formed with cluster s and t and v   is a remaining cluster in the forest (also called WPGMA)."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "method='centroid' assigns"
                    }
                  ]
                },
                {
                  "__type": "Math",
                  "__tag": 4058,
                  "value": "dist(s,t) = ||c_s-c_t||_2"
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "where "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "c_s"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "c_t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " are the centroids of   clusters "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "s"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", respectively. When two   clusters "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "s"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " are combined into a new   cluster "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "u"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", the new centroid is computed over all the   original objects in clusters "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "s"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". The   distance then becomes the Euclidean distance between the   centroid of "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "u"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and the centroid of a remaining cluster   "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "v"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " in the forest. This is also known as the UPGMC   algorithm."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "method='median' assigns "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "d(s,t)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " like the "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "centroid"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "   method. When two clusters "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "s"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " are combined   into a new cluster "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "u"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", the average of centroids s and t   give the new centroid "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "u"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". This is also known as the   WPGMC algorithm."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "method='ward' uses the Ward variance minimization algorithm.   The new entry "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "d(u,v)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is computed as follows,"
                    }
                  ]
                },
                {
                  "__type": "Math",
                  "__tag": 4058,
                  "value": "d(u,v) = \\sqrt{\\frac{|v|+|s|}\n                      {T}d(v,s)^2\n              + \\frac{|v|+|t|}\n                      {T}d(v,t)^2\n              - \\frac{|v|}\n                      {T}d(s,t)^2}"
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "where "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "u"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is the newly joined cluster consisting of   clusters "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "s"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " and "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "t"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "v"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is an unused   cluster in the forest, "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "T=|v|+|s|+|t|"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", and   "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "|*|"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is the cardinality of its argument. This is also   known as the incremental algorithm."
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Warning: When the minimum distance pair in the forest is chosen, there may be two or more pairs with the same minimum distance. This implementation may choose a different minimum than the MATLAB version."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "build-install/usr/lib/python3.14/site-packages/scipy/cluster/hierarchy/_hierarchy_impl.py",
  "item_line": 723,
  "item_type": "function",
  "aliases": [
    "scipy.cluster.hierarchy.linkage"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "from scipy.cluster.hierarchy import dendrogram, linkage\nfrom matplotlib import pyplot as plt\nX = [[i] for i in [2, 8, 0, 4, 1, 9, 9, 0]]\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "Z = linkage(X, 'ward')\nfig = plt.figure(figsize=(25, 10))\ndn = dendrogram(Z)\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "Z = linkage(X, 'single')\nfig = plt.figure(figsize=(25, 10))\ndn = dendrogram(Z)\nplt.show()\n",
        "execution_status": "success",
        "out": ""
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "2.0.0.dev0+git20260907.41eeb59",
          "kind": "assets",
          "path": "fig-827c62b0e926b953.png"
        }
      },
      {
        "__type": "Figure",
        "__tag": 4024,
        "value": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "scipy",
          "version": "2.0.0.dev0+git20260907.41eeb59",
          "kind": "assets",
          "path": "fig-864909605f46c421.png"
        }
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [
    {
      "__type": "SeeAlsoItem",
      "__tag": 4028,
      "name": {
        "__type": "CrossRef",
        "__tag": 4002,
        "value": "scipy.spatial.distance.pdist",
        "reference": {
          "__type": "RefInfo",
          "__tag": 4000,
          "module": "current-module",
          "version": "current-version",
          "kind": "to-resolve",
          "path": "scipy.spatial.distance.pdist"
        },
        "kind": "module"
      },
      "descriptions": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "pairwise distance metrics"
            }
          ]
        }
      ],
      "type": "func"
    }
  ],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "y",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "method",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "single"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "metric",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "euclidean"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "optimal_ordering",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "False"
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "linkage"
  },
  "references": [
    ".. [1] Daniel Mullner, \"Modern hierarchical, agglomerative clustering",
    "       algorithms\", :arXiv:`1109.2378v1`.",
    ".. [2] Ziv Bar-Joseph, David K. Gifford, Tommi S. Jaakkola, \"Fast optimal",
    "       leaf ordering for hierarchical clustering\", 2001. Bioinformatics",
    "       :doi:`10.1093/bioinformatics/17.suppl_1.S22`"
  ],
  "qa": "scipy.cluster.hierarchy._hierarchy_impl:linkage",
  "arbitrary": [],
  "local_refs": [
    "Z",
    "method",
    "metric",
    "optimal_ordering",
    "y"
  ]
}