Python - SciPy
SciPy is an open-source Python library used for scientific and technical computing. Built on NumPy, SciPy provides advanced mathematical, scientific, and engineering tools such as optimization, integration, interpolation, eigenvalue problems, and more.
This tutorial introduces SciPy and its core features to get you started.
What is SciPy?
SciPy is short for Scientific Python.
It builds upon NumPy and offers higher-level modules for a wide range of scientific tasks.
Some of its main submodules include:
scipy.optimize (Optimization)
scipy.integrate (Integration)
scipy.linalg (Linear Algebra)
scipy.stats (Statistics)
scipy.fft (Fast Fourier Transform)
scipy.spatial (Spatial data structures)
Installation
Install SciPy using pip:
pip install scipy
Getting Started
To use SciPy, import it as follows:
import scipy
from scipy import optimize, integrate, stats
SciPy Submodules
1. Optimization (scipy.optimize)
Used to solve optimization problems such as finding the minimum or maximum of a function.
Example: Minimize a Function
from scipy.optimize import minimize
# Define the function to minimize
def objective_function(x):
return x**2 + 5 * x + 6
# Minimize the function
result = minimize(objective_function, x0=0)
print("Minimum value:", result.fun)
print("At x =", result.x)
2. Integration (scipy.integrate)
Used to compute definite integrals of functions.
Example: Integrate a Function
from scipy.integrate import quad
# Define the function to integrate
def f(x):
return x**2
# Integrate the function from 0 to 1
result, error = quad(f, 0, 1)
print("Integral:", result)
3. Linear Algebra (scipy.linalg)
Provides tools for matrix operations, eigenvalues, and decompositions.
Example: Solving Linear Equations
Solve the system of equations:
2