
- 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 - to_tree() Function
The SciPy to_tree() method is used to convert linkage matrix(z) to a tree representation.
This method creates a hierarchical tree and nodes where, each node has attributes id(unique identifier), count(number of data points), left and right(child nodes), and dist(merging distance between child nodes) and helps you to understand and manipulate hierarchical clusterings more flexibly, as it gives a tree-like structure instead of just a matrix.
In hierarchical clustering, the tree is a data structure that represents how data points are grouped together. Each node in the tree represents either a single data point(leaf) and larger cluster formed by merging smaller clusters.
Syntax
Following is the syntax of the SciPy to_tree() method −
.to_tree(z)
Parameters
This method accepts two parameters −
-
Z: Linkage matrix created by methods like ward, complete, single, etc. It contains information about how clusters are merged.
-
rd(optional): If rd = False (default) only the root node of the tree is returned and when rd = True it gives a tuple containing a root node of the tree and a list of ClusterNodes.
Return Value
This method returns ClusterNode when rd = False and returns tuple(r,d) when rd = True where 'r' refers to the root node which is the top-most node of the tree and 'd' refers to list of ClusterNode object in the tree.
Example 1
Following is the example that shows the usage of SciPy to_tree(Z[, rd]) method where rd is false by default.
This is useful when you want details of high-level attributes of the clustering like root cluster's size and structure.
import numpy as np from scipy.cluster import hierarchy from scipy.cluster.hierarchy import to_tree #sample data data = [[30, 34], [67, 66], [31, 32], [70, 68]] #hierarchical clustering Z = hierarchy.linkage(data) #matrix to tree root_node = to_tree(Z) # Access root node attributes print("Root Node ID:", root_node.id) print("Number of points in root cluster:", root_node.count) print("Distance at root:", root_node.dist) # Access children of root node print("Left child ID:", root_node.left.id) print("Right child ID:", root_node.right.id)
Following is an output of the above code −
Root Node ID: 6 Number of points in root cluster: 4 Distance at root: 48.91829923454004 Left child ID: 4 Right child ID: 5
Example 2
Following is the example that shows the usage of SciPy to_tree(Z[, rd]) method where rd is True.
This is useful when u need detailed access to all nodes in the clustering tree, enabling in-depth exploration of the hierarchical structure.
import numpy as np from scipy.cluster import hierarchy from scipy.cluster.hierarchy import to_tree # Data data = [[2, 3], [3, 4], [5, 8], [10, 12], [15, 18], [20, 25]] # Hierarchical clustering Z = hierarchy.linkage(data, method='average') # Converting to tree structure with rd=True root_node, node_dict = to_tree(Z, rd=True) # Root node information print("Root node ID:", root_node.id) print("Number of nodes in the tree:", len(node_dict)) # Node dictionary is actually a list # Listing node IDs print("Node IDs in the tree:", [node.id for node in node_dict]) print("Cluster count in root node:", root_node.count)
Output of the above code is as follows −
Root node ID: 10 Number of nodes in the tree: 11 Node IDs in the tree: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Cluster count in root node: 6