
- 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 optimize.minimize() Function
The scipy.optimize.minimize() function is used to minimize a scalar objective function. It supports various optimization algorithms which includes gradient-based methods such as BFGS, L-BFGS-B and derivative-free methods like Nelder-Mead.
This function can handle both unconstrained and constrained optimization problems. We can specify options like bounds, constraints or a custom gradient. The result includes the optimal solution, function value, success status and additional details about the optimization process.
This function is useful for solving various optimization problems such as engineering, machine learning and economics.
Syntax
Following is the syntax of the function scipy.optimize.minimize() to perform minimization in SciPy −
scipy.optimize.minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None)
Parameters
Below are the parameters of the scipy.optimize.minimize() function −
- fun (callable): The objective function to minimize.
- x0 (array-like): Initial guess for the variables.
- method (string, optional): Optimization method such as 'BFGS', 'Nelder-Mead', etc.
- jac (callable, optional): Gradient of the objective function.
- bounds (sequence, optional): Bounds for variables like [(0, 1), (-1, 1)]).
- constraints (dict, optional): Constraints for the optimization such as {'type': 'eq', 'fun': constraint_function}.
- tol (float, optional): Tolerance for stopping criteria.
- callback (callable, optional): A function to monitor the optimization process.
- options (dict, optional): Solver-specific options such as maximum iterations.
Return Value
The scipy.optimize.minimize() function returns an object with the following attributes −
- x: The optimal solution.
- fun: The function value at the optimum.
- success: Boolean indicating if the optimization was successful.
- message: A message describing the termination reason.
- jac: The gradient at the optimum if provided.
Example of Basic Minimization
Following is an example that minimizes the quadratic function ( f(x) = x^2 + x + 2 ) using the scipy.optimize.minimize() function −
from scipy.optimize import minimize # Objective function def objective(x): return x**2 + x + 2 # Initial guess x0 = [0] # Minimize the function result = minimize(objective, x0) # Display results print("Optimal solution:", result.x) print("Function value at optimum:", result.fun)
Here is the output of the scipy.optimize.minimize() function for basic minimization −
Optimal solution: [-0.50000001] Function value at optimum: 1.75
Minimization with Bounds
This example minimizes f(x) = x^2 + x + 2 ) within the bounds (0, 1) −
from scipy.optimize import minimize # Objective function def objective(x): return x**2 + x + 2 # Bounds bounds = [(0, 1)] # Initial guess x0 = [0.5] # Minimize the function with bounds result = minimize(objective, x0, bounds=bounds) # Display results print("Optimal solution:", result.x) print("Function value at optimum:", result.fun)
Following is the output of the scipy.optimize.minimize() function with bounds −
Optimal solution: [0.] Function value at optimum: 2.0
Minimization with Constraints
This example minimizes f(x, y) = x^2 + y^2) with the constraint (x + y = 1) −
from scipy.optimize import minimize # Objective function def objective(x): return x[0]**2 + x[1]**2 # Constraint: x + y = 1 def constraint(x): return x[0] + x[1] - 1 # Constraints dictionary constraints = {'type': 'eq', 'fun': constraint} # Initial guess x0 = [0.5, 0.5] # Minimize the function with constraints result = minimize(objective, x0, constraints=constraints) # Display results print("Optimal solution:", result.x) print("Function value at optimum:", result.fun)
Here is the output of the scipy.optimize.minimize() function with constraints −
Optimal solution: [0.5 0.5] Function value at optimum: 0.5