SciPy - fcluster() Method



The SciPy fcluster() method is a part of heirarchical clustering which identifies the classes of cluster(linkage matrix) with the help of unsupervised machine learning.

Below are some real-life purposes of this method −

  • It identifies the customer segmentation.
  • The most popular is genetic clustering which is used in health care sector for defining its genetic similarity.
  • It identifies the similarity of images based on demographic region.

Syntax

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

fcluster(Z, t, criterion)

Parameters

This method accepts the following parameters −

  • Z: This define the linkage matrix.
  • t: This parameter defines the number of cluster can be requested.
  • criterion: The parameter is used to define the type of flat clustering such as distance, inconsistent, and maxdists. All these clusters can be written in the form of string.

Return value

This method returns the n-dimensional array.

Example 1

Following is the basic example that illustrate the usage of fcluster() method.

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

data = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [1, 0]])
Z = linkage(data, 'ward')
clusters = fcluster(Z, t=10, criterion='distance')
print(clusters)

Output

The above code produces the following result −

[1 1 1 1 1]

Example 2

This program specifies the exact number of clusters which is t = 3 and set the maxclust to the criterion that partitioned the given data into exact 3 clusters.

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

data = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [1, 0]])
Z = linkage(data, 'ward')
res_cluster = fcluster(Z, t=3, criterion='maxclust')
print(res_cluster)

Output

The above code produces the following result −

[1 2 2 3 1]

Example 3

Below the program sets the value 'inconsistent' to the criterion within the fcluster() method and this usage define the inconsistency of matrix. So, this identifies the clusters which having less inconsistency with the value.

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

data = np.array([[11, 12], [13, 14], [15, 16], [17, 18], [11, 10]])
Z = linkage(data, 'ward')
R = inconsistent(Z)
res_cluster = fcluster(Z, t=1.15, criterion='inconsistent')
print(res_cluster)

Output

The above code produces the following result −

[1 1 1 1 1]
scipy_reference.htm
Advertisements