Selected Reading

SciPy - inconsistent() Method



The SciPy inconsistent() method is used to perform the calculation of inconsistency statistics on a linkage matrix. We can also say it as by providing useful insight into different levels of hierarchical clustering.

Syntax

Following is the syntax of the SciPy inconsistent() method −

inconsistent(Z)

Parameters

This function accepts only a single parameter −

  • Z: This parameter determine the linkage matrix.

Return value

This method returns the n-dimensional array.

Example 1

Following is the basic usage of SciPy inconsistent() method.

from scipy.cluster.hierarchy import linkage, inconsistent
import numpy as np

# given data
inp = np.array([[1, 2], [2, 3], [3, 4], [5, 6], [8, 9]])

# hierarchical clustering
Z = linkage(inp, method = 'single')
# inconsistency statistics
res = inconsistent(Z)
print(res)

Output

The above code produces the following output −

[[1.41421356 0.         1.         0.        ]
 [1.41421356 0.         2.         0.        ]
 [2.12132034 1.         2.         0.70710678]
 [3.53553391 1.         2.         0.70710678]]

Example 2

The program perform the random datasets that shows heirarchical clustering using complete linkage method. Here, we are inserting the statistical data(d = 4).

from scipy.cluster.hierarchy import linkage, inconsistent
import numpy as np

# given data
X = np.random.rand(10, 2)

# hierarchical clustering
Z = linkage(X, method = 'complete')

# inconsistency statistics with depth 3
res = inconsistent(Z, d = 4)
print(res)

Output

The above code produces the following output −

[[0.11686734 0.         1.         0.        ]
 [0.12320749 0.         1.         0.        ]
 [0.15625215 0.05569854 2.         0.70710678]
 [0.25072888 0.         1.         0.        ]
 [0.24700603 0.12197973 3.         0.98439051]
 [0.2431393  0.15556122 3.         1.1170798 ]
 [0.33971115 0.21046693 4.         1.32142073]
 [0.38888022 0.31795366 4.         1.37511476]
 [0.46547574 0.29610456 8.         1.55631532]]
scipy_reference.htm
Advertisements