Machine LearningData Preprocessing & Feature Engineering

Which transformations are suitable for left-skewed and right-skewed data?

Question 276 of 303 · Updated

On this page

Short answer#

For right-skewed data (a long tail toward large values), try transformations that compress large values more strongly than small ones: logarithm, square root, cube root, reciprocal, or a fitted power transformation such as Box–Cox or Yeo–Johnson. For left-skewed data (a long tail toward small values), a power greater than one may help for suitable positive data, or you can reflect the values and then apply a right-skew-reducing transformation. Box–Cox and Yeo–Johnson estimate a power parameter and can be useful in either direction, but their result must be checked rather than assumed.

There is no universal transformation selected by the sign of skewness alone. First understand the variable, its valid domain, the model's assumptions, and whether the transformation improves the actual modeling objective. A feature does not have to be normally distributed for every machine-learning algorithm.

1. Understand the direction of skew#

  • Right-skewed (positive skew): most observations are relatively small, with a few very large observations. Examples include income, transaction values, claim amounts, and website visits.
  • Left-skewed (negative skew): most observations are relatively large, with a tail extending toward smaller values. Examples can include scores near a maximum, task completion percentages near 100%, or measurements with a lower-tail cluster.

Skewness describes asymmetry, not whether data is good or bad. A long tail may be a real business signal. Do not transform merely to make a histogram look symmetric.

A small right-skewed example#

Consider transaction amounts:

[10, 12, 15, 20, 25, 30, 40, 300]

The value 300 is much larger than the rest. With natural logarithms, the values become approximately:

[2.30, 2.48, 2.71, 3.00, 3.22, 3.40, 3.69, 5.70]

The large gap between 40 and 300 is compressed from 260 units to about 2.01 log units. The largest observation remains the largest; a monotonic log transform changes spacing, not ranking.

2. Transformations for right-skewed data#

A. Log transformation#

y = log(x)

A log transform strongly compresses large positive values. It is a common first experiment for positive measurements with a long right tail, such as revenue, income, population, or transaction amount.

Example using base 10:

  • 10 becomes 1
  • 100 becomes 2
  • 1,000 becomes 3
  • 10,000 becomes 4

This is useful when differences are naturally multiplicative or percentage-based. However, ordinary log requires strictly positive input. If zeros are meaningful, log1p(x) = log(1+x) is sometimes appropriate, but adding one is a modeling choice tied to the units and should not be treated as a universal fix. Negative values cannot be passed directly to a standard logarithm.

B. Square-root transformation#

y = sqrt(x)

The square root is milder than a logarithm for many nonnegative, right-skewed variables.

Example:

  • 1 becomes 1
  • 4 becomes 2
  • 25 becomes 5
  • 100 becomes 10
  • 400 becomes 20

It reduces the influence of large counts while retaining more separation among them than a log may. It is often considered for count-like values, but a count outcome may be better modeled with a Poisson or negative-binomial model when its distribution and mean–variance relationship support that choice.

C. Cube-root transformation#

y = cbrt(x)

The cube root is another relatively gentle compression and, unlike the square root, is defined for negative real numbers as well as positive ones.

Example:

  • 1 becomes 1
  • 8 becomes 2
  • 27 becomes 3
  • 125 becomes 5
  • 1,000 becomes 10

It can be useful when a square root is too mild and a log is too aggressive, or when values include negatives. The cube root does not guarantee symmetry.

D. Reciprocal transformation#

y = 1/x

The reciprocal can compress large positive values very strongly, but it has important drawbacks:

  • It is undefined at zero.
  • Values close to zero can become extremely large in magnitude.
  • It reverses the order of positive observations: a larger original value has a smaller reciprocal.
  • It can make interpretation difficult and amplify measurement noise near zero.

For some statistical power-transform conventions, a negative reciprocal such as -1/x is used to preserve the original ordering while applying a reciprocal-shaped power. Do not choose a reciprocal solely because a variable is skewed; inspect the domain and the resulting relationship.

3. What about left-skewed data?#

For left-skewed data, the tail is on the low-value side. A simple ladder-of-powers intuition is that powers greater than one can stretch the upper end relative to the lower end, potentially reducing negative skew for suitable positive values.

A. Square or cube transformation#

y = x^2 or y = x^3

For a positive variable, a power above one expands differences among larger observations. This can sometimes reduce left skew.

Example:

