Contents

Building a Python Function to Calculate WoE and IV

   Apr 9, 2023     11 min read

Learn how to calculate Weight of Evidence (WoE) and Information Value (IV) in Python for feature selection in logistic regression, using practical examples with the Titanic dataset.

Weight of Evidence (WoE) and Information Value (IV) are widely used metrics in binary classification problems, especially in logistic regression models, credit risk analysis, and scorecards.

These metrics help evaluate:

  • the predictive power of a variable;
  • the relationship between categories and the target event;
  • the quality of transformations applied to variables.

While working on some Python projects, I noticed the lack of simple and reusable functions to calculate WoE and IV in a practical way. For this reason, I decided to develop my own functions for:

  • categorical variables;
  • continuous variables;
  • generating consolidated WoE and IV tables.

In this post, I present these functions using the classic Titanic dataset from Kaggle.

What is WoE?

Weight of Evidence (WoE) measures the relationship between the distribution of events and non-events within a specific category.

\[WoE = \ln\left(\frac{\%\,\text{non-event}}{\%\,\text{event}}\right)\]

Positive values indicate a higher concentration of non-events, while negative values indicate a higher concentration of events.

What is IV?

Information Value (IV) measures the predictive power of a variable.

\[IV = \sum (\%\,\text{non-event} - \%\,\text{event}) \times WoE\]

A common interpretation of IV is:

IVInterpretation
< 0.02No predictive power
0.02 – 0.1Weak
0.1 – 0.3Medium
0.3 – 0.5Strong
> 0.5Very strong

WoE and IV for Discrete Variables

I started by developing a function for discrete variables, and from there, I kept refining the approach.

Next, I used data from the Titanic - Machine Learning from Disaster competition to apply the WoE and IV function in Python. The results were quite interesting:

Survived01DistrWoEIVIV_total
female0.1475410.6812870.216562-1.5298770.8165651.341681
male0.8524590.3187132.6746880.9838330.5251161.341681
C0.1366120.2735290.499442-0.6942640.0950570.122728
Q0.0856100.0882350.970249-0.0302030.0000790.122728
S0.7777780.6382351.2186380.1977340.0275920.122728

The function generated a table that clearly highlights the WoE and IV values for each category, making it easier to understand which variables have a greater capacity to differentiate the event of interest.

If you are working with this same dataset (or any other), I highly recommend testing this approach. It is a very useful tool for data exploration and improving predictive models.

WoE and IV for Continuous Variables

When it comes to continuous variables, the process becomes a bit more challenging, mainly when defining the best discretization strategy. This is because different problems can require different approaches to binning.

After a few attempts and drafts, I chose to divide the data into deciles, which allows for a more stable analysis of the distribution and, if necessary, subsequent aggregation into quartiles or other groupings.

This choice resulted in a more interpretable and consistent approach for calculating WoE and IV in continuous variables.

The final function now generates more structured tables, making it easier to analyze the predictive power of each variable range:

variablelimit01DistrWoEIV
Age<=[14.]0.0582880.1315790.442987-0.8142140.06
Age[14.] a [19.]0.0965390.0994150.971070-0.0293560.00
Age[19.] a [22.]0.0874320.0555561.5737700.4534740.01
Age[22.] a [25.]0.0801460.0760231.0542240.0528050.00
Age[25.] a [28.]0.0673950.0701750.960383-0.0404240.00
Age[28.] a [31.8]0.0728600.0760230.958386-0.0425050.00
Age[31.8] a [36.]0.0856100.1286550.665425-0.4073300.02
Age[36.] a [41.]0.0619310.0555561.1147540.1086340.00
Age[41.] a [50.]0.0856100.0906430.944474-0.0571270.00
Age 1.0000001.0000001.0000000.0000000.09
Fare<=[7.55]0.1438980.0380123.7856241.3312110.14
Fare[7.55] a [7.8542]0.1111110.0760231.4615380.3794900.01
Fare[7.8542] a [8.05]0.1584700.0555562.8524591.0481810.11
Fare[8.05] a [10.5]0.1092900.0526322.0765030.7306850.04
Fare[10.5] a [14.4542]0.0874320.1052630.830601-0.1856060.00
Fare[14.4542] a [21.6792]0.0928960.1081870.858662-0.1523800.00
Fare[21.6792] a [27.]0.0783240.1345030.582324-0.5407290.03
Fare[27.] a [39.6875]0.1038250.0994151.0443590.0434030.00
Fare[39.6875] a [77.9583]0.0765030.1374270.556679-0.5857660.04
Fare 1.0000001.0000001.0000000.0000000.37

Consolidated Function for Continuous and Categorical Variables

After overcoming the initial challenges, I decided to take the implementation a step further and develop a consolidated function capable of calculating WoE and IV for both categorical and continuous variables within a single framework.

This approach simplifies the analysis process, providing a unified view of the predictive power of the variables.

The final table obtained is shown below:

variablelimit01DistrWoEIVIV_total
female 0.1475410.6812870.216562-1.5298770.8165651.341681
male 0.8524590.3187132.6746880.9838330.5251161.341681
C 0.1366120.2735290.499442-0.6942640.0950570.122728
Q 0.0856100.0882350.970249-0.0302030.0000790.122728
S 0.7777780.6382351.2186380.1977340.0275920.122728
Age<=[14.]0.0582880.1315790.442987-0.8142140.060000 
Age[14.] a [19.]0.0965390.0994150.971070-0.0293560.000000 
Age[19.] a [22.]0.0874320.0555561.5737700.4534740.010000 
Age[22.] a [25.]0.0801460.0760231.0542240.0528050.000000 
Age[25.] a [28.]0.0673950.0701750.960383-0.0404240.000000 
Age[28.] a [31.8]0.0728600.0760230.958386-0.0425050.000000 
Age[31.8] a [36.]0.0856100.1286550.665425-0.4073300.020000 
Age[36.] a [41.]0.0619310.0555561.1147540.1086340.000000 
Age[41.] a [50.]0.0856100.0906430.944474-0.0571270.000000 
Age 1.0000001.0000001.0000000.0000000.090000 
Fare<=[7.55]0.1438980.0380123.7856241.3312110.140000 
Fare[7.55] a [7.8542]0.1111110.0760231.4615380.3794900.010000 
Fare[7.8542] a [8.05]0.1584700.0555562.8524591.0481810.110000 
Fare[8.05] a [10.5]0.1092900.0526322.0765030.7306850.040000 
Fare[10.5] a [14.4542]0.0874320.1052630.830601-0.1856060.000000 
Fare[14.4542] a [21.6792]0.0928960.1081870.858662-0.1523800.000000 
Fare[21.6792] a [27.]0.0783240.1345030.582324-0.5407290.030000 
Fare[27.] a [39.6875]0.1038250.0994151.0443590.0434030.000000 
Fare[39.6875] a [77.9583]0.0765030.1374270.556679-0.5857660.040000 
Fare 1.0000001.0000001.0000000.0000000.370000 

Interpreting the Results

Looking at the results obtained from the Titanic dataset, we can draw some important conclusions about the predictive power of the variables:

  • The Sex variable showed an IV greater than 1.3, indicating very high predictive power.
  • The Fare variable showed an IV close to 0.37, suggesting a strong capacity for discrimination.
  • On the other hand, the Age variable showed a lower IV (0.09), indicating a weaker predictive contribution when analyzed in isolation.

These metrics are widely used in predictive modeling tasks and can support decisions such as:

  • feature selection;
  • scorecard building;
  • variable transformation for logistic regression;
  • identifying non-linear relationships between variables and the event of interest.

Conclusion

Using WoE and IV improve the interpretability and predictive capability of logistic regression models.

In addition to helping with feature selection, these metrics make it possible to understand how different ranges or categories influence the probability of the event of interest.

The functions developed in this post allow you to:

  • calculate WoE and IV for categorical variables;
  • automatically discretize continuous variables;
  • consolidate the results into a single table for exploratory analysis.

Code and Supplementary Materials

The code used in this post is available on GitHub:

The repository contains the functions developed to calculate WoE and IV, including:

  • categorical variables;
  • continuous variables;
  • consolidated function;
  • application examples using the Titanic dataset.

References

  • Anderson, Raymond. The Credit Scoring Toolkit: Theory and Practice for Retail Credit Risk Management and Decision Automation. Oxford University Press, 2007.

  • Siddiqi, Naeem. Credit Risk Scorecards: Developing and Implementing Intelligent Credit Scoring. Wiley, 2006.

  • Sudarson Mothilal Thoppay (2015). woe: Computes Weight of Evidence and Information Values. R package version 0.2. https://CRAN.R-project.org/package=woe

  • Thilo Eichenberg (2018). woeBinning: Supervised Weight of Evidence Binning of Numeric Variables and Factors. R package version 0.1.6. https://CRAN.R-project.org/package=woeBinning