0
0
SciPydata~20 mins

Chi-squared test in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Chi-squared Test Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A3.45 0.178 2
B5.12 0.074 3
C4.67 0.097 2
D6.89 0.032 2
Attempts:
2 left
💡 Hint
Recall that degrees of freedom for a 2x3 table is (2-1)*(3-1).
data_output
intermediate
2: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)
A
[[15. 25.]
 [ 5. 15.]]
B
[[14. 26.]
 [ 6. 14.]]
C
[[10. 30.]
 [10. 10.]]
D
[[12. 28.]
 [ 8. 12.]]
Attempts:
2 left
💡 Hint
Expected frequencies are calculated based on row and column totals.
🧠 Conceptual
advanced
1: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?
AReject the null hypothesis; there is evidence of association.
BFail to reject the null hypothesis; no evidence of association.
CThe test is inconclusive; p-value is too close to 0.05.
DAccept the null hypothesis; variables are independent.
Attempts:
2 left
💡 Hint
Compare p-value to significance level alpha = 0.05.
🔧 Debug
advanced
1: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)
ATypeError: Expected 2D array, got 1D array instead
BValueError: Input contains NaN
CIndexError: list index out of range
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check the shape of the observed data passed to chi2_contingency.
🚀 Application
expert
2: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?
AUse scipy.stats.ttest_ind with raw data of ages and fruits.
BUse scipy.stats.chi2_contingency with a 2x3 contingency table of counts.
CUse scipy.stats.chisquare with a 1D array of fruit counts.
DUse scipy.stats.chi2_contingency with raw categorical data arrays.
Attempts:
2 left
💡 Hint
Chi-squared test for independence requires a contingency table.