AI Hints
Statistics & Math for ML · Lesson 20 of 138

Skewness

Long tails.

⏱ 2 min read▶ Video at 21:45🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

Many datasets aren't symmetric. Incomes have a long tail to the right, because a few people earn a lot. That's called right skew, or positive skew. Easy exams have a tail to the left, because most people score high. That's left skew.

Skewness
From the video: Skewness
  • Not everything is symmetric
  • Long right tail: positive skew
  • Long left tail: negative skew

Measuring skew

Sci Pi's stats module has a skew function. Zero means symmetric. Positive means a long right tail, and negative means a long left tail. Run it. The heights are close to zero. Incomes are strongly positive. And the easy exam is negative.

skew.pypython
import numpy as np
from scipy.stats import skew

rng = np.random.default_rng(0)
heights = rng.normal(170, 8, 5000)
income = rng.lognormal(10, 0.7, 5000)
easy_exam = 100 - rng.exponential(10, 5000)

print("heights:", skew(heights).round(2))
print("income: ", skew(income).round(2))
print("exam:   ", skew(easy_exam).round(2))
Output
heights: 0.02
income:  2.48
exam:    -1.96

Fixing skew

Strong skew can confuse some models, like linear regression. A common fix is to take the logarithm. A log squashes big values much more than small ones, so the long tail gets pulled in. That's why you'll often see log of income, or log of price, in machine learning.

keyvalue
skew ≈ 0symmetric
skew > 0right tail, mean > median
skew < 0left tail, mean < median
Fixnp.log of the values

Summary

Skewness measures lopsidedness. Positive skew has a right tail, and the log often fixes it. Next, z scores, and how unusual a value is.

Key takeaways
  • scipy.stats.skew
  • Right tail: positive
  • np.log pulls in tails
Next up: Z-Scores — how unusual is it?