Menu

1 / 3

Fit a sensor calibration with NumPy, then inspect the errors

DEV Community: tutorial·Wenlo·5 days ago
#dtqVcWIQ
Reading 0:00
15s threshold

A fictional sensor reports numbers around 102 to 503, while a reference instrument reports temperatures from 0 to 40 °C. How can we convert a new raw reading into an estimated temperature?

We will fit a straight line with NumPy, inspect every residual, and plot the relationship with Matplotlib. The data is fictional and the script is self-contained. This is a numerical learning example, not a calibration procedure for medical, industrial, or other safety-critical equipment.

1. Write the model before calling the solver

Our assumption is temperature = slope × raw reading + intercept. Each observation contributes one equation. A row of the design matrix therefore contains [raw reading, 1], and the coefficient vector contains [slope, intercept] in that order.

Five observations give us five equations for two unknowns. Real measurements rarely sit on exactly the same straight line, so trying to satisfy every equation exactly would be the wrong goal.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.