Original values: [1, 2, 3, 4, 5, 8]

Squared values: [1, 4, 9, 16, 25, 64]

The high end is spread out much more. Whether this actually improves the distribution depends on the data; a square also loses the sign of negative values and is not one-to-one over the real line. Cubing preserves sign and ordering, but can magnify outliers.

B. Reflect, then transform#

A common approach for left-skewed positive data is to reflect it around a constant:

z = c - x

Choose c greater than the largest training observation. The reflected variable has its long tail on the right. You can then try a log, square root, or another suitable right-skew-reducing transformation on z.

Example:

Original scores: [40, 75, 82, 88, 92, 95, 98]

Choose c = 100. Reflected values are:

[60, 25, 18, 12, 8, 5, 2]

The reflected values are right-skewed in this illustrative set. Applying a log compresses the large reflected values. To return to the original direction, invert the steps: if w = log(c-x), then x = c - exp(w).

Important cautions:

  • The constant c affects the transformed values. Document why it was selected.
  • Estimate c using training data only; do not calculate it from the full dataset before splitting.
  • Reflection reverses order. Keep the inverse mapping and explain the transformed feature to downstream users.
  • If future observations can exceed the chosen c, the transformation may fail. Define a robust policy for out-of-range values.
  • Reflection is a technique, not a guarantee that the result will be normal or better for prediction.

C. Use a fitted power transformation#

A fitted power transformation can be more systematic than manually trying a square or cube. Box–Cox and Yeo–Johnson estimate a parameter from the training data to find a transformation that improves symmetry or variance behavior under their objective. They can be tried on left- or right-skewed data, but inspect the fitted parameter and output rather than assuming a direction-specific result.

4. Box–Cox transformation#

The Box–Cox family is:

when , and y = log(x) when .

The parameter controls the shape of the transformation. Familiar special cases include:

  • : essentially no power-shape change (up to a constant shift)
  • : square-root-like
  • : logarithm
  • : reciprocal-shaped

The fitted value is commonly selected using maximum likelihood. Box–Cox requires every input value to be strictly positive. If the data include zero or negative values, do not silently pass them in. A domain-justified shift can make values positive, but the shift changes interpretation and must be recorded; alternatively use Yeo–Johnson.

5. Yeo–Johnson transformation#

Yeo–Johnson is a power-transform family designed to handle positive and negative observations. It uses one power expression for nonnegative values and a corresponding expression for negative values, so it can also accept zero.

Practical benefits:

  • Works with positive, zero, and negative values.
  • Estimates a power parameter from the training data.
  • Can reduce skewness and sometimes stabilize variance.
  • Has an inverse transformation when the fitted parameter is retained.

It is available in common tools such as SciPy and scikit-learn. In scikit-learn, PowerTransformer(method="yeo-johnson") is the default; method="box-cox" selects Box–Cox and therefore requires strictly positive inputs. By default, scikit-learn also standardizes the transformed output to zero mean and unit variance. If you want to study only the effect of the power transformation, set standardize=False.

6. How should you choose? A practical decision path#

  1. Check meaning and validity. Is the feature a count, currency amount, duration, bounded score, signed difference, or ratio? Identify zeros, negatives, censoring, and impossible values.
  2. Plot the raw distribution. Use a histogram or density plot, box plot, and quantiles. Calculate skewness, but do not rely on one statistic.
  3. Start with a simple candidate. For positive right-skewed values, compare log, square root, and possibly cube root. For left skew, consider a power above one or a carefully defined reflection. For mixed-sign values, Yeo–Johnson is a convenient candidate.
  4. Fit learned parameters on training data only. This includes Box–Cox/Yeo–Johnson parameters, shifts, and reflection constants. Put the transformation inside a preprocessing pipeline so cross-validation refits it within each training fold.
  5. Compare diagnostics. Replot the transformed feature, inspect skewness and quantiles, and use a Q–Q plot when distributional shape matters. For regression, inspect residual-versus-fitted plots and residual Q–Q plots; transforming a predictor is not the same as fixing nonconstant residual variance.
  6. Compare model performance. Evaluate the same validation splits and relevant business metrics with and without the transform. Consider interpretability, stability, tail behavior, and inverse-transform requirements.
  7. Monitor production behavior. Persist the fitted transformation and parameters, validate input ranges, track missing/zero/out-of-range rates, and monitor drift. Never refit preprocessing on live test or validation data during evaluation.

