SciPy - cophenet() Method



The SciPy cophenet() method calculates the cophenetic distance between each observation of the hierarchical cluster. These clusters are defined using linkage which shows the splitting of clusters.

The Cophenetic distances calculate the distance between two points, illustrated by a dendrogram. A dendrogram shows the hierarchical relationship between the objects.

Syntax

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

cophenet(Z, pdist(data))

Parameters

This method accepts the two parameters −

  • Z: This paramter store the linkage() method.
  • pdist(data): It is used to define the pairwise distribution of data.

Return value

This method return the float value as result.

Example 1

Following is basic program that illustrate the usage of SciPy cophenet() method.

import numpy as np
from scipy.cluster.hierarchy import linkage, cophenet
from scipy.spatial.distance import pdist

# given data for 2D points
data = np.array([[10, 20], [20, 30], [30, 40], [50, 60], [80, 90]])

# hierarchical clustering
Z = linkage(data, method='single')

# cophenetic correlation coefficient
c, d = cophenet(Z, pdist(data))

print(f"The value of cophenetic correlation coefficient: {c}")

Output

The above code produces the following output −

Cophenetic Correlation Coefficient: 0.8355044182110838

Example 2

This program shows the value of cophenetic correlation cofficient using the complete linkage method.

import numpy as np
from scipy.cluster.hierarchy import linkage, cophenet
from scipy.spatial.distance import pdist

# given data for 2d points
data = np.array([[10, 20], [20, 30], [30, 40], [50, 60], [80, 90]])

# perform hierarchical clustering using the 'complete' linkage method
Z_complete = linkage(data, method='complete')

# cophenetic correlation coefficient
c_complete, d_complete = cophenet(Z_complete, pdist(data))

print(f"The value of cophenetic correlation coefficient (using complete method): {c_complete}")

Output

The above code produces the following output −

The value of cophenetic correlation coefficient (using complete method): 0.7173095078886984

Example 3

Below the program illustrates the value of cophenetic correlation coefficient using the average linkage method.

import numpy as np
from scipy.cluster.hierarchy import linkage, cophenet
from scipy.spatial.distance import pdist

# given data for 2D points
data = np.array([[11, 22], [22, 33], [33, 44], [55, 66], [88, 99]])

# given data for five dimensional point
data_high_dim = np.random.rand(10, 5)

# hierarchical clustering
Z_high_dim = linkage(data_high_dim, method='average')

# cophenetic correlation coefficient
c_high_dim, d_high_dim = cophenet(Z_high_dim, pdist(data_high_dim))

print(f"The value of cophenetic correlation coefficient (high-dimensional): {c_high_dim}")

Output

The above code produces the following output −

The value of cophenetic correlation coefficient (high-dimensional): 0.6727006277242108
scipy_reference.htm
Advertisements