Lesson

ML Data Distribution

10 min reading
Free Course

ML Data Distribution: Uniform, Normal, and Skewed Shapes

ML data distribution describes how a feature's values spread across their range: clustered around a center, spread evenly, or leaning toward one side. Checking the shape early helps you avoid feeding distorted features into linear or distance-based models. A histogram and a few summary numbers are usually enough to see it.

Normal vs Uniform vs Skewed Distributions

  • Uniform Distribution: Every value inside [low, high] has equal probability (like rolling a fair die or random.uniform(0, 10)).

  • Normal (Gaussian) Distribution: Most values cluster symmetrically around the mean in a bell curve (random.gauss(mu, sigma)).

  • Skewed Distribution: A long tail stretches left or right (common in prices, file sizes, and page views).

import random
import statistics

random.seed(7)
uniform_vals = [round(random.uniform(0, 100), 2) for _ in range(500)]
normal_vals = [round(random.gauss(50, 10), 2) for _ in range(500)]

print("Uniform stdev:", round(statistics.stdev(uniform_vals), 2))
print("Normal  stdev:", round(statistics.stdev(normal_vals), 2))

Handling Heavy Right Skew in ML

When a positive feature like revenue has extremely large values, applying a log transform (math.log1p(x) or np.log1p(x)) compresses the long tail and often improves linear regression accuracy.

Common Mistakes to Avoid

  • Assuming real-world data is normal. Session lengths, prices, and bug counts are usually right-skewed. Check before choosing a method.

  • Using math.log(x) when values include 0. math.log(0) raises ValueError: math domain error. Use log1p(x) instead. It still fails for values of -1 or lower, so check for negatives.

  • Forgetting to invert target transforms. Apply expm1to predictions before reporting them in the original units.

Frequently Asked Questions

How can I check distribution shape quickly in Python?

Plot a histogram with matplotlib.pyplot.hist(values, bins=30)or compare the mean to the median. A mean much larger than the median suggests right skew.

Do decision trees require normally distributed features?

No. Tree-based models such as Random Forests and XGBoost split on thresholds which depend only on the order of values. Skewness and transforms like log don't change those splits.

What does random.gauss(mu=50, sigma=10) generate?

Floating-point numbers drawn from a normal distribution centered at 50 with a standard deviation of 10.

Does a log transform always help?

Not always. It helps most with positive, right-skewed data and linear or distance-based models. Check the histogram before and after.

Conclusion

Check the shape of a feature before modeling: use a histogram and compare mean to median. For positive right-skewed data, applylog1p, and remember to invert it with expm1 on predictions. Tree-based models don't need the transform, but linear and distance-based ones often benefit.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Python Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum