{ } Raw JSON

bundles / scipy latest / scipy / cluster / hierarchy / to_mlab_linkage

function

scipy.cluster.hierarchy:to_mlab_linkage

source: /scipy/cluster/hierarchy.py :1910

Signature

def   to_mlab_linkage ( Z )

Summary

Convert a linkage matrix to a MATLAB(TM) compatible one.

Extended Summary

Converts a linkage matrix Z generated by the linkage function of this module to a MATLAB(TM) compatible one. The return linkage matrix has the last column removed and the cluster indices are converted to 1..N indexing.

Parameters

Z : ndarray

A linkage matrix generated by scipy.cluster.hierarchy.

Returns

to_mlab_linkage : ndarray

A linkage matrix compatible with MATLAB(TM)'s hierarchical clustering functions.

The return linkage matrix has the last column removed and the cluster indices are converted to 1..N indexing.

Notes

Array API Standard Support

to_mlab_linkage has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ✅                   
JAX                   ✅                     ✅                   
Dask                  ✅                     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

from scipy.cluster.hierarchy import ward, to_mlab_linkage
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]]
Z = ward(pdist(X))
Z
After a linkage matrix ``Z`` has been created, we can use `scipy.cluster.hierarchy.to_mlab_linkage` to convert it into MATLAB format:
mZ = to_mlab_linkage(Z)
mZ
The new linkage matrix ``mZ`` uses 1-indexing for all the clusters (instead of 0-indexing). Also, the last column of the original linkage matrix has been dropped.

See also

from_mlab_linkage

transform from Matlab to SciPy format.

linkage

for a description of what a linkage matrix is.

Aliases

  • scipy.cluster.hierarchy.to_mlab_linkage