
- 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 - bandwidth() Function
The SciPy's bandwidth() function is used to calculate the lower (l) and upper (u) bandwidth of a 2D numeric array.
The term Matrix Bandwidth denotes the spread of non-zero elements around the main diagonal of the matrix. Matrices with small bandwidth are known as banded matrices.
The terms l (lower bandwidth) and u (upper bandwidth) indicate how many diagonals are non-zero below and above the main diagonal. Together, l and u define the matrix's total bandwidth, which indicates how far the non-zero values stretch from the main diagonal in both directions.
This method raises Type error if the data type is unsupported for example − float16, float128 or complex256. To resolve this you can convert the matrix into supported data types such as float32, float64, int32, and int64.
The bandwidth() method is useful because knowing the smaller bandwidth of a matrix indicates faster computations and memory-efficient storage as it shows that most of the important entries of the matrix are clustered close to the diagonal.
Syntax
The syntax for the Scipy bandwidth() method is as follows −
.bandwidth(a)
Parameters
This method accepts only a single parameter −
a (ndarray) − A 2D array-like or NumPy array representing the matrix.
Return Value
This method returns a tuple containing two integers representing lower bandwidth(l) − diagonals below the main diagonal and upper bandwidth(u) − diagonals above the main diagonal.
Example 1
This is the basic example of how to use the bandwidth() method in SciPy to calculate the lower (l) and upper (u) bandwidths of a 2D square matrix, which represent the distances of the non-zero diagonals from the main diagonal.
The main diagonal in the matrix have values of 1, 4, 7, 10. We obtain 1 as a bandwidth for both lower and upper because the nonzero diagonals in the following code had lengths of one above and below to the main diagonal.
import numpy as np from scipy import linalg from scipy.linalg import bandwidth A = np.array([ [1, 2, 0, 0], [3, 4, 5, 0], [0, 6, 7, 8], [0, 0, 9, 10] ]) bw = linalg.bandwidth(A) print("Lower Bandwidth (l), Upper Bandwidth (u):", bw)
When we run above program, it produces following result
Lower Bandwidth (l), Upper Bandwidth (u): (1, 1)
Example 2
The code below calculates the lower and upper bandwidth of matrix C using bandwidth() method. It also proves that it has two non-zero rows below the diagonal with a lower bandwidth of two and no non-zero elements, above the diagonal with an upper bandwidth of zero.
import numpy as np from scipy.linalg import bandwidth C = np.array([ [1., 0., 0.], [2., 3., 0.], [4., 5., 6.] ]) lu = bandwidth(C) print(f"Lower Bandwidth: {lu[0]}, Upper Bandwidth: {lu[1]}")
Following is an output of the above code
Lower Bandwidth: 2, Upper Bandwidth: 0
Example 3
In the below example let us see the lower(l) and upper(u) bandwidth() for a upper triangle matrix. In the below code the non-zero elements are above the main diagonal in 2 rows, hence the upper bandwidth is '2' and the lower bandwidth is 0.
import numpy as np from scipy.linalg import bandwidth D = np.array([ [1., 2., 3.], [0., 4., 5.], [0., 0., 6.] ]) lu = bandwidth(D) print(f"Lower Bandwidth: {lu[0]}, Upper Bandwidth: {lu[1]}")
Output of the above code is as follows
Lower Bandwidth: 0, Upper Bandwidth: 2
Example 4
The example demonstrates how to convert the matrix to a compatible type, such as float64, in order to accommodate unsupported data types in the bandwidth() method.
import numpy as np from scipy.linalg import bandwidth # Matrix with unsupported type matrix = np.array([[1, 0, 0], [4, 5, 0], [0, 2, 3]], dtype=np.float128) # Convert to supported type and calculate bandwidth matrix = matrix.astype(np.float64) l, u = bandwidth(matrix) print(matrix) print("Lower bandwidth:", l) print("Upper bandwidth:", u)
Output of the above code is as follows
[[1. 0. 0.] [4. 5. 0.] [0. 2. 3.]] Lower bandwidth: 1 Upper bandwidth: 0