SciPy - ward() Method



The SciPy ward() method is a part of agglomerative cluster which minimize the total cluster variance within its control. It is particularly used when you need to understand the structure of data and detect the natural grouping. This group can be performed using dendrograms which visualize the data.

This method reduce the data dimension in terms of similar cluster points.

Syntax

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

ward(distance_matrix)

Parameters

This method accepts only a single parameter −

  • distance_matrix: This paramter works on a built-in method pdist() that calculate the operation based on given data array.

Return value

This method return the linkage matrix(res) which shows the heirarchical clustering.

Example 1

Following is the SciPy ward() Method perform the task of heirarchical clustering on a small dataset and display the visualization effect using dendrogram.

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

# Sample data
data = np.array([[10, 20], [30, 40], [50, 60], [70, 80]])

# the distance matrix
distance_matrix = pdist(data)

# hierarchical clustering using ward() method
res = ward(distance_matrix)

# Plot the dendrogram
plt.figure()
dendrogram(res)
plt.show()

Output

The above code produces the following result −

scipy_ward_method_one

Example 2

In this example, we are using Iris dataset to operate the task of hierarchical clustering using ward() and dendrogram().

import numpy as np
from scipy.cluster.hierarchy import ward, dendrogram
import matplotlib.pyplot as plt
from scipy.spatial.distance import pdist
from sklearn.datasets import load_iris

# Load Iris dataset
iris = load_iris()
data = iris.data

# the distance matrix
distance_matrix = pdist(data)

# calculate the hierarchical clustering using ward() method
res = ward(distance_matrix)

# plot the dendrogram
plt.figure(figsize=(6, 4))
dendrogram(res)
plt.show()

Output

The above code produces the following result −

scipy_ward_method_two
scipy_reference.htm
Advertisements