Target Variable Transformation
In a regression task, the target variable y is the quantity the model is trying to predict.
A Model Asks for One Specific Thing
Suppose you have a dataset, a model, and a pile of candidate transformations. Which one should you apply? The honest answer is that picking the right transformation is part judgment and part experiment. The best choice depends on the particular pairing of data and model, and nothing about one pairing guarantees the same result for another.
This article follows one question through the rest of the discussion: given a model, what should be changed in the data, and what should be left exactly as it is? Keep that question in mind while you read, because the answer changes with the model family. Tree-based models are less sensitive to transformations, while linear models often depend on them.
Features Versus the Target
In a regression task, the target variable y is the quantity the model is trying to predict. Most of the transformation discussion concerns the features, which are the inputs the model sees. The source notes that it is sometimes worth transforming y as well. This is a different kind of change. Feature transformation alters what the model sees as input X. Target transformation alters what the model is asked to predict.
| Question | Feature transformation | Target transformation |
|---|---|---|
| What is changed? | The input values X the model sees | The quantity y the model is trying to predict |
| Where does it apply? | To one or more feature columns | To the target column in regression |
| What comes back out? | Predictions are already in the target's scale | Predictions come out in the transformed scale and must be converted back |
| Typical example | Transforming a feature whose values span several orders of magnitude | Using log(y + 1) when y is a count |
Feature and target transformations change different parts of the learning problem.
Choosing Which Features to Touch
Once you know the model family matters, the next question is which features to touch. The source is clear that not all features require transformation. In a clean teaching example, every feature may share the same statistical properties, which makes one uniform treatment look natural. Real datasets rarely behave that way. Usually only a subset of the features needs transforming, and the remaining ones should be left as they are.
Even among the features that do need work, one treatment rarely fits all of them. Different features may call for different transformations. A feature whose values are spread across several orders of magnitude may need a different correction from a feature whose values already sit in a narrow band. For that reason, a sensible workflow inspects features one at a time, asks what each one looks like, and chooses a transformation for that feature specifically. The flowchart below sketches that sequence of checks.
Tracing the Log(y + 1) Round Trip
The source singles out one common situation: predicting counts, such as the number of orders a customer places. Counting outcomes are a fairly routine regression task, and the source reports that applying the log(y + 1) transformation to the target often helps. The transformed target is $\log(y + 1)$, where $y$ is the original count.
Changing the target changes the order of operations. You do not feed the raw counts to the model. You transform y first, train on the transformed values, and the model's predictions come out in the transformed scale. To read a prediction as a count again, you must invert the transformation. Because $z = \log(y + 1)$ implies $y = e^{z} - 1$, a predicted value $\hat{y}$ in log space becomes $e^{\hat{y}} - 1$ on the count scale. The pipeline below traces that round trip.
Notice where the original count scale returns. It does not come back during training, and it does not come back at prediction time. It appears only at the last step, when the log-space output is inverted. A model evaluated only on log-space outputs can look fine while the counts it implies are off, so the inversion step belongs in your checks.
Why Adding One Keeps Zero Valid
What do you think happens?
A customer placed zero orders, so y = 0. What is log(0 + 1)?
Reveal answer
log(1) = 0
Plain log(0) is undefined, but adding 1 first gives log(0 + 1) = log(1) = 0. That is the reason the source's form keeps zero counts defined.
The +1 has two jobs. It keeps the transformation defined when the count is zero, and it compresses large values, since the logarithm grows slowly for big inputs. Both properties matter for count data, where zeros are common and a few very large counts can dominate a raw regression.
What the Transformation Does to a Skewed Count Target
Consider a count target where most values are small but a few are very large. On the raw scale, the large values stretch the distribution into a long right tail. The table below shows several original counts next to their log(y + 1) values. The large counts move much closer together after the transformation, while zero stays a valid, finite value.
Reading a count prediction on the original scale
A model trained on log(y + 1) predicts ŷ = 2.5 for one customer. What is the predicted number of orders on the original count scale?
- 1Identify the inversion
Since z = log(y + 1) gives y = ez − 1, the prediction on the count scale is exp(ŷ) − 1.
- 2Substitute the prediction
Replace ŷ with 2.5 to get exp(2.5) − 1.
- 3Evaluate the exponential
exp(2.5) is approximately 12.18.
- 4Subtract one
12.18 − 1 gives approximately 11.18.
- 5Interpret
The model's log-space output corresponds to about 11 orders on the original scale, not 2.5.
The predicted count is approximately 11.18 orders.
Mistakes With Target Transformations
Common mistake Treating a transformed-target prediction as a count
What it looks likeThe model outputs 2.5 for a customer and that number is reported as 2.5 orders.
Why it failsThe model was trained on log(y + 1), so its output lives in log space.
Do this insteadApply exp(ŷ) − 1 before reporting the prediction on the count scale.
Common mistake Assuming every feature needs the same transformation
What it looks likeApplying one correction to every column because one column looked skewed.
Why it failsNot all features need transformation, and different features may need different treatments.
Do this insteadInspect features one at a time and transform only those that call for it.
Common mistake Applying a transformation out of habit
What it looks likeKeeping log(y + 1) because it worked on a different dataset.
Why it failsThe right transformation depends on the pairing of data and model, and results do not carry over automatically.
Do this insteadCompare performance with and without the transformation using the same evaluation procedure.
Common mistake Forgetting that the transformation changes the target, not the inputs, so the model must still be evaluated on the original counts after inversion
Practice: Reading a Transformed Prediction
A model trained on log(y + 1) for a count target predicts ŷ = 3.0 for a store. Compute the predicted count on the original scale using exp(ŷ) − 1, then explain in one sentence why reporting 3.0 as the count would be wrong.
Hints
- Start from the inversion rule: the count scale value is exp(ŷ) − 1.
- Use exp(3) as approximately 20.09.
- Think about which scale the model's output lives on, not which scale you want to report.
Key Takeaways
- The right transformation depends on the pairing of dataset and model, and choosing it is part judgment and part experiment.
- Feature transformation changes the inputs the model sees, while target transformation changes what the model is asked to predict.
- Not every feature needs transformation, and each feature that does may need its own treatment.
- For count targets, log(y + 1) keeps zero counts defined and compresses large values, but its predictions must be inverted with exp(ŷ) − 1 before they are read as counts.
- A transformation is justified by comparing results with and without it under the same evaluation procedure.