Bird
0
0

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

medium📝 Debug Q14 of 15
SciPy - Statistical Tests

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)
AObserved data must be a NumPy array, not a list
BNo error, code runs correctly
Cchi2_contingency requires 1D data, not 2D
DMissing import for chi2_contingency
Step-by-Step Solution
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]
Quick Trick: chi2_contingency accepts lists or arrays for observed data [OK]
Common Mistakes:
MISTAKES
  • Thinking lists are invalid input
  • Assuming 1D data only
  • Forgetting to import function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes