SciPy - complete() Method



The SciPy complete() method perform the task of complete linkage(largest point) on a condensed distance matrix.

A condensed matrix is itself an array that has collection of various methods for processing the array. Also, it defines the distance between two clusters as the largest distance between any two points say(single data point in first cluster and any single data point in the second cluster).

Syntax

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

complete(y)

Parameters

This method accepts a single parameter −

  • y: This parameter store the distance of array matrix.

Return value

This method returns the linkage matrix(result).

Example 1

Following is the SciPy complete() method that shows how to perform the complete linkage clustering on a given distance matrix and visualize output using dendrogram().

import numpy as np
from scipy.cluster.hierarchy import complete, dendrogram
import matplotlib.pyplot as plt

# Distance matrix
y = np.array([0.5, 0.2, 0.3, 0.3, 0.8, 0.6])

# complete linkage clustering
result = complete(y)

# plot the dendrogram
plt.figure(figsize=(6, 4))
dendrogram(result)
plt.title('Dendrogram - Complete Linkage')
plt.xlabel('indexes')
plt.ylabel('Distance')
plt.show()

Output

The above code produces the following result −

scipy_complete_method_one

Example 2

Below the example has given a random dataset that allows the operation of a distance matrix and performs the complete linkage clustering. Then, it uses the method dendrogram () to visualize the hierarchical clustering.

import numpy as np
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import complete, dendrogram
import matplotlib.pyplot as plt

# random data
data = np.random.rand(6, 2)

# calculate the distance matrix
y = pdist(data, metric='euclidean')

# complete linkage clustering
result = complete(y)

# plot the dendrogram
plt.figure(figsize=(6, 4))
dendrogram(result)
plt.title('Dendrogram - Complete Linkage on Random Data')
plt.xlabel('indexes')
plt.ylabel('Distance')
plt.show()

Output

The above code produces the following result −

scipy_complete_method_two

Example 3

In this example, we illustrate the task of complete linkage clustering with the custom distance metric.

import numpy as np
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import complete, dendrogram
import matplotlib.pyplot as plt

# Sample data
data = np.array([[1, 3], [2, 4], [8, 6], [7, 8]])

# calculate the distance matrix using a custom metric 
y = pdist(data, metric='cityblock')

# complete linkage clustering
result = complete(y)

# Plot the dendrogram
plt.figure(figsize=(6, 4))
dendrogram(result)
plt.title('Dendrogram - Complete Linkage with Cityblock Distance')
plt.xlabel('Sample index')
plt.ylabel('Distance')
plt.show()

Output

The above code produces the following result −

scipy_complete_method_three
scipy_reference.htm
Advertisements