SelectKBest
chi2
feature selection
machine learning
sklearn

How SelectKBest chi2 calculates score?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Feature selection is a crucial step in the preprocessing pipeline of machine learning. It helps in reducing the dimensionality of a dataset, which consequently improves the speed and accuracy of models and reduces overfitting. One of the popular methods for feature selection is `SelectKBest`, which is available in Scikit-learn—a well-known Python library for machine learning.

The `SelectKBest` function allows users to select features according to the k highest scores. Among the various scoring functions available, the Chi-Squared (χ2\chi^2) statistics is commonly used for classification tasks where the target variable is categorical. This article provides a detailed explanation of how `SelectKBest` using chi-square (χ2\chi^2) calculates the scores for selecting features.

Understanding Chi-Square (χ2\chi^2) Statistic

The chi-square test is a statistical test used to determine whether there is a significant association between two categorical variables. The χ2\chi^2 statistic measures how expectations compare with actual observed data. The formula for the chi-square statistic is:

χ2=(OE)2E\chi^2 = \sum \frac{(O - E)^2}{E}

where OO represents the observed frequency and EE represents the expected frequency of the occurrences.

Calculation Steps

  1. Create Contingency Table: • For each feature, cross-tabulate the values of the feature against the target variable. This forms a contingency table where each row sums to the frequency of the feature, and each column sums to the frequency of the class labels.
  2. Calculate Expected Frequencies: • Calculate the expected frequency for each cell in the contingency table using the formula: E_ij=sum of row×sum of columntotal number of samplesE\_{ij} = \frac{\text{sum of row} \times \text{sum of column}}{\text{total number of samples}} • Here, EijE_{ij} is the expected frequency for the entry of the ithi^{th} row and the jthj^{th} column.
  3. Compute Chi-Square Statistic: • Use the observed and expected frequencies to calculate the χ2\chi^2 statistic as shown in the formula above.
  4. Determine p-Value: • While `SelectKBest` primarily uses the χ2\chi^2 statistic for ranking features, the p-value could also be calculated for statistical significance assessment. It determines the probability of observing the data given that the null hypothesis is true (i.e., no association).
  5. Ranking Features: • The $ \chi^2 $ statistics calculated for each feature are used to rank them. `SelectKBest` selects the top k features with the highest $ \chi^2 $ scores for use in the learning algorithm.

Example

Consider a toy dataset with two features and a binary target variable.

InstanceFeature 1Feature 2Target
1HighYes1
2MediumNo0
3MediumYes1
4LowYes0
5LowNo1

Step-by-Step Calculation

Feature 1: • Contingency (example):

Class 1Class 0Total
High101
Medium112
Low112
Total325

Expected Frequency Calculation: • For example, the expected frequency for High-Class 1 is: E_HighClass1=1×35=0.6E\_{High-Class 1} = \frac{1 \times 3}{5} = 0.6

Calculate χ2\chi^2 for Feature 1: χ2=(10.6)20.6+(00.4)20.4+...\chi^2 = \frac{(1-0.6)^2}{0.6} + \frac{(0-0.4)^2}{0.4} + ...

Feature 2: • Similar calculations performed for contingency table and χ2\chi^2 statistic.

Conclusion

The `SelectKBest` with chi-square (χ2\chi^2) is highly valuable for feature selection in machine learning. By statistically quantifying the dependency between each feature and the target variable, it enables data scientists to efficiently reduce the number of input variables to the model. Despite its effectiveness, it is important to use this method primarily for categorical data, as it requires the features and target to be non-negative and discrete for meaningful calculations.

StepDescription
Create Contingency TableCross-tabulate feature vs. target
Calculate ExpectedUse marginal sums to compute expected frequencies
Compute χ2\chi^2Measure deviation of observed from expected values
Determine p-valueAssess statistical significance
Rank FeaturesSelect features with the highest χ2\chi^2 scores

In summary, the SelectKBest method with chi-square is a fundamental tool that empowers practitioners to build more efficient and interpretable machine learning models.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.