The Wayback Machine - https://web.archive.org/web/20240812095532/https://www.geeksforgeeks.org/chi-square-test-for-feature-selection-mathematical-explanation/
Open In App

Chi-Square Test for Feature Selection – Mathematical Explanation

Last Updated : 10 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

One of the primary tasks involved in any supervised Machine Learning venture is to select the best features from the given dataset to obtain the best results. One way to select these features is the Chi-Square Test. Mathematically, a Chi-Square test is done on two distributions two determine the level of similarity of their respective variances. In its null hypothesis, it assumes that the given distributions are independent. This test thus can be used to determine the best features for a given dataset by determining the features on which the output class label is most dependent. For each feature in the dataset, the \chi ^{2}  is calculated and then ordered in descending order according to the \chi ^{2}  value. The higher the value of \chi ^{2}  , the more dependent the output label is on the feature and higher the importance the feature has on determining the output. Let the feature in question have m attribute values and the output have k class labels. Then the value of \chi ^{2}  is given by the following expression:

\chi ^{2} = \sum _{i=1}^{m} \sum _{j=1}^{k}\frac{(O_{ij}-E_{ij})^{2}}{E_{ij}}
 

where O_{ij}  – Observed frequency E_{ij}  – Expected frequency For each feature, a contingency table is created with m rows and k columns. Each cell (i,j) denotes the number of rows having attribute feature as i and class label as k. Thus each cell in this table denotes the observed frequency. To calculate the expected frequency for each cell, first, the proportion of the feature value in the total dataset is calculated and then it is multiplied by the total number of the current class label. 

Solved Example: Consider the following table:

 

Here the output variable is the column named “PlayTennis” which determines whether tennis was played on the given day given the weather conditions. The contingency table for the feature “Outlook” is constructed as below:-  

Note: Expected value for each cell is given inside the parenthesis. The expected value for the cell (Sunny,Yes) is calculated as \frac{5}{14}\times 9 = 3.21  and similarly for others. The \chi ^{2}_{outlook}  value is calculated as below:- \chi ^{2}_{outlook} = \frac{(2-3.21)^{2}}{3.21}+\frac{(3-1.79)^{2}}{1.79}+\frac{(4-2.57)^{2}}{2.57}+\frac{(0-1.43)^{2}}{1.43}+\frac{(3-3.21)^{2}}{3.21}+\frac{(2-1.79)^{2}}{1.79}  [Tex]\Rightarrow \chi ^{2}_{outlook} = 3.129  [/Tex]

The contingency table for the feature “Wind” is constructed as below:

 

The \chi ^{2}_{wind}  value is calculated as below:- \chi ^{2}_{wind} = \frac{(3-3.86)^{2}}{3.86}+\frac{(3-1.14)^{2}}{1.14}+\frac{(6-5.14)^{2}}{5.14}+\frac{(2-2.86)^{2}}{2.86}  [Tex]\Rightarrow \chi ^{2}_{wind} = 3.629  [/Tex]On comparing the two scores, we can conclude that the feature “Wind” is more important to determine the output than the feature “Outlook”. This article demonstrates how to do feature selection using Chi-Square Test.

The chi-square test is a statistical method that can be used for feature selection in machine learning. It is used to determine whether there is a significant association between two categorical variables. In the context of feature selection, the chi-square test can be used to identify the features that are most strongly associated with the target variable.

Mathematically, the chi-square test involves calculating the chi-square statistic, which is a measure of the difference between the observed frequency of each category and the expected frequency under the null hypothesis of no association between the variables.

The chi-square statistic is calculated as follows:

χÂē = ÎĢ((O - E)Âē / E)

where:

χÂē is the chi-square statistic
O is the observed frequency of each category
E is the expected frequency of each category, which is calculated under the assumption of no association between the variables
The expected frequency for each category is calculated as follows:

E = (row total x column total) / grand total

where:

row total is the total number of observations in the row
column total is the total number of observations in the column
grand total is the total number of observations in the entire dataset
Once the chi-square statistic has been calculated for each feature, the p-value can be calculated using the chi-square distribution with (number of categories – 1) degrees of freedom. The p-value represents the probability of observing a chi-square statistic as extreme as the one calculated, assuming that there is no association between the variables.

