- 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.brute() Function
scipy.optimize.brute() is a powerful function in the SciPy library's optimization module. It performs a brute-force grid search to find the global minimum of a given function over a specified range. This method systematically evaluates the objective function on a grid of points, making it particularly useful for problems with multiple local minima where global optimization is desired.
Using this function, users can define the search space as a tuple of slices and set various parameters to control the granularity and efficiency of the search. While it is computationally intensive for high-dimensional problems, it guarantees finding the global minimum if the function is evaluated finely enough.
Syntax
The syntax for the scipy.optimize.brute() function is as follows −
scipy.optimize.brute(func, ranges, args=(), Ns=20, full_output=False, finish=None, workers=1, disp=False)
Parameters
Below are the parameters for the scipy.optimize.brute() function −
- func: The objective function to be minimized.
- ranges: A tuple of slices specifying the range for each variable. Each slice represents the search bounds.
- args: Additional arguments passed to the objective function.
- Ns: The number of grid points along each dimension and the default value is 20.
- full_output: If True then returns additional information like the function value at the minimum.
- finish: A minimization function to refine the result e.g., scipy.optimize.minimize().
- workers: The number of workers for parallel computation in which -1 uses all available processors.
- disp: If True then displays progress and convergence messages.
Return Value
The scipy.optimize.brute() function returns:
- x0: The coordinates of the global minimum.
- fval: The function value at the global minimum if full_output=True.
- grid: The evaluated grid points if full_output=True.
- Jout: The objective function values evaluated on the grid if full_output=True.
Minimizing a Quadratic Function
Here is an example of using scipy.optimize.brute() to minimize a quadratic function −
import numpy as np
from scipy.optimize import brute
# Define the objective function
def objective(x):
return x**2 + 3*x + 2
# Perform brute-force optimization
result = brute(objective, ranges=[slice(-10, 10, 0.5)], full_output=True)
# Output the result
print("Global minimum:", result[0])
print("Function value at minimum:", result[1])
Following is the output of the function scipy.optimize.brute() which is used to minimize a quadratic function −
Global minimum: [-1.5] Function value at minimum: -0.25
Optimizing a Multi-Variable Function
In this example we optimize a two-variable function by using the function scipy.optimize.brute() −
from scipy.optimize import brute
# Define a two-variable function
def func(x):
return (x[0] - 3)**2 + (x[1] + 2)**2
# Perform brute-force optimization
result = brute(func, ranges=[slice(-5, 5, 0.5), slice(-5, 5, 0.5)], full_output=True)
# Output the result
print("Global minimum:", result[0])
print("Function value at minimum:", result[1])
Below is the output of optimizing a Multi-Variable Function −
Global minimum: [3. -2.] Function value at minimum: 0.0
Using a Refinement Step
Here in this example we will optimize the function f(x)=sin(x)+0.1x2 by using scipy.optimize.brute(). After finding an approximate global minimum with brute-force search, we will refine it using a local optimizer like scipy.optimize.minimize() −
from scipy.optimize import brute, minimize
# Define the function
def func(x):
return np.sin(x) + 0.1*x**2
# Perform brute-force optimization with refinement
result = brute(func, ranges=[slice(-10, 10, 0.5)], full_output=True, finish=minimize)
# Output the result
print("Global minimum:", result[0])
print("Function value at minimum:", result[1])
Here is the output of Refinement step performed with the help of scipy.optimize.brute() function −
Global minimum: [-1.30644259] Function value at minimum: -0.7945823375576344