
- 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 - linalg.leslie() Function
The linalg.leslie function in SciPy is used to compute the Leslie Matrix.
The Leslie matrix is a discrete, age-structured model of population growth that is very popular in population ecology, which is created by P.H. Leslie and named after him.
It is one of most common technique used to describe the growth of populations, which considers only one sexâtypically the femaleâand a population that is restricted to migration.
Syntax
The following is the syntax of the linalg.leslie function in SciPy −
scipy.linalg.leslie(f, s)
Parameters
The following is the list of parameters that are accepted by the scipy linalg.leslie() function −
- f − This parameter takes an array as an input which represents the "fecundity" coefficients.
- s − The survival coefficient has to be 1-dimensional. Note that the length of each slice 's' must be one less than the length of f, and it must be at least 1.
Return
The scipy linalg.leslie() function takes the above parameter and returns an array. This array is zero expect the first row, which represents the f, and the first sub diagonal, which is the s. f[0] + s[0] is the data type of this array.
Example 1
In the example below, we use the linalg.leslie() function in SciPy by passing both the required parameters 'f' and 's'. The function returns an array that represents the Leslie matrix for the given input values.
from scipy.linalg import leslie res=leslie([2,4,6,8], [1,2,3]) print('The leslie matrix for the given input is:') print(res)
The output for the above code is as follows −
The leslie matrix for the given input is: [[2 4 6 8] [1 0 0 0] [0 2 0 0] [0 0 3 0]]
Example 2
Here we pass both the required parameters f and s, where the value of s is greater than f-1 to the linalg.leslie() function in Scipy and find out the output.
from scipy.linalg import leslie res=leslie([2,4,6,8], [1,2,3,4]) print('The leslie matrix for the given input is:') print(res)
The code above returns an error message as the leslie function expects the survival probabilities to be one element shorter than the fertilities as given below −
ValueError Traceback (most recent call last) Cell In[1], line 2 1 from scipy.linalg import leslie ----> 2 res=leslie([2,4,6,8], [1,2,3,4]) 3 print('The leslie matrix for the given input is:') 4 print(res) File /lib/python3.12/site-packages/scipy/linalg/_special_matrices.py:294, in leslie(f, s) 292 raise ValueError("Incorrect shape for s. s must be 1D") 293 if f.size != s.size + 1: --> 294 raise ValueError("Incorrect lengths for f and s. The length" 295 " of s must be one less than the length of f.") 296 if s.size == 0: 297 raise ValueError("The length of s must be at least 1.") ValueError: Incorrect lengths for f and s. The length of s must be one less than the length of f.
Example 3
In the following example code, we pass both the required parameter f and s, such that the value of s is less than f-1 to the scipy.linalg.leslie() function.
from scipy.linalg import leslie res=leslie([2,4,6,8], [1,2]) print('The leslie matrix for the given input is:') print(res)
The output for the above code is an error as the leslie function in scipy.linalg requires the lengths of the f and s arguments to be compatible to construct a valid Leslie matrix −
ValueError Traceback (most recent call last) Cell In[2], line 2 1 from scipy.linalg import leslie ----> 2 res=leslie([2,4,6,8], [1,2]) 3 print('The leslie matrix for the given input is:') 4 print(res) File /lib/python3.12/site-packages/scipy/linalg/_special_matrices.py:294, in leslie(f, s) 292 raise ValueError("Incorrect shape for s. s must be 1D") 293 if f.size != s.size + 1: --> 294 raise ValueError("Incorrect lengths for f and s. The length" 295 " of s must be one less than the length of f.") 296 if s.size == 0: 297 raise ValueError("The length of s must be at least 1.") ValueError: Incorrect lengths for f and s. The length of s must be one less than the length of f.