SciPy Tutorial

Master scientific computing with SciPy - numerical operations, optimization, and more

SciPy Numerical Computing Python

Introduction to SciPy

SciPy is an open-source Python library used for scientific and technical computing. It builds on NumPy and provides additional functionality for optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers, and other tasks common in science and engineering.

Installation

pip install scipy numpy

SciPy requires NumPy, so make sure to install both packages.

Numerical Operations

SciPy provides various numerical operations that build upon NumPy's capabilities.

import numpy as np
from scipy import special

# Example: Computing special functions
x = np.linspace(0, 10, 100)
y = special.jn(0, x)  # Bessel function of the first kind

Optimization

SciPy's optimization module provides various algorithms for finding minima of functions.

from scipy.optimize import minimize

def objective(x):
    return x[0]**2 + x[1]**2

# Minimize the function
result = minimize(objective, [1, 1])
print(result.x)  # [0. 0.]

Integration

SciPy can perform both single and multiple integrals.

from scipy.integrate import quad

# Single integral
result, error = quad(lambda x: x**2, 0, 1)
print(result)  # 0.33333333333333337

Interpolation

SciPy provides various interpolation methods for data analysis.

from scipy.interpolate import interp1d
import numpy as np

x = np.linspace(0, 10, 10)
y = np.sin(x)

# Create interpolation function
f = interp1d(x, y)

# Interpolate at new points
new_x = np.linspace(0, 10, 50)
new_y = f(new_x)

Linear Algebra

SciPy extends NumPy's linear algebra capabilities.

from scipy.linalg import eig
import numpy as np

# Create matrix
A = np.array([[1, 2], [3, 4]])

# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = eig(A)
print(eigenvalues)  # [ 5.37228132 -0.37228132]

Statistics

SciPy's stats module provides a wide range of statistical functions.

from scipy.stats import norm

# Generate normal distribution
mu, sigma = 0, 0.1
s = np.random.normal(mu, sigma, 1000)