Back to Inference: Hypothesis Testing and Beyond

Chi-Square and Categorical Data

The right test when both variables are categorical. Two forms — independence and goodness-of-fit. FIND_VIDEO: search 'chi-square test independence goodness of fit' — recommended channel: StatQuest. Aim for 10 min or under.

21 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. Chi-Square Definition — The test evaluates dependence between a non-negative feature and a categorical target class.
  2. Data Loading — Load the dataset and identify the specific categorical features requiring transformation.
  3. Label Encoding — String features must be converted to non-negative numerical inputs using techniques like numpy.where or dictionary mapping.
  4. Setup and Import — Split the data into training sets and import the Chi2 function from Scikit-learn.
  5. Execute Chi2 — Apply the chi2 function to the training data to generate the F-scores and P-values.
  6. Feature Ranking — Convert the P-values into a Pandas Series and sort them to rank features by predictive significance.
PDF notes

Frequently asked questions

What is the null hypothesis in this test?

The null hypothesis is that the feature and the target variable are statistically independent. We seek to reject this hypothesis.

What P-value is considered 'low' or significant?

Typically, a P-value below 0.05 is used to reject the null hypothesis, indicating the feature is significant.

Can I use One-Hot Encoding instead of Label Encoding?

Yes, but Label or Ordinal Encoding is often preferred here as Chi2 works well with integer representations of categories.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.