Bird
Raised Fist0
ML Pythonml~20 mins

Mutual information for feature selection in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Mutual Information Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Mutual Information in Feature Selection

Which statement best describes the role of mutual information in feature selection?

AIt quantifies the amount of shared information between a feature and the target variable, capturing any kind of dependency.
BIt measures the linear correlation between features and the target variable.
CIt ranks features based on their variance across the dataset.
DIt removes features that have missing values in the dataset.
Attempts:
2 left
💡 Hint

Think about how mutual information captures relationships beyond just linear ones.

Predict Output
intermediate
2:00remaining
Output of Mutual Information Calculation

What is the output of the following Python code that calculates mutual information between features and a binary target?

ML Python
from sklearn.feature_selection import mutual_info_classif
import numpy as np

X = np.array([[1, 2, 3], [1, 3, 3], [0, 2, 1], [0, 3, 1]])
y = np.array([0, 1, 0, 1])

mi = mutual_info_classif(X, y, discrete_features=[True, True, True], random_state=0)
print([round(v, 2) for v in mi])
A[0.0, 0.0, 0.0]
B[0.19, 0.0, 0.0]
C[0.0, 0.19, 0.0]
D[0.19, 0.19, 0.0]
Attempts:
2 left
💡 Hint

Mutual information is non-negative and measures dependency; check which feature varies with the target.

Model Choice
advanced
2:00remaining
Choosing Features Based on Mutual Information

You have 10 features and their mutual information scores with the target. Which approach best selects features to improve model performance?

ASelect the top 3 features with the highest mutual information scores only.
BSelect features with mutual information scores above zero, regardless of redundancy between features.
CSelect features randomly to avoid bias from mutual information scores.
DSelect features with high mutual information scores and low mutual information among themselves to reduce redundancy.
Attempts:
2 left
💡 Hint

Consider both relevance to target and redundancy among features.

Hyperparameter
advanced
2:00remaining
Hyperparameter Impact on Mutual Information Estimation

When using mutual_info_classif from scikit-learn, which hyperparameter affects the smoothness of the mutual information estimate for continuous features?

A<code>n_neighbors</code>
B<code>random_state</code>
C<code>discrete_features</code>
D<code>copy</code>
Attempts:
2 left
💡 Hint

Think about parameters controlling neighborhood size in nearest neighbor estimation.

🔧 Debug
expert
2:00remaining
Debugging Mutual Information Calculation Error

What error will the following code raise when calculating mutual information, and why?

from sklearn.feature_selection import mutual_info_classif
import numpy as np

X = np.array([[1.5, 2.3], [3.1, 4.7], [5.2, 6.8]])
y = np.array([0, 1, 0])

mi = mutual_info_classif(X, y, discrete_features=True)
print(mi)
ARuntimeWarning due to division by zero in mutual information calculation.
BTypeError because <code>discrete_features</code> must be a boolean or array-like, not a single boolean when X has continuous features.
CNo error; the code runs and outputs mutual information scores.
DValueError because the number of samples in X and y do not match.
Attempts:
2 left
💡 Hint

Check the type and shape of discrete_features parameter relative to input data.

Practice

(1/5)
1. What does mutual information measure in feature selection?
easy
A. The amount of shared information between a feature and the target variable
B. The correlation coefficient between two features
C. The difference between feature means
D. The number of missing values in a feature

Solution

  1. Step 1: Understand mutual information concept

    Mutual information measures how much knowing one variable reduces uncertainty about another.
  2. Step 2: Apply to feature selection context

    In feature selection, it measures how much information a feature shares with the target variable.
  3. Final Answer:

    The amount of shared information between a feature and the target variable -> Option A
  4. Quick Check:

    Mutual information = shared info [OK]
Hint: Mutual info = shared info between feature and target [OK]
Common Mistakes:
  • Confusing mutual information with correlation
  • Thinking it measures missing data
  • Assuming it measures difference in means
2. Which Python function is used to compute mutual information for classification tasks?
easy
A. mutual_info_classif
B. mutual_info_regression
C. mutual_info_score
D. mutual_info_classifier

Solution

  1. Step 1: Recall mutual information functions in sklearn

    For classification, sklearn provides mutual_info_classif.
  2. Step 2: Differentiate from regression function

    mutual_info_regression is for regression, not classification.
  3. Final Answer:

    mutual_info_classif -> Option A
  4. Quick Check:

    Classification uses mutual_info_classif [OK]
Hint: Classification uses mutual_info_classif function [OK]
Common Mistakes:
  • Using mutual_info_regression for classification
  • Confusing function names
  • Assuming mutual_info_score exists in sklearn
3. Given this code snippet, what is the output?
from sklearn.feature_selection import mutual_info_classif
import numpy as np
X = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])
y = np.array([0, 1, 0, 1])
mi = mutual_info_classif(X, y, discrete_features=[True, True])
print(np.round(mi, 2))
medium
A. [0.0 0.0]
B. [0.69 0.0]
C. [0.0 0.69]
D. [0.69 0.69]

Solution

  1. Step 1: Understand input data and parameters

    X has two discrete features, y is binary. Using mutual_info_classif with discrete_features=True for both.
  2. Step 2: Calculate mutual information values

    Both features vary similarly with y, so both have similar mutual information around 0.69 (close to ln(2)).
  3. Final Answer:

    [0.69 0.69] -> Option D
  4. Quick Check:

    Both features share info with y ~0.69 [OK]
Hint: Discrete features with binary target give ~0.69 MI if informative [OK]
Common Mistakes:
  • Assuming zero mutual information for all features
  • Mixing up discrete_features parameter
  • Rounding errors in output
4. Identify the error in this code snippet for mutual information feature selection:
from sklearn.feature_selection import mutual_info_classif
X = [[1, 2], [2, 3], [3, 4]]
y = [0, 1, 0]
mi = mutual_info_classif(X, y)
print(mi)
medium
A. y should be a 2D array, not 1D
B. X should be a numpy array, not a list of lists
C. mutual_info_classif requires discrete_features parameter
D. mutual_info_classif cannot handle integer data

Solution

  1. Step 1: Check input data types

    mutual_info_classif expects numpy arrays or similar, not plain Python lists.
  2. Step 2: Identify error cause

    Passing list of lists for X can cause unexpected behavior or errors; converting to numpy array fixes this.
  3. Final Answer:

    X should be a numpy array, not a list of lists -> Option B
  4. Quick Check:

    Use numpy arrays for X [OK]
Hint: Always convert input data to numpy arrays before sklearn functions [OK]
Common Mistakes:
  • Thinking y must be 2D
  • Assuming discrete_features is always required
  • Believing mutual_info_classif rejects integer data
5. You have a dataset with 10 features. After computing mutual information scores, you find two features have the highest scores but are highly correlated with each other. What is the best approach to select features?
hard
A. Select both features because they have the highest mutual information
B. Select features randomly to avoid bias
C. Select only one of the two correlated features with the highest mutual information
D. Discard both features to avoid redundancy

Solution

  1. Step 1: Understand mutual information and correlation

    High mutual information means features are informative, but high correlation means redundancy.
  2. Step 2: Choose features to reduce redundancy

    To avoid redundant information, select only one of the correlated features with the highest mutual information.
  3. Final Answer:

    Select only one of the two correlated features with the highest mutual information -> Option C
  4. Quick Check:

    Pick one correlated feature with highest MI [OK]
Hint: Avoid redundant features by picking one with highest MI [OK]
Common Mistakes:
  • Selecting both correlated features causing redundancy
  • Discarding informative features unnecessarily
  • Choosing features randomly without criteria