Python SciPy Ndimage Imread: Image Processing

scipy imread

Recently, I was working on an image analysis project where I needed to read and manipulate images using Python. This is when I discovered the power of SciPy’s ndimage module and its imread function. The ndimage module offers a comprehensive suite of image processing tools, with imread being particularly useful for reading images as NumPy … Read more >>

scipy.optimize.minimize: Methods, Bounds and Constraints

Python SciPy minimize: the Command Prompt comparing how many function evaluations each method needs

scipy.optimize.minimize finds the input that makes a function as small as possible: The starting guess matters, and so does result.success. A failed run still hands back an x, and it means nothing. All output below is from real runs on Python 3.12.5, SciPy 1.18.1, NumPy 2.5.3. Using scipy.optimize.minimize Pass a function and a starting point. … Read more >>

How to Load MAT Files in Python SciPy

python load mat file

Working with MATLAB files in Python projects is a common challenge I face in data science workflows. When collaborating with MATLAB users or working with legacy scientific datasets, knowing how to properly load and manipulate .mat files becomes essential. In this article, I’ll walk you through several practical methods to load MAT files in Python … Read more >>

SciPy linprog: Linear Programming in Python

SciPy linprog: the Command Prompt solving a production planning problem and printing the optimal number of chairs, tables and the profit

linprog in SciPy solves linear programming problems: minimise a linear objective subject to linear constraints. Three things about it surprise people, and all three are in the first example: it only minimises, every variable is non-negative by default, and constraints must be written as <=. I solved each of these on SciPy 1.18.1 on Python … Read more >>

Python SciPy Leastsq: Solve Non-Linear Least Squares Problems

scipy optimize least squares

I was working on a data analysis project where I needed to fit a complex exponential decay model to experimental measurements. The challenge was that my model was non-linear, making traditional linear regression methods unsuitable. This is where SciPy’s leastsq function came to the rescue. In this article, I’ll share how to use SciPy’s leastsq … Read more >>

Scipy KDTree: Nearest Neighbor Searches in Python

kdtree query

Recently, I was working on a project where I needed to find the nearest points in a multidimensional dataset quickly. The brute force approach of calculating distances between all points was painfully slow with large datasets. That’s when I discovered Scipy’s KDTree, which dramatically improved my search efficiency. In this article, I’ll share how to … Read more >>

Python SciPy Interpolate

scipy interpolate

When working with real-world data, it’s common to encounter situations where you need to estimate values between known data points. This is where interpolation comes in handy, and Python’s SciPy library offers powerful tools for this purpose. Over my decade-plus journey as a Python developer, I’ve found SciPy’s interpolation functions to be invaluable for various … Read more >>

How to Use Python Scipy Gaussian_KDE?

gaussian_kde python

Recently, I was working on a data analysis project where I needed to estimate the probability density function of a dataset. The issue is, traditional histograms weren’t giving me the smooth representation I needed. This is where Gaussian Kernel Density Estimation (KDE) from SciPy came to the rescue. In this article, I’ll cover everything you … Read more >>

Python Scipy Gamma

gamma function python

In my decade-plus of Python development, I’ve frequently needed to work with statistical distributions for data analysis and modeling. The gamma distribution is particularly useful for modeling skewed data that can’t go below zero, perfect for analyzing things like rainfall amounts, insurance claims, or service times. SciPy makes working with gamma distributions remarkably simple, but … Read more >>

scipy.signal.freqz: Plot a Filter’s Frequency Response

Python SciPy freqz: the magnitude response of a Butterworth filter plotted in decibels

scipy.signal.freqz computes the frequency response of a digital filter from its coefficients. It returns two arrays: h is complex, which is the detail that trips most people up. You take abs(h) for magnitude and np.angle(h) for phase. Every number and plot below came from running the code on Python 3.12.5, SciPy 1.18.1, NumPy 2.5.3, Matplotlib … Read more >>