Data Fitting
Curve Fitting
Regression Analysis
Multiple Line Fitting
Statistical Modeling

How to fit more than one line to data points

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

If one straight line does not describe your data well, the next step is often not a higher-degree polynomial. A more interpretable option is to fit multiple line segments, usually called piecewise linear regression or segmented regression. The main decisions are where the breakpoints go and whether the segments must join continuously.

Start with the Statistical Question

"Fit more than one line" can mean different things:

  • cluster the points into separate groups and fit one line per group
  • fit contiguous line segments along the x axis
  • fit a continuous broken line with one or more change points
  • fit a robust model that ignores outliers instead of adding more segments

The most common case is segmented regression: the data follows one slope up to a breakpoint and a different slope after it.

A simple continuous two-segment model can be written as:

  • 'y = b0 + b1 * x + b2 * max(0, x - c)'

Here c is the breakpoint. Before c, the slope is b1. After c, the slope becomes b1 + b2.

A Practical Brute-Force Approach

If you only need one breakpoint, a very practical method is:

  1. sort points by x
  2. try each reasonable breakpoint candidate
  3. fit a left line and a right line
  4. choose the split with the smallest total squared error

That is simple, understandable, and often good enough.

Here is a runnable Python example using NumPy:

python
1import numpy as np
2
3
4def fit_line(x, y):
5    coef = np.polyfit(x, y, 1)
6    yhat = np.polyval(coef, x)
7    sse = np.sum((y - yhat) ** 2)
8    return coef, sse
9
10
11def best_two_segment_fit(x, y, min_points=3):
12    order = np.argsort(x)
13    x = x[order]
14    y = y[order]
15
16    best = None
17    for split in range(min_points, len(x) - min_points + 1):
18        left_coef, left_sse = fit_line(x[:split], y[:split])
19        right_coef, right_sse = fit_line(x[split:], y[split:])
20        total_sse = left_sse + right_sse
21
22        if best is None or total_sse < best[0]:
23            best = (total_sse, split, left_coef, right_coef)
24
25    return best, x, y
26
27
28x = np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype=float)
29y = np.array([2.0, 4.1, 5.8, 7.9, 8.2, 8.8, 9.1, 9.5], dtype=float)
30
31best, x_sorted, y_sorted = best_two_segment_fit(x, y)
32total_sse, split, left_coef, right_coef = best
33
34print("split index:", split)
35print("left line: y = %.3f x + %.3f" % (left_coef[0], left_coef[1]))
36print("right line: y = %.3f x + %.3f" % (right_coef[0], right_coef[1]))
37print("total SSE:", total_sse)

This does not enforce continuity at the breakpoint. It simply finds the best two separate linear fits over contiguous ranges.

Enforcing a Continuous Broken Line

If you want the two lines to meet, parameterize the model with a hinge term and optimize the breakpoint. One easy method is to scan possible c values and fit the linear coefficients with least squares.

python
1import numpy as np
2
3
4def fit_continuous_piecewise(x, y, candidates):
5    best = None
6    for c in candidates:
7        X = np.column_stack([
8            np.ones_like(x),
9            x,
10            np.maximum(0.0, x - c)
11        ])
12        coef, _, _, _ = np.linalg.lstsq(X, y, rcond=None)
13        yhat = X @ coef
14        sse = np.sum((y - yhat) ** 2)
15        if best is None or sse < best[0]:
16            best = (sse, c, coef)
17    return best
18
19
20candidates = np.linspace(x.min() + 0.5, x.max() - 0.5, 20)
21sse, breakpoint, coef = fit_continuous_piecewise(x, y, candidates)
22print("breakpoint:", breakpoint)
23print("coefficients:", coef)
24print("sse:", sse)

This model is often easier to explain because the segments join smoothly.

More Than Two Lines

For more than one breakpoint, you can extend the same ideas, but model selection becomes important. More segments always reduce training error, so you need a criterion that balances fit quality against complexity.

Common choices are:

  • cross-validation
  • AIC or BIC
  • a minimum number of points per segment
  • domain knowledge about where regime changes can happen

If the data really contains several distinct trends, dynamic programming methods can find globally optimal segmented fits for a chosen number of segments. For many engineering tasks, though, one or two breakpoints plus validation is enough.

Common Pitfalls

A common mistake is adding segments when the real issue is outliers. In that case, robust regression may help more than piecewise regression.

Another mistake is fitting separate lines to unsorted data when the intent was segmented regression across the x axis. Segment order matters.

People also often choose too many breakpoints because training error keeps dropping. Without validation, that is just overfitting.

Finally, be explicit about whether continuity is required. Two independent lines and one continuous broken line are different models.

Summary

  • Fitting more than one line usually means piecewise or segmented linear regression
  • First decide whether you want separate clusters, contiguous segments, or a continuous broken line
  • A brute-force split search is a practical method for fitting two segments
  • A hinge-term model is useful when the segments should meet continuously
  • Use validation or information criteria to avoid overfitting when adding breakpoints
  • More lines are not always the right answer if the real problem is noise or outliers

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.