This is a pre-release version (latest).Go to latest (1.18.0)
{ } Raw JSON

bundles / scipy latest / scipy / cluster / hierarchy / _hierarchy_impl / weighted

function

scipy.cluster.hierarchy._hierarchy_impl:weighted

source: build-install/usr/lib/python3.14/site-packages/scipy/cluster/hierarchy/_hierarchy_impl.py:334

Signature

def weighted(y)

Summary

Perform weighted/WPGMA linkage on the condensed distance matrix.

Extended Summary

See linkage for more information on the return structure and algorithm.

Parameters

y : ndarray

The upper triangular of the distance matrix. The result of pdist is returned in this form.

Returns

Z : ndarray

A linkage matrix containing the hierarchical clustering. See linkage for more information on its structure.

Examples

from scipy.cluster.hierarchy import weighted, fcluster
from scipy.spatial.distance import pdist
✓
First, we need a toy dataset to play with:: x x x x x x x x x x x x
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]]
✓
Then, we get a condensed distance matrix from this dataset:
y = pdist(X)
✓
Finally, we can perform the clustering:
Z = weighted(y)
✓
Z
✗
array([[ 0.     ,  1.     ,  1.     ,  2.     ],
       [ 6.     ,  7.     ,  1.     ,  2.     ],
       [ 3.     ,  4.     ,  1.     ,  2.     ],
       [ 9.     , 11.     ,  1.     ,  2.     ],
       [ 2.     , 12.     ,  1.20711,  3.     ],
       [ 8.     , 13.     ,  1.20711,  3.     ],
       [ 5.     , 14.     ,  1.20711,  3.     ],
       [10.     , 15.     ,  1.20711,  3.     ],
       [18.     , 19.     ,  3.05596,  6.     ],
       [16.     , 17.     ,  3.32379,  6.     ],
       [20.     , 21.     ,  4.06358, 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.5, criterion='distance')
fcluster(Z, 4, criterion='distance')
fcluster(Z, 6, 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.weighted