How to fit the 2D scatter data with a line with C
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Fitting 2D scatter data with a line usually means ordinary least squares linear regression. Given points x, y, you want a line y = m x + b that minimizes the sum of squared vertical residuals. For that problem, you do not need a large numerical library; a small C program can compute the slope and intercept directly from sums over the data.
The Least-Squares Formula
For n points, the standard formulas are:
- '
m = (n * sum(xy) - sum(x) * sum(y)) / (n * sum(x^2) - sum(x)^2)' - '
b = (sum(y) - m * sum(x)) / n'
These formulas assume:
- '
xis the independent variable' - the goal is to minimize vertical error in
y - the best-fit line is not vertical
That last assumption matters. If all x values are identical, the denominator becomes zero and the slope-intercept form breaks down.
A Runnable C Implementation
Compile and run it:
This is enough for many practical cases where you just need a basic straight-line fit.
What the Numbers Mean
The slope m tells you how much y changes when x increases by one unit. The intercept b is the predicted value of y when x is zero.
If the fitted line is y = 0.6 x + 2.2, then every additional unit of x raises the prediction by about 0.6.
That interpretation is easy, but do not mistake interpretability for correctness. A line can be easy to describe and still be the wrong model for the data.
Check the Residuals
A line fit is only useful if a line is a sensible model. If the points curve, cluster into multiple groups, or are dominated by outliers, a straight line may be misleading.
A simple residual calculation helps:
Residuals close to zero suggest the line is reasonable. A pattern in the residuals usually means the model is missing structure.
Know the Limitation of Ordinary Least Squares
This approach minimizes vertical distances, not geometric distance to the line. That is correct for many regression problems, but not for every line-fitting problem.
If you want the best geometric line through scattered points, especially for nearly vertical lines, you may need total least squares or another orthogonal-distance method.
So the right question is not just "how do I fit a line?" It is "what kind of line-fitting problem do I actually have?"
Common Pitfalls
The biggest mistake is applying the least-squares line formula to data that is obviously nonlinear.
Another mistake is ignoring the zero-denominator case when all x values are identical or nearly identical.
A third issue is trusting the line parameters without inspecting residuals or plotting the points.
Summary
- A straight-line fit in C can be implemented with ordinary least squares using simple sums
- The formulas give slope and intercept for
y = m x + b - This approach assumes vertical residual minimization, not arbitrary geometric distance
- Always check for the vertical-line failure case and inspect residuals
- A small C program is enough for basic regression when the model assumptions are appropriate
Related reading
- How to fix AttributeError module 'tensorflow' has no attribute 'get_default_graph'?
- How to fix error Cannot register 2 metrics with the same name /tensorflow/api/keras/optimizers
- How to fix error where a KerasTensor is passed to a TF API?
- How to fix initial_lr not specified when resuming optimizer error for scheduler?
- How to fix Helm installation failed complaning about a nil pointer evaluating interface on fullnameOverride
- How to get a random element from a C++ container?
- How to fix low volatile GPU-Util with Tensorflow-GPU and Keras?
- How to fix module 'tensorflow' has no attribute 'estimator' error
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.