0
0
SciPydata~30 mins

Chi-squared test in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Chi-squared Test for Independence
📖 Scenario: You work in a small shop and want to check if the type of product customers buy depends on their age group. You collected data on how many customers in each age group bought each product type.
🎯 Goal: You will create a table of observed counts, set up the test, run the chi-squared test for independence, and print the test results to see if product choice depends on age group.
📋 What You'll Learn
Create a 2D list called observed with exact counts for each age group and product type
Create a variable called alpha and set it to 0.05 for the significance level
Use scipy.stats.chi2_contingency on observed to get the test results
Print the chi-squared statistic, p-value, degrees of freedom, and expected frequencies
💡 Why This Matters
🌍 Real World
Chi-squared tests help businesses understand if two categories, like age group and product choice, are related or independent.
💼 Career
Data analysts and scientists use chi-squared tests to analyze survey data, customer behavior, and other categorical data to make informed decisions.
Progress0 / 4 steps
1
Create the observed data table
Create a 2D list called observed with these exact counts: [[30, 10, 20], [20, 20, 20], [10, 30, 10]]. Each inner list represents an age group and each number is the count of customers buying a product type.
SciPy
Need a hint?

Use a list of lists to represent the table of counts.

2
Set the significance level
Create a variable called alpha and set it to 0.05 to represent the significance level for the test.
SciPy
Need a hint?

The significance level is usually 0.05 for many tests.

3
Run the chi-squared test
Import chi2_contingency from scipy.stats. Use chi2_contingency on the observed table and save the results in variables chi2, p, dof, and expected.
SciPy
Need a hint?

Use from scipy.stats import chi2_contingency and then call it with observed.

4
Print the test results
Print the variables chi2, p, dof, and expected to see the chi-squared statistic, p-value, degrees of freedom, and expected frequencies.
SciPy
Need a hint?

Use four print statements, one for each variable.