
- 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 - Unconstrained Optimization
Unconstrained Optimization in SciPy
Unconstrained optimization in SciPy refers to the process of finding the minimum or maximum of an objective function without any restrictions or constraints on the variables. Unconstrained optimization is typically performed using the scipy.optimize.minimize() function which provides a wide range of algorithms suited to different types of optimization problems. Lets go into detail about how this works along with different aspects.
Syntax
Following is the syntax of the function scipy.optimize.minimize() which is used to find the minimum or maximum of an objective function −
scipy.optimize.minimize( fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None )
Parameters
Here are the Parameters of scipy.optimize.minimize() function −
- fun: Objective function to minimize
- x0: Initial guess for the variables.
- method: Optimization algorithm. There are different methods such as 'BFGS', 'Nelder-Mead', 'L-BFGS-B', 'trust-constr'.
- jac(optional): Gradient of objetcive function.
- hess(optional): Hessian of objective function.
- bounds: Variable bounds for constrained problems
- constraints: Equality or inequality constraints.
- options: Solver-specific settings.
Basic Minimization
Following is the basic Minimization example in which we will minimize a simple quadratic function f(x) = x2+x+2 −
> 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 basic minimization by using the function scipy.optimize.minimize() −
Optimal solution: [-0.50000001] Function value at optimum: 1.75
Minimization with Variable Bounds
When variables have specific bounds we can define them using the bounds parameter in scipy.optimize.minimize() function. This is useful for constrained problems. The following example shows how to minimize a function with variable bounds −
> from scipy.optimize import minimize # Objective function def objective(x): return x[0]**2 + x[1]**2 # Bounds on the variables bounds = [(0, 1), (-1, 1)] # x in [0, 1], y in [-1, 1] # Initial guess x0 = [0.5, 0] # A point within the bounds # Minimization result = minimize(objective, x0, method='L-BFGS-B', bounds=bounds) # Output print("Optimal solution:", result.x) print("Function value at optimum:", result.fun)
Here is the output of the function scipy.optimize.minimize() used with bounds parameter −
Optimal solution: [ 0.00000000e+00 -1.11022301e-08] Function value at optimum: 1.2325951233541654e-16
Optimization methods in minimize() Function
The scipy.optimize.minimize() function supports a variety of optimization methods in which each tailored for specific types of problems such as unconstrained or constrained optimization, gradient-based or gradient-free methods and problems with bounds.
Method | Description | Type | Gradient Needed? | Hessian Needed? | Use Case |
---|---|---|---|---|---|
Nelder-Mead | Simplex algorithm that minimizes based only on function values. | Gradient-free | No | No | Non-smooth functions, small problems |
Powell | Directional search algorithm optimizing along chosen directions. | Gradient-free | No | No | Non-smooth functions, high dimensions |
CG | Conjugate Gradient method minimizing quadratic approximation of functions. | Gradient-based | Yes | No | Smooth functions, large-scale problems |
BFGS | Quasi-Newton method approximating the inverse Hessian. | Gradient-based | Yes | Approximation | Smooth functions, efficient optimization |
Newton-CG | Newtons method with conjugate gradient to improve efficiency. | Gradient-based | Yes | Optional | Large-scale problems with Hessian-vector products |
trust-ncg | Trust-region Newton-Conjugate Gradient method. | Trust-region, Newton's | Yes | Approximation | Large-scale problems |
trust-krylov | Trust-region method using Krylov subspace approximation of Hessians. | Trust-region, Krylov-based | Yes | Approximation | Large-scale problems |
trust-exact | Trust-region method leveraging exact Hessians for precise solutions. | Trust-region, Newton's | Yes | Yes | Small-scale problems with exact Hessians |
dogleg | Trust-region method that uses a dogleg step for solving sub-problems. | Trust-region, Newton's | Yes | Yes | Medium-scale problems with exact Hessians |