Create a Matplotlib Boxplot for Time Series Data in Python

Matplotlib Boxplot for Time Series Data in Python

I’ve found that a single line chart often hides the most important parts of a story. When you are dealing with time series data, showing just the average or the trend line isn’t enough to see the full picture. You often need to see the “spread” or the volatility of that data over specific intervals, … Read more >>

Supervised Consistency Training in Keras

Supervised Consistency Training in Keras

Training deep learning models that generalize well to real-world data can be a real challenge. In my four years of working with Keras, I’ve found that supervised consistency training is a game-changer for model stability. This technique ensures that your model produces similar predictions even when the input data undergoes slight variations or noise. In … Read more >>

Matplotlib Multiple Circle Plots

Matplotlib Multiple Circle Plots

Creating multiple circles in Matplotlib is one of those tasks that sounds simple until you actually try to align them perfectly. I have spent years building dashboards and data visualizations in Python, and I can tell you that there are at least three ways to do this. Each has its own place depending on whether … Read more >>

Supervised Contrastive Learning in Python Keras

Supervised Contrastive Learning in Python Keras

When I first started training deep learning models for image classification, I often relied on the standard Cross-Entropy loss. It worked well enough, but I noticed the models struggled when the classes were visually similar. I discovered that Supervised Contrastive Learning (SupCon) is a game-changer for these scenarios. It helps the model learn to pull … Read more >>

How to Create a Stacked Bar Plot in Pandas

Create a Stacked Bar Plot in Pandas

I have found that a stacked bar plot is one of the most effective ways to visualize part-to-whole relationships. Whether I’m analyzing regional sales or tracking demographic shifts, Pandas makes it incredibly simple to generate these charts with just a few lines of code. In this tutorial, I’ll show you exactly how to build and … Read more >>

Rotate Tick Labels in Matplotlib: 45 and 90 Degrees

Rotate tick labels in Matplotlib: the same bar chart with x-axis labels at 0, 45 and 90 degrees

To rotate x-axis tick labels in Matplotlib, call plt.xticks(rotation=45) after plotting. That one line fixes the overlapping month names almost everyone lands here for. Rotation alone usually isn’t enough, though. At 45 degrees the labels drift away from their ticks unless you also set the alignment, and that’s the part most guides leave out. Every … Read more >>

Check if a Number Is Between Two Values in Python

Checking whether a Python variable is between two numbers: the Command Prompt showing chained comparisons with inclusive and exclusive bounds

To check whether a Python variable sits between two numbers, chain the comparison the way you’d write it on paper: That’s the whole answer. Python allows low <= x <= high directly, which most languages don’t, so you rarely need and. Below I check the corners that actually bite: range(), floats, and arrays. Everything ran … Read more >>

scikit-learn accuracy_score: Usage and When It Misleads

scikit-learn accuracy_score: the Command Prompt showing a model scoring 98 percent accuracy while catching none of the fraud cases

The accuracy_score in sklearn returns the fraction of predictions that match the truth. Here’s the import and the whole call: It takes the true labels first and the predictions second. Swapping them gives the same number, so the mistake hides, but every other metric in sklearn.metrics cares about the order. Everything here ran on scikit-learn … Read more >>

PyTorch MSELoss: reduction, F.mse_loss and Shapes

PyTorch MSELoss: the Command Prompt showing the mean, sum and none reduction options side by side

nn.MSELoss is the mean squared error loss in PyTorch. It squares the gap between each prediction and its target, then averages. There’s a functional twin, F.mse_loss, that does exactly the same arithmetic without the object. Which you use is a style choice, and I’ll show why below. Runs and screenshots come from PyTorch 2.14.0, Python … Read more >>

np.add.at() in NumPy: Adding at Repeated Indices

np.add.at in NumPy: the Command Prompt showing plain fancy indexing losing duplicate indices while np.add.at counts all of them

np.add.at adds values into an array at the indices you give it, and it applies every index even when one repeats. That repetition is the entire point. Plain array[indices] += values quietly keeps only the last write for any index that appears more than once. Below I show that difference, the 2D form, and how … Read more >>