Bird
Raised Fist0
SciPydata~5 mins

Chi-squared test in SciPy - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of the Chi-squared test?
The Chi-squared test checks if there is a significant difference between expected and observed data in categories. It helps us see if two things are related or independent.
Click to reveal answer
beginner
What are the two main types of Chi-squared tests?
1. Chi-squared test for independence: checks if two categorical variables are related.
2. Chi-squared goodness-of-fit test: checks if observed data fits an expected distribution.
Click to reveal answer
intermediate
In Python's scipy library, which function is used to perform a Chi-squared test for independence?
The function is scipy.stats.chi2_contingency(). It takes a contingency table (observed counts) and returns the test statistic, p-value, degrees of freedom, and expected frequencies.
Click to reveal answer
beginner
What does a low p-value (e.g., less than 0.05) mean in a Chi-squared test?
A low p-value means the observed data is unlikely if the variables were independent. So, we reject the idea that they are independent and say there is a relationship.
Click to reveal answer
beginner
What is a contingency table in the context of the Chi-squared test?
A contingency table is a grid that shows counts of observations for combinations of two categorical variables. It helps organize data to perform the Chi-squared test.
Click to reveal answer
Which scipy function is used for the Chi-squared test of independence?
Ascipy.stats.ttest_ind
Bscipy.stats.chi2_contingency
Cscipy.stats.f_oneway
Dscipy.stats.pearsonr
What does the Chi-squared test compare?
AObserved and expected frequencies
BMeans of two groups
CCorrelation between two variables
DVariance of a dataset
If the p-value from a Chi-squared test is 0.03, what should you do at a 0.05 significance level?
AReject the null hypothesis
BFail to reject the null hypothesis
CIncrease the sample size
DIgnore the result
Which data structure is needed to perform a Chi-squared test of independence?
AList of means
BTime series
CScatter plot
DContingency table
The Chi-squared test is best suited for which type of data?
ATime series data
BContinuous data
CCategorical data
DText data
Explain how to perform a Chi-squared test of independence using scipy with a simple example.
Think about a table showing counts of two categories and how to check if they relate.
You got /3 concepts.
    Describe what a p-value tells you in the context of a Chi-squared test.
    Focus on what the p-value means about the relationship between variables.
    You got /3 concepts.

      Practice

      (1/5)
      1.

      What does the Chi-squared test mainly check in data?

      easy
      A. If two categorical variables are related
      B. If the mean of a dataset is zero
      C. If data follows a normal distribution
      D. If two numerical variables have a linear relationship

      Solution

      1. Step 1: Understand the purpose of the Chi-squared test

        The Chi-squared test is used to check if there is a relationship between two categorical variables by comparing observed and expected counts.
      2. Step 2: Compare options with the test purpose

        Only If two categorical variables are related correctly describes this purpose. Options B, C, and D describe other statistical tests or concepts.
      3. Final Answer:

        If two categorical variables are related -> Option A
      4. Quick Check:

        Chi-squared test = relationship between categories [OK]
      Hint: Chi-squared tests category links, not means or correlations [OK]
      Common Mistakes:
      • Confusing Chi-squared with tests for means
      • Thinking it tests numerical correlations
      • Mixing it up with normality tests
      2.

      Which of the following is the correct way to import the Chi-squared test function from scipy.stats?

      easy
      A. from scipy.stats import chi2_contingency
      B. import scipy.stats.chi2_contingency
      C. from scipy import chi2_contingency
      D. import chi2_contingency from scipy.stats

      Solution

      1. Step 1: Recall correct Python import syntax

        To import a specific function from a module, use from module import function. Here, the function is chi2_contingency from scipy.stats.
      2. Step 2: Check each option's syntax

        from scipy.stats import chi2_contingency uses the correct syntax. import scipy.stats.chi2_contingency tries to import a submodule as a whole which is invalid. from scipy import chi2_contingency imports from the wrong module. import chi2_contingency from scipy.stats uses incorrect import order.
      3. Final Answer:

        from scipy.stats import chi2_contingency -> Option A
      4. Quick Check:

        Correct import = from scipy.stats import chi2_contingency [OK]
      Hint: Use 'from module import function' for specific imports [OK]
      Common Mistakes:
      • Using dot notation in import incorrectly
      • Importing from wrong module
      • Wrong import order syntax
      3.

      What is the output of this code snippet?

      import numpy as np
      from scipy.stats import chi2_contingency
      
      # Observed data in a 2x2 table
      observed = np.array([[10, 20], [20, 40]])
      chi2, p, dof, expected = chi2_contingency(observed)
      print("{:.3f}".format(p))
      medium
      A. 1.000
      B. 0.045
      C. 0.158
      D. 0.500

      Solution

      1. Step 1: Run the chi2_contingency on the observed table

        The observed counts are [[10, 20], [20, 40]]. The function calculates the Chi-squared statistic and p-value testing independence.
      2. Step 2: Interpret the p-value output

        Running the code gives a p-value of approximately 0.158, meaning there is no strong evidence against independence.
      3. Final Answer:

        0.158 -> Option C
      4. Quick Check:

        p-value from chi2_contingency ≈ 0.158 [OK]
      Hint: Run chi2_contingency and check p-value for independence [OK]
      Common Mistakes:
      • Confusing chi2 statistic with p-value
      • Rounding incorrectly
      • Misreading output order
      4.

      Identify the error in this code that tries to perform a Chi-squared test:

      import numpy as np
      from scipy.stats import chi2_contingency
      
      observed = [[15, 25], [35, 25]]
      chi2, p, dof, expected = chi2_contingency(observed)
      print(p)
      medium
      A. Observed data must be a NumPy array, not a list
      B. No error, code runs correctly
      C. chi2_contingency requires 1D data, not 2D
      D. Missing import for chi2_contingency

      Solution

      1. Step 1: Check data type requirements for chi2_contingency

        The function accepts array-like input, including lists of lists, so the observed data as a list of lists is valid.
      2. Step 2: Verify imports and function usage

        The import is correct, and the function call matches the expected signature. The code will run and print the p-value.
      3. Final Answer:

        No error, code runs correctly -> Option B
      4. Quick Check:

        chi2_contingency accepts lists; code runs fine [OK]
      Hint: chi2_contingency accepts lists or arrays for observed data [OK]
      Common Mistakes:
      • Thinking lists are invalid input
      • Assuming 1D data only
      • Forgetting to import function
      5.

      You have this observed frequency table for two groups and two outcomes:

      observed = [[30, 10], [20, 40]]

      You want to test if the groups differ in outcome distribution. Which step is NOT part of the Chi-squared test process using scipy.stats.chi2_contingency?

      hard
      A. Calculate expected frequencies assuming independence
      B. Compute the Chi-squared statistic comparing observed and expected
      C. Check if the p-value is less than your significance level
      D. Normalize the observed data to sum to 1 before testing

      Solution

      1. Step 1: Understand the Chi-squared test steps

        The test calculates expected counts assuming independence, then computes the Chi-squared statistic and p-value, and finally compares p-value to significance level.
      2. Step 2: Identify the incorrect step

        Normalizing observed data to sum to 1 is not part of the Chi-squared test. The test uses raw counts, not proportions.
      3. Final Answer:

        Normalize the observed data to sum to 1 before testing -> Option D
      4. Quick Check:

        Chi-squared uses counts, not normalized data [OK]
      Hint: Chi-squared test uses counts, not proportions [OK]
      Common Mistakes:
      • Normalizing data before test
      • Confusing proportions with counts
      • Ignoring p-value significance check
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard