How to do it...

To perform equal-frequency discretization followed by ordering the intervals according to the target mean, we need to import from Feature-engine the EqualFrequencyDiscretiser() and the OrdinalCategoricalEncoder(), among other Python libraries and classes:

  1. Import the required Python libraries and classes:
import pandas as pdimport matplotlib.pyplot as pltfrom sklearn.datasets import load_bostonfrom sklearn.model_selection import train_test_splitfrom feature_engine.discretisers import EqualFrequencyDiscretiserfrom feature_engine.categorical_encoders import OrdinalCategoricalEncoder
  1. Let's load the predictor variables and target from the Boston House Prices dataset:
boston_dataset = load_boston()data = pd.DataFrame(boston_dataset.data, ...

Get Python Feature Engineering Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.