Python SciPy Butterworth Filter

butterworth filter python

Recently, I was working on a signal processing project where I needed to remove noise from sensor data collected from a traffic monitoring system in downtown Chicago. The challenge was filtering out random fluctuations while preserving the important traffic pattern information. This is where the Butterworth filter in SciPy came to my rescue. In this … Read more >>

Scipy’s Lil_Matrix

lil_matrix

Recently, I was working on a machine learning project that involved a large dataset with mostly zero values. When I tried to process this data using regular NumPy arrays, my computer almost crashed due to memory limitations. That’s when I discovered the power of sparse matrices in SciPy, particularly the LIL (List of Lists) matrix … Read more >>

@typescript-eslint/no-unused-vars: Underscore, Disable and Options

typescript-eslint no-unused-vars: the terminal showing that an underscore-prefixed parameter is still reported as an error

@typescript-eslint/no-unused-vars reports variables, parameters, imports and types that you declared and never used. The usual advice is to rename the thing with a leading underscore, and on a default setup that does not work: The underscore convention has to be switched on. This guide shows the configuration that enables it, along with the inline comments … Read more >>

SciPy curve_fit in Python: maxfev, bounds and p0

SciPy curve_fit: the Command Prompt showing the RuntimeError that the number of calls to function has reached maxfev

SciPy curve fit takes a model function and some data, then finds the parameter values that make the model match. It lives in SciPy‘s optimize module: popt comes back with the fitted parameters and pcov with their covariance. The one thing that goes wrong more than anything else is RuntimeError: Optimal parameters not found, which … Read more >>

Python Split String by Character: Into Characters or on a Delimiter

Python split string by character: the Command Prompt comparing list() with split() on the same string

“Python split string by character” is asked about two completely different jobs, which is why the answers people find so often miss. One means breaking a string into its individual characters. The other means cutting it at a particular character: list() handles the first. str.split() handles the second. Both are covered below, along with the … Read more >>

NumPy Shape in Python: shape[0], shape[1] and the Tuple

NumPy shape: the Command Prompt showing shape[0] as the row count and shape[1] as the column count

NumPy shape is an attribute, not a function. It hands you a tuple with one entry per dimension, and you read it with no parentheses at all: That single detail explains most of the errors people hit with it. The rest of this guide covers what the tuple means, what shape[0] and shape[1] actually count, … Read more >>

Convert a String to datetime in Python (strptime and ISO)

Python convert a string to datetime: the Command Prompt showing the time data does not match format error

datetime.strptime converts a string to a datetime in Python, using a format that describes your text exactly: Reach for fromisoformat whenever the text is already ISO 8601. It needs no format string and it is dramatically faster. All output below is from real runs on Python 3.12.5 on Windows. Converting a string to datetime with … Read more >>

PyTorch Softmax: dim, log_softmax and CrossEntropyLoss

PyTorch softmax: the Command Prompt showing the wrong loss produced by applying softmax before CrossEntropyLoss

torch.softmax turns raw model scores into probabilities that add up to 1: Two things go wrong with it constantly. Picking the wrong dim normalises across the batch instead of across the classes, and applying softmax before CrossEntropyLoss applies it twice. Both are demonstrated below, on Python 3.12.5, PyTorch 2.14.0+cpu. Using softmax in PyTorch Every value … Read more >>

Matplotlib Horizontal Grid Lines (and set_axisbelow)

Matplotlib horizontal grid lines: a plot with grid on both axes beside one with horizontal lines only

To show only horizontal grid lines in Matplotlib, pass axis=”y” to grid: Add ax.set_axisbelow(True) with it. Without that, gridlines are drawn on top of your bars and markers, which isn’t what anyone wants. Every plot below is a real Matplotlib window on Python 3.12.5, Matplotlib 3.11.2. Showing horizontal grid lines only The axis argument decides … Read more >>

How to Check if a String Contains All Unique Characters in Python?

Check if a String Contains All Unique Characters in Python

In this tutorial, I will explain how to check if a string contains all unique characters in Python. As a Python developer, I came across a scenario where I needed to check if a string contains all unique characters. After researching different methods, I discovered several ways to accomplish this task efficiently. In this article, … Read more >>