bundles / scipy latest / scipy / cluster / hierarchy / _hierarchy_impl / ward
function
scipy.cluster.hierarchy._hierarchy_impl:ward
source: build-install/usr/lib/python3.14/site-packages/scipy/cluster/hierarchy/_hierarchy_impl.py:623
Signature
def ward(y)Summary
Perform Ward's linkage on a condensed distance matrix.
Extended Summary
See linkage for more information on the return structure and algorithm.
The following are common calling conventions:
Z = ward(y)Performs Ward's linkage on the condensed distance matrixy.Z = ward(X)Performs Ward's linkage on the observation matrixXusing Euclidean distance as the distance metric.
Parameters
y: ndarrayA condensed distance matrix. A condensed distance matrix is a flat array containing the upper triangular of the distance matrix. This is the form that
pdistreturns. Alternatively, a collection of m observation vectors in n dimensions may be passed as an m by n array.
Returns
Z: ndarrayThe hierarchical clustering encoded as a linkage matrix. See linkage for more information on the return structure and algorithm.
Examples
from scipy.cluster.hierarchy import ward, fcluster from scipy.spatial.distance import pdist✓
X = [[0, 0], [0, 1], [1, 0], [0, 4], [0, 3], [1, 4], [4, 0], [3, 0], [4, 1], [4, 4], [3, 4], [4, 3]]✓
y = pdist(X)
✓Z = ward(y)
✓Z
✗array([[ 0. , 1. , 1. , 2. ],
[ 3. , 4. , 1. , 2. ],
[ 6. , 7. , 1. , 2. ],
[ 9. , 10. , 1. , 2. ],
[ 2. , 12. , 1.29099, 3. ],
[ 5. , 13. , 1.29099, 3. ],
[ 8. , 14. , 1.29099, 3. ],
[11. , 15. , 1.29099, 3. ],
[16. , 17. , 5.7735 , 6. ],
[18. , 19. , 5.7735 , 6. ],
[20. , 21. , 8.16497, 12. ]])
The linkage matrix ``Z`` represents a dendrogram - see
`scipy.cluster.hierarchy.linkage` for a detailed explanation of its
contents.
We can use `scipy.cluster.hierarchy.fcluster` to see to which cluster
each initial point would belong given a distance threshold:
fcluster(Z, 0.9, criterion='distance') fcluster(Z, 1.1, criterion='distance') fcluster(Z, 3, criterion='distance') fcluster(Z, 9, criterion='distance')✓
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)Also, `scipy.cluster.hierarchy.dendrogram` can be used to generate a plot of the dendrogram.
See also
- linkage
for advanced creation of hierarchical clusterings.
- scipy.spatial.distance.pdist
pairwise distance metrics
Aliases
scipy.cluster.hierarchy.ward