Mutual Information Feature Selection
Introduction
Welcome! In today's lesson, we are diving into the concept of Mutual Information for Feature Selection within the context of dimensionality reduction. By the end of this lesson, you'll understand how to use Mutual Information to measure the significance of features in a dataset, thus leading to more efficient model computation by selecting the most relevant features.
We’ll use the built-in mtcars dataset and visualize feature importance using a bar plot.
Understanding Mutual Information
Mutual Information (MI) measures how much knowing one variable reduces uncertainty about another. In feature selection, a larger MI indicates a more informative feature with respect to the target.
How Feature Selection using Mutual Information Works
R Implementation of Mutual Information (from scratch)
Below is an R function to compute Mutual Information (MI) between a feature and the target variable. This implementation works for numeric features by discretizing them into bins, then calculating MI based on the joint and marginal probabilities.
- Discretization: Numeric features are divided into quantile-based bins. This step is necessary because MI is typically computed on categorical data.
- Contingency Table: A table is created to count the occurrences of each combination of binned feature values and target classes.
- Probability Calculation: The counts are converted to joint and marginal probabilities.
- MI Calculation: The MI formula is applied by summing over all combinations where the joint probability is greater than zero.
This function returns the MI value (in nats) for a given feature and the target. A higher MI indicates a stronger relationship between the feature and the target variable.

