The Chi-squared test helps us check if two things are related or if they happen by chance.
Chi-squared test in SciPy
from scipy.stats import chi2_contingency chi2, p, dof, expected = chi2_contingency(observed_table)
observed_table is a 2D list or array with counts.
The function returns four values: chi-squared statistic, p-value, degrees of freedom, and expected counts.
from scipy.stats import chi2_contingency observed = [[10, 20], [20, 40]] chi2, p, dof, expected = chi2_contingency(observed) print(f"Chi2: {chi2}, p-value: {p}")
from scipy.stats import chi2_contingency observed = [[30, 10, 20], [20, 40, 10]] chi2, p, dof, expected = chi2_contingency(observed) print(f"Degrees of freedom: {dof}")
This program tests if preference depends on gender using a 2x2 table.
from scipy.stats import chi2_contingency # Data: Survey results of preference by gender # Rows: Male, Female # Columns: Like, Dislike observed = [[30, 10], [20, 40]] chi2, p, dof, expected = chi2_contingency(observed) print(f"Chi-squared statistic: {chi2:.2f}") print(f"p-value: {p:.4f}") print(f"Degrees of freedom: {dof}") print("Expected frequencies:") for row in expected: print([round(x, 2) for x in row])
A small p-value (usually less than 0.05) means the two things are likely related.
The test needs counts, not percentages or averages.
Tables should have enough data; very small counts can make results unreliable.
The Chi-squared test checks if two categories are connected.
Use it with tables of counts to find relationships.
Look at the p-value to decide if the result is important.
Practice
What does the Chi-squared test mainly check in data?
Solution
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.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.Final Answer:
If two categorical variables are related -> Option AQuick Check:
Chi-squared test = relationship between categories [OK]
- Confusing Chi-squared with tests for means
- Thinking it tests numerical correlations
- Mixing it up with normality tests
Which of the following is the correct way to import the Chi-squared test function from scipy.stats?
Solution
Step 1: Recall correct Python import syntax
To import a specific function from a module, usefrom module import function. Here, the function ischi2_contingencyfromscipy.stats.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.Final Answer:
from scipy.stats import chi2_contingency -> Option AQuick Check:
Correct import = from scipy.stats import chi2_contingency [OK]
- Using dot notation in import incorrectly
- Importing from wrong module
- Wrong import order syntax
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))Solution
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.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.Final Answer:
0.158 -> Option CQuick Check:
p-value from chi2_contingency ≈ 0.158 [OK]
- Confusing chi2 statistic with p-value
- Rounding incorrectly
- Misreading output order
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)
Solution
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.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.Final Answer:
No error, code runs correctly -> Option BQuick Check:
chi2_contingency accepts lists; code runs fine [OK]
- Thinking lists are invalid input
- Assuming 1D data only
- Forgetting to import function
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?
Solution
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.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.Final Answer:
Normalize the observed data to sum to 1 before testing -> Option DQuick Check:
Chi-squared uses counts, not normalized data [OK]
- Normalizing data before test
- Confusing proportions with counts
- Ignoring p-value significance check
Ready to prove it?
Pass the test and this pattern moves unknown