7. Numerical selection examples#

Example 1: Right-skewed transaction amount#

Suppose amounts are [5, 10, 20, 40, 400].

  • Square root: approximately [2.24, 3.16, 4.47, 6.32, 20].
  • Natural log: approximately [1.61, 2.30, 3.00, 3.69, 5.99].

Both compress the high end. The log compresses the extreme 400 more strongly. If the variable is strictly positive and the relationship is multiplicative, log is a sensible candidate. If the values are counts and the compression should be milder, try square root. Choose using validation and interpretation, not a fixed rule.

Example 2: Left-skewed assessment score#

Suppose scores cluster near 100 with a few low results: [45, 80, 88, 92, 96, 98, 99].

Reflect around 100:

[55, 20, 12, 8, 4, 2, 1]

Then apply a log to the reflected values. This compresses the reflected high tail. The transformed result must be interpreted through the inverse mapping score = 100 - exp(transformed_value). In real work, choose the reflection constant based on a documented domain boundary or a training-set rule and validate future-range behavior.

Example 3: Data containing negatives and zero#

Suppose a feature is [-8, -2, 0, 3, 20]. Ordinary log and Box–Cox cannot directly handle this data. A square root is also not real-valued for negative inputs. Yeo–Johnson can process the entire set without an arbitrary positive shift. Fit it on training data, retain the fitted parameter, and compare its diagnostics and downstream validation performance.

8. Common mistakes#

  • Transforming every skewed feature automatically: Tree-based models often handle skewed predictors without requiring normality. Linear or distance-based methods may benefit, but validate.
  • Confusing feature normality with residual assumptions: Ordinary least squares does not require every predictor to be normally distributed. For classical inference, assumptions concern the model errors and conditional structure; inspect residuals.
  • Using log on zero or negative values: Choose a valid alternative or a justified shift; do not hide invalid inputs.
  • Using reciprocal near zero: It can create enormous values and numerical instability.
  • Choosing a transformation because the histogram looks prettier: Check the relationship with the target and out-of-sample performance.
  • Leaking validation information: Fit transformation parameters only on the training fold.
  • Forgetting interpretation: A model trained on log(y) predicts on the log scale. Exponentiating a prediction does not generally produce the original-scale conditional mean because of retransformation bias.
  • Assuming transformation removes outliers: A valid extreme observation remains important. Investigate data errors and rare-event meaning separately.

9. Python example with scikit-learn#

Use a pipeline so the transformation is fitted only on each training fold:

PYTHON
from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline from sklearn.preprocessing import PowerTransformer, StandardScaler from sklearn.impute import SimpleImputer from sklearn.linear_model import Ridge numeric_features = ["income", "account_balance", "signed_change"] numeric_pipeline = Pipeline([ ("imputer", SimpleImputer(strategy="median")), ("power", PowerTransformer(method="yeo-johnson", standardize=False)), ("scale", StandardScaler()) ]) preprocessor = ColumnTransformer([ ("numeric", numeric_pipeline, numeric_features) ]) model = Pipeline([ ("preprocess", preprocessor), ("regressor", Ridge()) ]) model.fit(X_train, y_train) predictions = model.predict(X_valid)

This example uses Yeo–Johnson because the listed features may include zero or negative values. If a feature is known to be strictly positive, compare Box–Cox as a candidate. Do not apply one global transformation blindly to every numerical column; different features can have different domains and shapes. Also, scaling after power transformation is a separate operation.

10. Final rule of thumb#

  • Strong right skew, positive values: begin with log; compare square root or cube root if log is too aggressive.
  • Moderate right skew, nonnegative counts: try square root or cube root.
  • Very strong skew with strictly positive values: compare fitted Box–Cox with simple transforms.
  • Zero or negative values: consider Yeo–Johnson; cube root is another simple candidate.
  • Left skew: consider a power above one for appropriate positive data, or reflect around a documented constant and then transform; also evaluate Yeo–Johnson.
  • Reciprocal: use only when its domain and interpretation make sense, especially avoiding values near zero.
  • Any direction: retain the original feature as a baseline, compare diagnostics and validation performance, and transform only when there is a measurable reason.

The goal is not to force every feature into a bell curve. The goal is to represent the data in a way that improves the model's assumptions, stability, interpretability, or predictive performance without losing important information.

Related questions

Explore other topics:Deep LearningMLOps