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-Squared IntroductionThe Chi-Squared test is introduced as a statistical method for feature selection, often used in univariate or bivariate analysis.
  2. Test Application CriteriaChi-Squared computes statistics between non-negative features and the target class, specifically for categorical variables in classification.
  3. Data Setup and SelectionThe Titanic dataset is loaded, and relevant categorical features are selected alongside the target variable, 'survived'.
  4. Label Encoding RequirementIt is stated that label encoding must be performed on all categorical features before applying the Chi-Squared test.
  5. Encoding Binary FeaturesLabel encoding is demonstrated for the binary 'sex' column using `np.where` to convert 'male' and 'female' to 1 and 0.
  6. Encoding Multi-Category FeaturesA dictionary comprehension approach is used to assign ordinal labels to the multi-category 'embarked' column.
  7. Encoding Boolean FeaturesThe 'alone' boolean column is label encoded into 1s and 0s using `np.where`.
  8. Train Test SplitTrain-test split is implemented using `sklearn` to separate the data into training sets, which is necessary to avoid overfitting.
  9. Performing Chi-SquaredThe `sklearn.feature_selection.chi2` function is imported and prepared to run on the training data.
  10. Interpreting Chi-Squared OutputIt is explained that the `chi2` function returns two values: the F-score, which is the statistic, and the P-value.
PDF notes

Frequently asked questions

Why is label encoding mandatory before applying the Chi-Squared test?

Chi-Squared requires numerical, non-negative input (like counts or discrete labels) to calculate the expected frequencies accurately.

What does it mean if the P-value is less than 0.05?

A low P-value suggests the observed relationship between the feature and the target is statistically significant, meaning the feature is likely important.

Can I use Chi-Squared for regression tasks?

No, the score is specifically designed to evaluate categorical variables in a classification task relative to the output class.