Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 40 ยท Data Preparation

Binning, Power Transforms and Custom Features

Reshape skewed or awkward features with KBinsDiscretizer, Binarizer, PowerTransformer, QuantileTransformer and FunctionTransformer, plus PolynomialFeatures.

Scaling changes the size of a feature but not its shape. Sometimes the shape is the problem: income is heavily skewed, age matters only in bands, or the relationship you want needs a logarithm. This lesson covers the transformers that reshape features: binning, power and quantile transforms, and your own functions.

Binning with KBinsDiscretizer

KBinsDiscretizer turns a continuous number into a small number of buckets. It can encode them as integers (ordinal) or one-hot columns, and bin by equal width (uniform), equal counts (quantile) or clusters (kmeans).

Python KBinsDiscretizer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Binning lets a linear model fit a step-shaped relationship, at the cost of losing detail inside each bin. It is rarely needed for tree models, which find their own thresholds.

Binarizer: above or below a threshold

Python Binarizer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Skewed features and the power transform

Many real variables (income, prices, counts, response times) have a long right tail. Linear models and distance methods behave better when features look closer to a bell curve. PowerTransformer finds the best power transform for each column: Yeo-Johnson (works with any values) or Box-Cox (positive values only).

Python PowerTransformer reduces skew

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A skew near 0 means a symmetric distribution. The lambda close to 0 corresponds to a log transform.

QuantileTransformer: force a shape

QuantileTransformer maps values through their rank, producing a uniform (or normal) distribution whatever the original shape. That also tames outliers, but it can distort distances between points because it ignores how far apart values were.

Python QuantileTransformer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

FunctionTransformer: bring your own function

FunctionTransformer wraps any function as a transformer, so a log or a ratio can live inside a pipeline. For a log on non-negative data, np.log1p (log of 1 plus x) is the safe choice.

Python FunctionTransformer with log1p

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When the target is skewed, TransformedTargetRegressor applies a function to y before fitting and undoes it after predicting.

Python Transform the target, predict in original units

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

PolynomialFeatures: interactions and powers

PolynomialFeatures creates xยฒ, xยณ and products like x1*x2. Fed into a linear model they let it capture curves and interactions. It is the core of polynomial regression.

Python PolynomialFeatures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The number of features grows fast with the degree and the number of columns, so keep degrees low and combine with regularization.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A feature such as income has a long right tail. Which transformer is designed to make it more bell-shaped?

    PowerTransformer learns a power (Yeo-Johnson or Box-Cox) that reduces skew.
  2. 2. What does KBinsDiscretizer(strategy='quantile') do?

    Quantile bins are cut at percentiles so each bin has about the same count.
  3. 3. Why use np.log1p rather than np.log?

    log(0) is undefined, while log1p(0) is 0.
  4. 4. What is TransformedTargetRegressor for?

    It trains on a transformed target and returns predictions in the original units.

Summary

  • KBinsDiscretizer and Binarizer turn numbers into bands or flags.
  • PowerTransformer and QuantileTransformer reshape skewed distributions.
  • FunctionTransformer wraps any function, such as np.log1p, as a pipeline step.
  • TransformedTargetRegressor handles skewed targets.
  • PolynomialFeatures adds powers and interactions for linear models.

Next: Outliers and Data Cleaning