
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - linkage() Method
The SciPy linkage() method works on hierarchical cluster which can be used to perform the task of linkage matrix. This matrix provide the code structure of matrix data.
The hierarchical cluster is defined by separating data into group based. Following are two uses in data analysis −
- Identifying Natural Groupings: It is used to identify the grouping items with the help of natural division.
- Dendrogram Construction: This create a dendogram, it is a type of tree-diagram which records splitting sequence of data or heirarchical structure.
This method is commonly used in data analysis for grouping the similar items into clusters that can help us to understand the structure of data and make prediction.
Syntax
Following is the syntax of the SciPy linkage() method −
linkage(data, method = 'single') or, linkage(data, method = 'single', metric = 'type')
Parameters
This method accepts the following parameters −
- data: This parameter define the list of data elements in array forms.
- method = 'single': This parameter define the type of linkage algorithm.
- metric = 'type': The default type is 'euclidean'.
Return value
This method returns the linkage matrix which is a shape of numpy array(n-1, 4) where n defines the number of observation.
Example 1
Following is the SciPy linkage() method operates the linkage clustering on a custom dataset and plots the dendogram to visualize the process of data clustering.
import numpy as np from scipy.cluster.hierarchy import linkage, dendrogram import matplotlib.pyplot as plt # Sample data data = np.array([[1, 2], [2, 3], [5, 8], [8, 8]]) # Compute the linkage matrix using single linkage result = linkage(data, method='single') # Plot the dendrogram plt.figure(figsize=(8, 4)) dendrogram(result) plt.title('Dendrogram - Single Linkage') plt.xlabel('Sample index') plt.ylabel('Distance') plt.show()
Output
The above code produces the following result −

Example 2
Here, we demonstrates the complete linkage clustering on a dataset with six observations and plots the dendrogram using the Euclidean distance metric.
import numpy as np from scipy.cluster.hierarchy import linkage, dendrogram import matplotlib.pyplot as plt # Sample data data = np.array([[1, 2], [2, 3], [5, 8], [8, 8], [1, 0], [2, 1]]) # Compute the linkage matrix using complete linkage and Euclidean distance result = linkage(data, method='complete', metric='euclidean') # Plot the dendrogram plt.figure(figsize=(8, 4)) dendrogram(result) plt.title('Dendrogram - Complete Linkage') plt.xlabel('Sample index') plt.ylabel('Distance') plt.show()
Output
The above code produces the following result −

Example 3
Below the example perform the average linkage clustering using Manhattan distance on the same dataset as Example 2. Here, it use the metric type as 'cityblock'.
Note that, the Manhattan distance is measured using two points axes at right angles. It is used in high-dimensional datasets.
import numpy as np from scipy.cluster.hierarchy import linkage, dendrogram import matplotlib.pyplot as plt # Sample data data = np.array([[1, 2], [2, 3], [5, 8], [8, 8], [1, 0], [2, 1]]) # Compute the linkage matrix using average linkage and Manhattan distance result = linkage(data, method='average', metric='cityblock') # Plot the dendrogram plt.figure(figsize=(8, 4)) dendrogram(result) plt.title('Dendrogram - Average Linkage with Manhattan Distance') plt.xlabel('Sample index') plt.ylabel('Distance') plt.show()
Output
The above code produces the following result −
