← Back to Chapters

SciPy Tutorial

? SciPy Tutorial

? Quick Overview

SciPy is a powerful Python library for scientific and technical computing. It is built on top of NumPy and provides advanced tools for integration, optimization, linear algebra, signal processing, and more.

In this topic, you will learn how to:

  • Install and import SciPy in your Python environment.
  • Use scipy.integrate for numerical integration.
  • Use scipy.optimize for optimization tasks.
  • Use scipy.linalg for linear algebra operations.

? Key Concepts

  • NumPy-based: SciPy functions operate primarily on NumPy arrays.
  • Modular structure: Different domains are separated into submodules such as integrate, optimize, and linalg.
  • Scientific workflows: Common scientific tasks like solving equations, minimizing functions, and computing integrals are provided out-of-the-box.
  • High-level interface: Many complex algorithms are exposed in a simple, Pythonic way.

? Syntax and Modules

You typically install SciPy using pip, then import the specific submodules you need in your script.

  • scipy.integrate → Numerical integration, ODE solvers.
  • scipy.optimize → Minimization, curve fitting, root finding.
  • scipy.linalg → Advanced linear algebra routines.
? Installing SciPy
# Install SciPy
pip install scipy

? Code Examples

? Numerical Integration with scipy.integrate

Use integrate.quad to compute the definite integral of a function over an interval.

? View Integration Example
from scipy import integrate

# Define a function
def f(x):
    return x**2

# Compute definite integral from 0 to 1
result, error = integrate.quad(f, 0, 1)
print(result)  # Expected output: 0.33333333333333337

? Optimization with scipy.optimize

Use minimize to find the minimum value of a function.

? View Optimization Example
from scipy.optimize import minimize

# Define the function to minimize
def f(x):
    return (x - 3)**2

# Call minimize starting from initial guess 0
result = minimize(f, 0)
print(result.x)  # Expected output: [3.]

? Linear Algebra with scipy.linalg

Use linalg.solve to solve systems of linear equations.

? View Linear Algebra Example
from scipy import linalg
import numpy as np

# Create coefficient matrix A and right-hand side vector b
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])

# Solve the linear system Ax = b
x = linalg.solve(A, b)
print(x)  # Expected output: [-4.   4.5]

? Output and Explanation

? What the examples do

  • Integration example: Computes the integral of f(x) = x² from 0 to 1. The analytical result is 1/3, and SciPy returns a value close to 0.33333333333333337 plus an estimate of the numerical error.
  • Optimization example: Minimizes (x - 3)², which has its minimum at x = 3. The minimize function starts from an initial guess (0) and iteratively finds the minimum, returning [3.] as the solution.
  • Linear algebra example: Solves the system Ax = b where A = [[1, 2], [3, 4]] and b = [5, 6]. The result [-4. 4.5] is the vector x that satisfies the equations defined by A and b.

? Tips and Best Practices

  • Use SciPy when you need advanced scientific computations beyond basic NumPy operations.
  • Make sure you are comfortable with NumPy arrays, as they are the core data structure used by SciPy.
  • Import only the submodules you need, such as from scipy import integrate or from scipy import linalg, to keep your code clear.
  • Read the official SciPy documentation for detailed information about modules like integrate, optimize, and linalg.
  • Pay attention to returned values: some functions return tuples (e.g., value and error estimate) that you should unpack correctly.

? Try It Yourself

  • Use scipy.integrate.quad to compute the integral of sin(x) between 0 and π.
  • Use scipy.optimize.minimize to find the minimum of a different function, for example (x + 2)² + 1.
  • Create a 3×3 matrix and a vector b, then solve the system Ax = b using scipy.linalg.solve.
  • Explore other submodules such as scipy.stats or scipy.signal and try a basic function from each.