Features with low p-values are considered to be more strongly associated with the target variable and are selected for further analysis or modeling.

In summary, the chi-square test is a statistical method that can be used for feature selection by measuring the association between categorical variables. The test involves calculating the chi-square statistic and p-value and selecting features with low p-values as being more strongly associated with the target variable.

Advantages of using the chi-square test for feature selection include:

  1. Simple and easy to use: The chi-square test is a simple and widely-used statistical method that can be easily applied for feature selection in machine learning.
  2. Computationally efficient: The chi-square test is computationally efficient and can be applied to large datasets with many features.


Previous Article
Next Article

Similar Reads

Difference Between Feature Selection and Feature Extraction
Machine learning models require input features that are relevant and important to predict the outcome. However, not all features are equally important for a prediction task, and some features might even introduce noise in the model. Feature selection and feature extraction are two methods to handle this problem. In this article, we will explore the
8 min read
Python - Pearson's Chi-Square Test
In this article, we will perform Pearson’s Chi-Square test using a mathematical approach and then using Python's SciPy module. It is an important statistic test in data science for categorical column selection. generally in data science projects, we select only those columns which are important and are not correlated with each other. Pearson’s Chi-
5 min read
How to Perform a Chi-Square Goodness of Fit Test in Python
In this article, we are going to see how to Perform a Chi-Square Goodness of Fit Test in Python The Chi-Square Goodness of fit test is a non-parametric statistical hypothesis test that's used to determine how considerably the observed value of an event differs from the expected value. it helps us check whether a variable comes from a certain distri
3 min read
Chi-square test in Machine Learning
Chi-Square test is a statistical method crucial for analyzing associations in categorical data. Its applications span various fields, aiding researchers in understanding relationships between factors. This article elucidates Chi-Square types, steps for implementation, and its role in feature selection, exemplified through Python code on the Iris da
11 min read
Mathematical explanation for Linear Regression working
Suppose we are given a dataset: Given is a Work vs Experience dataset of a company and the task is to predict the salary of a employee based on his / her work experience. This article aims to explain how in reality Linear regression mathematically works when we use a pre-defined function to perform prediction task. Let us explore how the stuff work
1 min read
ML | Mathematical explanation of RMSE and R-squared error
RMSE: Root Mean Square Error is the measure of how well a regression line fits the data points. RMSE can also be construed as Standard Deviation in the residuals. Consider the given data points: (1, 1), (2, 2), (2, 3), (3, 6). Let us break the above data points into 1-d lists. Input: x = [1, 2, 2, 3] y = [1, 2, 3, 6] Code: Regression Graph Python C
5 min read
Mathematical explanation of K-Nearest Neighbour
KNN stands for K-nearest neighbour, it’s one of the Supervised learning algorithm mostly used for classification of data on the basis how it’s neighbour are classified. KNN stores all available cases and classifies new cases based on a similarity measure. K in KNN is a parameter that refers to the number of the nearest neighbours to include in the
2 min read
Chi-square distance in Python
Chi-square distance calculation is a statistical method, generally measures similarity between 2 feature matrices. Such distance is generally used in many applications like similar image retrieval, image texture, feature extractions etc. The Chi-square distance of 2 arrays ‘x’ and ‘y’ with ‘n’ dimension is mathematically calculated using below form
2 min read
Is There Any Difference Between Feature Extraction and Feature Learning?
Answer: Yes, feature extraction involves manually selecting characteristics from data, while feature learning allows models to automatically discover the features to be used for a task.Feature extraction and feature learning represent two methodologies in machine learning for identifying and utilizing relevant information from raw data to improve m
1 min read
Parameters for Feature Selection
Prerequisite : Introduction to Dimensionality Reduction Dimensionality reduction is the process of reducing the number of random variables under consideration, by obtaining a set of principal variables. It can be divided into feature selection and feature extraction. Dimensionality Reduction is an important factor in predictive modeling. Various pr
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg