Challenge - 5 Problems
Chi-squared Test Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Chi-squared test on observed frequencies
What is the output of this code snippet performing a chi-squared test on observed frequencies?
SciPy
from scipy.stats import chi2_contingency observed = [[10, 20, 30], [6, 9, 17]] chi2, p, dof, expected = chi2_contingency(observed) print(round(chi2, 2), round(p, 3), dof)
Attempts:
2 left
💡 Hint
Recall that degrees of freedom for a 2x3 table is (2-1)*(3-1).
✗ Incorrect
The chi-squared test compares observed frequencies to expected frequencies. The degrees of freedom is (rows-1)*(columns-1) = 2. The test statistic and p-value are calculated by scipy.stats.chi2_contingency.
❓ data_output
intermediate2:00remaining
Expected frequencies from chi-squared test
Given the observed data, which option shows the correct expected frequencies matrix from the chi-squared test?
SciPy
from scipy.stats import chi2_contingency observed = [[15, 25], [5, 15]] _, _, _, expected = chi2_contingency(observed) print(expected)
Attempts:
2 left
💡 Hint
Expected frequencies are calculated based on row and column totals.
✗ Incorrect
The expected frequencies are computed by (row total * column total) / grand total for each cell. scipy.stats.chi2_contingency returns this matrix.
🧠 Conceptual
advanced1:30remaining
Interpreting p-value from chi-squared test
If a chi-squared test returns a p-value of 0.03, what does this mean about the null hypothesis at a 5% significance level?
Attempts:
2 left
💡 Hint
Compare p-value to significance level alpha = 0.05.
✗ Incorrect
A p-value less than 0.05 means the observed data is unlikely under the null hypothesis, so we reject it and conclude there is an association.
🔧 Debug
advanced1:30remaining
Identify the error in chi-squared test code
What error will this code raise when run?
SciPy
from scipy.stats import chi2_contingency observed = [10, 20, 30] chi2, p, dof, expected = chi2_contingency(observed)
Attempts:
2 left
💡 Hint
Check the shape of the observed data passed to chi2_contingency.
✗ Incorrect
chi2_contingency expects a 2D array (contingency table). Passing a 1D list causes a ValueError about input shape.
🚀 Application
expert2:30remaining
Choosing correct chi-squared test for data
You have a dataset with two categorical variables: 'Favorite Fruit' (Apple, Banana, Cherry) and 'Age Group' (Child, Adult). You want to test if fruit preference depends on age group. Which scipy function and input format should you use?
Attempts:
2 left
💡 Hint
Chi-squared test for independence requires a contingency table.
✗ Incorrect
The chi2_contingency function tests independence between two categorical variables using a contingency table of counts. Raw categorical arrays or 1D counts alone are not suitable inputs.