0
0
Data Analysis Pythondata~20 mins

Chi-squared test in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Chi-squared Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Chi-squared test output interpretation
What is the output of this Python code performing a chi-squared test on a contingency table?
Data Analysis Python
import numpy as np
from scipy.stats import chi2_contingency

# Contingency table: rows = Gender (Male, Female), columns = Preference (A, B)
data = np.array([[30, 10], [20, 40]])
chi2, p, dof, expected = chi2_contingency(data)
print(round(p, 3))
A0.001
B0.15
C0.05
D0.5
Attempts:
2 left
💡 Hint
Look at the p-value returned by chi2_contingency and round it to three decimals.
data_output
intermediate
2:00remaining
Expected frequencies from chi-squared test
Given this contingency table, what is the expected frequency for the cell in row 1, column 2 after running chi2_contingency?
Data Analysis Python
import numpy as np
from scipy.stats import chi2_contingency

data = np.array([[10, 20], [30, 40]])
chi2, p, dof, expected = chi2_contingency(data)
print(round(expected[0,1], 1))
A20.0
B18.0
C25.0
D30.0
Attempts:
2 left
💡 Hint
Expected frequency = (row total * column total) / grand total.
🔧 Debug
advanced
2:00remaining
Identify the error in chi-squared test code
What error does this code raise when trying to perform a chi-squared test?
Data Analysis Python
from scipy.stats import chi2_contingency

data = [[10, 20, 30], [5, 15]]
chi2, p, dof, expected = chi2_contingency(data)
AIndexError: list index out of range
BTypeError: unsupported operand type(s) for +: 'int' and 'list'
CValueError: all rows in a contingency table must be of the same length
DNameError: name 'chi2_contingency' is not defined
Attempts:
2 left
💡 Hint
Check the shape of the input data for chi2_contingency.
🧠 Conceptual
advanced
1:30remaining
Understanding degrees of freedom in chi-squared test
For a contingency table with 4 rows and 3 columns, what is the degrees of freedom used in the chi-squared test?
A6
B7
C11
D12
Attempts:
2 left
💡 Hint
Degrees of freedom = (number of rows - 1) * (number of columns - 1).
🚀 Application
expert
3:00remaining
Chi-squared test on survey data
You have survey data on smoking status (Smoker, Non-Smoker) and lung disease (Yes, No). Which option shows the correct Python code to perform a chi-squared test and print the p-value?
A
import pandas as pd
from scipy.stats import chi2_contingency

survey = pd.DataFrame({'Smoking': ['Smoker', 'Smoker', 'Non-Smoker', 'Non-Smoker'], 'Disease': ['Yes', 'No', 'Yes', 'No'], 'Count': [30, 70, 10, 90]})
contingency = survey.pivot(index='Smoking', columns='Disease', values='Count')
chi2, p, dof, expected = chi2_contingency(contingency)
print(round(p, 4))
B
import pandas as pd
from scipy.stats import chi2_contingency

survey = pd.DataFrame({'Smoking': ['Smoker', 'Smoker', 'Non-Smoker', 'Non-Smoker'], 'Disease': ['Yes', 'No', 'Yes', 'No'], 'Count': [30, 70, 10, 90]})
contingency = survey.groupby(['Smoking', 'Disease']).sum()
chi2, p, dof, expected = chi2_contingency(contingency)
print(p)
C
import pandas as pd
from scipy.stats import chi2_contingency

survey = pd.DataFrame({'Smoking': ['Smoker', 'Smoker', 'Non-Smoker', 'Non-Smoker'], 'Disease': ['Yes', 'No', 'Yes', 'No'], 'Count': [30, 70, 10, 90]})
contingency = survey.pivot(index='Disease', columns='Smoking', values='Count')
chi2, p, dof, expected = chi2_contingency(contingency)
print(round(p, 4))
D
import pandas as pd
from scipy.stats import chi2_contingency

survey = pd.DataFrame({'Smoking': ['Smoker', 'Smoker', 'Non-Smoker', 'Non-Smoker'], 'Disease': ['Yes', 'No', 'Yes', 'No'], 'Count': [30, 70, 10, 90]})
contingency = survey.pivot_table(index='Smoking', columns='Disease', values='Count', aggfunc='sum')
chi2, p, dof, expected = chi2_contingency(contingency)
print(round(p, 4))
Attempts:
2 left
💡 Hint
Use pivot_table with aggfunc='sum' to create the contingency table correctly.