- 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.fmin() Function
scipy.optimize.fmin() is a function in SciPy's optimization module used for unconstrained optimization of a scalar function. It employs the Nelder-Mead simplex algorithm which is a heuristic search method suitable for optimizing non-smooth or noisy functions.
This function is particularly useful when derivative information is not available or the function is non-differentiable. It minimizes the objective function by iteratively improving a simplex structure in the parameter space.
Syntax
The syntax for the scipy.optimize.fmin() function is as follows −
scipy.optimize.fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None, disp=True, retall=False, callback=None)
Parameters
Here are the parameters of the scipy.optimize.fmin() function −
- func: The objective function to be minimized.
- x0: Initial guess for the parameters.
- args (optional): Additional arguments to pass to the objective function.
- xtol (optional): Relative error in the solution for termination.
- ftol (optional): Relative error in the function value for termination.
- maxiter (optional): Maximum number of iterations to perform.
- maxfun (optional): Maximum number of function evaluations to perform.
- disp (optional): Set to True to print convergence messages (default is True).
- retall (optional): Set to True to return a list of solutions at each iteration.
- callback (optional): A function called after each iteration.
Return Value
The scipy.optimize.fmin() function returns the following −
- xmin: The parameters that minimize the objective function.
- fopt: The minimum value of the objective function.
- iter: The number of iterations performed.
- funcalls: The number of function calls made.
- warnflag: Indicates if the algorithm exited without convergence.
Minimizing a Quadratic Function
Minimizing a quadratic function is a common optimization task that demonstrates the use of optimization techniques effectively. Following is an example of using scipy.optimize.fmin() to minimize a simple quadratic function −
from scipy.optimize import fmin
# Define the objective function
def objective_function(x):
return (x - 3)**2
# Initial guess
x0 = 0
# Perform the optimization
result = fmin(objective_function, x0)
# Display the result
print("Optimal value of x:", result)
Here is the output of the scipy.optimize.fmin() function which is used for minimizing the quadratic function −
Optimization terminated successfully.
Current function value: 0.000000
Iterations: 28
Function evaluations: 56
Optimal value of x: [3.]
Optimizing a Nonlinear Function
Optimizing a nonlinear function can demonstrate the versatility of scipy.optimize.fmin() for solving problems where the objective function has non-linear behavior. Here is the example of optimizing a Nonlinear Function with the help of scipy.optimize.fmin() function −
from scipy.optimize import fmin
import numpy as np
# Define the objective function
def objective_function(x):
return np.sin(x) + (x - 2)**2
# Initial guess
x0 = 0
# Perform the optimization
result = fmin(objective_function, x0)
# Display the result
print("Optimal value of x:", result)
Below is the output of the optimization process applied on a Nonlinear Function −
Optimization terminated successfully.
Current function value: 0.833970
Iterations: 27
Function evaluations: 54
Optimal value of x: [2.352125]
Optimizing a Nonlinear Multi-Modal Function
Optimizing a nonlinear multi-modal function is a great way to test the effectiveness of optimization algorithms such as scipy.optimize.fmin() especially when the function has multiple local minima and the goal is to find the global minimum. Here's an example of optimizing such a function using fmin() function −
from scipy.optimize import fmin
import numpy as np
# Define the nonlinear multi-modal objective function
def objective_function(x):
return np.sin(3 * np.pi * x) + x**2
# Initial guess
x0 = 0.5
# Perform the optimization
result = fmin(objective_function, x0)
# Display the result
print("Optimal value of x:", result)
print("Minimum value of the function:", objective_function(result))
Here is the output function scipy.optimize.fmin() which is used to optimize the nonlinear Multi-Modal Function −
Optimization terminated successfully.
Current function value: -0.755510
Iterations: 9
Function evaluations: 18
Optimal value of x: [0.48896484]
Minimum value of the function: [-0.75550985]