From a Black Box to a Four-Line Equation: Recovering the Exact Formula a 0.99 Model Was Hiding
From the publisher’s feed, collected in the ZeroLabs newsroom.
Not long ago I was given a dataset and a deceptively simple instruction: predict the target. A decision tree found the key split, a linear model built on that split reached an R² of 0.94, and a gradient-boosting model pushed that figure to 0.99. Any one of those numbers would have been enough to call the problem solved. All of them were concealing the same fact. The target was not a statistical relationship to be approximated; it was an exact, deterministic equation, and it fit on a single line. I nearly missed it because I had divided one important feature at a single threshold when it actually changed behavior at three. This article retraces that path, and ends with a small, reusable diagnostic for telling a missed split apart from genuine nonlinearity.The task: rules, not a scoreThe assignment was deliberately old-fashioned. Rather than “train a model and report a metric,” the brief asked me to express the target as a small set of readable rules, each of the form “if this condition holds, compute that formula,” which would then be passed to a tiny interpreter:def framework(pairs, arr): """For each row, run the first calc() whose condition is True.""" targets = [] for row in arr: for cond, calc in pairs: if cond_eval(cond, row): targets.append(calc(row)) break return targetsThe real question, then, was not how accurate I could be. It was what the target actually was, and that shift in framing turned out to matter a great deal.The data consisted of 10,000 rows and 273 features (feat_0 through feat_272), with a single continuous target. There were no column names, and no hints about what any feature meant.Step 1: letting a tree point at the important featuresA decision tree is an excellent instrument for answering one narrow question: which of these 273 columns actually matter? I fit one and read off the feature importances.https://medium.com/media/e93197bda51259a181d291156623dd53/hrefFour features carry almost all of the signal, and feat_187 dominates the rest. A tree of depth one splits it cleanly at 0.70. So I did the obvious thing, and fit a separate linear model on each side of that split.for label, mask in [("feat_187 ("feat_187 > 0.70", t > 0.7)]: lr = LinearRegression().fit(X[mask], y[mask]) print(label, r2_score(y[mask], lr.predict(X[mask])))The outcome is where the story begins.https://medium.com/media/b6f4f6ce94431a57d143173dbe88f540/hrefIn the region above 0.70, the target was not merely approximated; it was recovered exactly. The fitted coefficients came out as clean, round numbers, and the residuals sat at the level of floating-point error. That is the unmistakable signature of synthetic, deterministic data.The other region, however, containing 70 percent of the rows, settled at an R² of 0.65. This is where the first trap lies. The aggregate score, 0.94, looks entirely respectable. One perfect region and one mediocre region average out to a figure that would pass most reviews without comment. The headline metric was quietly concealing the fact that the majority of the data was poorly modeled.A high overall score is an average, and averages hide their worst neighborhoods.Step 2: the seductive wrong turnThe region below 0.70 is plainly harder, and the natural reflex, which I duly had, is to reach for a model with more capacity. I tried gradient boosting on the four features, restricted to that region:gb = GradientBoostingRegressor(n_estimators=300, max_depth=3, random_state=0)gb.fit(X[mask_low], y[mask_low])r2_score(y[mask_low], gb.predict(X[mask_low])) # -> 0.996There it was, 0.996, and the conclusion almost writes itself. The second region is nonlinear, a linear formula cannot capture it, so one should use a flexible model, accept the 0.99, and move on.I came close to doing exactly that. The difficulty is that a score of 0.99 answers the question “can I predict this?” while saying nothing about “what is this?”, which was the actual assignment. An ensemble of 300 trees is also the opposite of a rule one can read. So before accepting that the region was genuinely nonlinear, I wanted a single piece of evidence that it was not simply something I had overlooked.Step 3: the diagnostic, letting the slope moveHere is the idea that resolved it. If feat_187 is the feature that governs everything, then I should not merely split it once; I should ask how the relationship changes across its whole range. I divided feat_187 into narrow bins and, within each bin, fit a plain linear model of the target on the remaining three features. Then I looked at how the coefficients behaved.edges = np.arange(0, 1.01, 0.05)for lo, hi in zip(edges[:-1], edges[1:]): m = (t >= lo) & (t lr = LinearRegression().fit(X3[m], y[m]) print(round((lo+hi)/2, 3), m.sum(), lr.coef_, round(lr.intercept_, 3), round(r2_score(y[m], lr.predict(X3[m])), 3))Two things were immediately apparent from that table. First, every bin fit at an R² of 1.0, with an intercept of essentially zero. Within any narrow slice of feat_187, the target is perfectly linear in the other three features, with no noise at all. Second, the coefficients do not drift gradually. They hold steady across a stretch of feat_187, step sharply, then hold steady again.Plotted against feat_187, the pattern is unmistakable.Coefficients of the three features, fit in feat_187 bins. Flat within each regime, with hard steps at 0.2, 0.5 and 0.7.The region was never nonlinear. It consisted of three further linear regimes layered on top of one another, and my single split at 0.70 had merged them into one. A straight line drawn through three different straight lines is exactly the sort of thing that lands at an R² of 0.65, and exactly the sort of thing a large ensemble will smooth over to produce a misleading 0.99.The breakpoints were not even subtle. They fall at clean values of 0.2, 0.5 and 0.7. The tree had reported 0.70 because it was the single most important split. My mistake was to stop examining the very feature it had just identified as the most important one.Step 4: the recovered equationWith the breakpoints known, the entire target collapses into four lines.feat_187 0.2 ≤ ··· 0.5 ≤ ··· feat_187 ≥ 0.7 : target = 0.15·feat_64 + 1.85·feat_126 + 1.05·feat_53There is no intercept, there are no interaction terms, and there is no noise. The same three features appear in every regime; only the coefficients change as feat_187 crosses a threshold. Substituting this back into the full dataset gives an R² of 1.0 and a maximum residual of 8.4e-15.That residual is floating-point zero. This is not a good model of the target; it is the target. And it fits directly into the rule interpreter from the first step: four conditions, four formulas, entirely legible.Same features, same data. The only thing the left panel was missing was more breakpoints.What I take from thisThe honest summary is that the apparent nonlinearity was an artifact of dividing one feature too coarsely. A flexible model concealed this by bending around the seams between regimes, whereas the binned-coefficient view exposed it by showing those seams directly.The diagnostic generalizes, and it costs very little. When a single feature dominates and a per-region linear fit underperforms, bin that feature and plot the coefficients against it. The shape of the result tells you which situation you are in.Flat segments with sharp steps indicate a missed breakpoint. Add the split, keep the model linear, and keep it interpretable.Smooth drift indicates genuine continuous nonlinearity, such as an interaction or a curve, and a flexible model is now the appropriate choice.Noisy scatter, with R² well below 1.0 in every bin, indicates real irreducible noise, and the simple fit may already be close to the ceiling.A black-box score answers whether you can predict something. It says nothing about what that something is, and it will readily hand you a 0.99 that hides a missed split, a noise ceiling, and a perfectly readable equation all at once.This particular target happened to be synthetic, so it resolved to something exact and clean, and real data will not return a residual of 1e-15. But the underlying move is the same. Before upgrading to a model you cannot read, it is worth spending a few minutes asking whether it was your split, rather than your model, that was too simple.From a Black Box to a Four-Line Equation: Recovering the Exact Formula a 0.99 Model Was Hiding was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.