SciPy - leaders() Method



The SciPy leaders() method returns the root nodes of heirarchical clustering. This is based on unsupervised machine learning algorithm which is used for cluster data points.

Syntax

Following is the syntax of the SciPy leaders() Method −

leaders(Z, clusters)

Parameters

This method accepts the following parameters −

  • Z: This parameter store the method named linkage().
  • clusters: This parameter is used to store method fcluster() which works on three parameters namely Z, t and criterion.

Return value

This method returns the n-dimensional array.

Example 1

Following is the basic example that illustrates the usage of SciPy leaders() method.

from scipy.cluster.hierarchy import linkage, fcluster, leaders
import numpy as np

inp_data = np.array([[10, 20], [40, 50], [50, 60], [70, 80], [10, 0]])
Z = linkage(inp_data, 'ward')
clusters = fcluster(Z, t=10, criterion='distance')
leader_indices, counts = leaders(Z, clusters)
print("The result of leader indices:", leader_indices)
print("The cluster sizes:", counts)

Output

The above code produces the following result −

The result of leader indices: [0 4 1 2 3]
The cluster sizes: [1 2 3 4 5]

Explanation of output in detail manner −

Here, leader indices by default sort the order of given data input. To understand its cluster forms then look into the below data indices −

  • leader for cluster 1: The data index 0 at [10, 20].
  • leader for cluster 2: The data index 4 at [10, 0].
  • leader for cluster 3: The data index 1 at [40, 50].
  • leader for cluster 4: The data index 2 at [50, 60].
  • leader for cluster 5: The data index 3 at [70, 80].
scipy_reference.htm
Advertisements