How to use SciPy for scientific computing, optimization, statistics, and more
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.
pip install scipy
SciPy is organized into submodules, each focused on a specific domain:
from scipy.optimize import minimize
def f(x):
return (x - 3)**2
result = minimize(f, x0=0)
print(result.x)
from scipy.optimize import root
def f(x):
return x**3 - 2*x - 5
solution = root(f, x0=2)
print(solution.x)
from scipy.integrate import quad
import math
result, error = quad(math.sin, 0, math.pi)
print(result)
from scipy import stats
data = [1, 2, 2, 3, 4, 5]
mean = stats.tmean(data)
std = stats.tstd(data)
print(mean, std)
from scipy.stats import norm
print(norm.pdf(0)) # probability density
print(norm.cdf(1.96)) # cumulative probability
from scipy import signal
import numpy as np
t = np.linspace(0, 1, 500)
square_wave = signal.square(2 * np.pi * 5 * t)
from scipy.fft import fft
import numpy as np
x = np.array([1, 2, 3, 4])
print(fft(x))
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