Lesson 29: Introduction to SciPy

How to use SciPy for scientific computing, optimization, statistics, and more

What Is SciPy?

SciPy is a scientific computing library built on top of NumPy. It provides advanced tools for mathematics, optimization, statistics, signal processing, and scientific algorithms. It is widely used in engineering, physics, machine learning, and research.

Installing SciPy

pip install scipy

Basic Usage

SciPy is organized into submodules, each focused on a specific domain:

Optimization Example

from scipy.optimize import minimize

def f(x):
    return (x - 3)**2

result = minimize(f, x0=0)
print(result.x)

Root Finding

from scipy.optimize import root

def f(x):
    return x**3 - 2*x - 5

solution = root(f, x0=2)
print(solution.x)

Numerical Integration

from scipy.integrate import quad
import math

result, error = quad(math.sin, 0, math.pi)
print(result)

Working with Statistics

from scipy import stats

data = [1, 2, 2, 3, 4, 5]

mean = stats.tmean(data)
std = stats.tstd(data)

print(mean, std)

Probability Distributions

from scipy.stats import norm

print(norm.pdf(0))      # probability density
print(norm.cdf(1.96))   # cumulative probability

Signal Processing Example

from scipy import signal
import numpy as np

t = np.linspace(0, 1, 500)
square_wave = signal.square(2 * np.pi * 5 * t)

Fourier Transforms

from scipy.fft import fft
import numpy as np

x = np.array([1, 2, 3, 4])
print(fft(x))

Why SciPy Matters

Next Steps

Now that you understand SciPy, you're ready to explore machine learning fundamentals in Lesson 30: Introduction to Machine Learning Concepts.

← Back to Lesson Index