
- 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 - leaves_list() Function
leaves_list(z) is a scipy.cluster.hierarchy module. It helps to identify the "leaf nodes" in a hierarchical clustering, which are essentially the raw data points in their original order after forming a tree-like cluster.
Hierarchical Clustering is the process of grouping similar data points based on their features such as products by their type or cities which have similar temperature.
After clustering to know how the things are grouped we use dendrogram which is a tree-like diagram used to visualize hierarchical clustering. The leaves at the bottom show the individual data points, and as we go higher it shows combined groups, which helps to understand relationships in the data.
The leaves_list(Z) method gives the order of the leaf nodes (i.e., the original data points) in a dendrogram after hierarchical clustering. By knowing this order we can rearrange data to follow the hierarchical clustering order, making patterns or relationships clearer. To reorder, simply index your dataset with the array returned by leaves_list(Z). The concept is better understood with the help of following examples.
Syntax
Following is the syntax of the SciPy leaves_list(Z) method −
leaves_list(z)
Parameters
This method accepts only a single parameter −
Z is the linkage matrix that contains hierarchical clustering information.
Return Value
This method returns the 1D array of integers showing the order of data points(leaves) as they appear from left to right in the dendrogram. It helps you to understand the sequence of original data points in the tree.
Example 1
Following is the basic SciPy leaves_list() method that illustrates the result of order of leaf nodes in a dendogram after hierarchical clustering.
import numpy as np from scipy.cluster.hierarchy import linkage, leaves_list data = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) Z = linkage(data, method='single') leaf_order = leaves_list(Z) print("Linkage Matrix:\n", Z) print("Leaf Order:", leaf_order)
Following is the output of the above code −
Linkage Matrix: [[0. 1. 2.82842712 2.] [2. 4. 2.82842712 3. ] [3. 5. 2.82842712 4. ]] Leaf Order: [3 2 0 1]
Example 2
This example demonstrates how to reorder the dataset by using leaves_list(Z) to get the data point order after hierarchical clustering. Patterns and relationships become more clear within the dataset when the rearranged data follows the hierarchical clustering structure.
import numpy as np from scipy.cluster.hierarchy import linkage, leaves_list data = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) Z = linkage(data, method='single') leaf_order = leaves_list(Z) reordered_data = data[leaf_order] print("Leaf Order:", leaf_order) print("Reordered Data:\n", reordered_data)
Following is the output of the above code −
Leaf Order: [3 2 0 1] Reordered Data: [[7 8] [5 6] [1 2] [3 4]]
Example 3
Following is the example that shows the usage of SciPy leaves_list(Z) method. It creates a dendrogram to visualize how cities with different temperatures are grouped together
In the following output, 9, 4, 5 − index 9 [-16, -15], index 4 [-13, -12] and index 5 [-11, -11] which are grouped together first, suggesting they are more similar. The dendrogram shows how the smaller cluster merge to form larger clusters, helping us understand the temperature grouping patterns.
from scipy.cluster.hierarchy import ward, dendrogram, leaves_list from scipy.spatial.distance import pdist from matplotlib import pyplot as plt temp_in_celsius = [[37, 40], [55, 33], [21, 35], [-1, -5], [-13, -12], [-11, -11], [43, 23], [21, 20], [19, 38], [-16, -15], [-6, -10], [-9, -8]] Z = ward(pdist(temp_in_celsius)) leaf_order = leaves_list(Z) print("Order of leaves:", leaf_order) fig = plt.figure(figsize=(15, 8)) dn = dendrogram(Z) plt.title("Order of temperature by cities") plt.show()