Bird
Raised Fist0
SciPydata~30 mins

t-test (ttest_ind, ttest_rel) in SciPy - Mini Project: Build & Apply

Choose your learning style10 modes available
Performing Independent and Paired t-tests with SciPy
📖 Scenario: You are a data analyst working with a health study team. They collected two sets of data:Step 1: Blood pressure readings from two different groups of patients (Group A and Group B).Step 2: Blood pressure readings from the same patients before and after a treatment.Your job is to use t-tests to find out if there are significant differences between these groups.
🎯 Goal: Build a Python program that:Creates two lists of blood pressure readings for Group A and Group B.Creates two lists of blood pressure readings for the same patients before and after treatment.Uses ttest_ind to compare Group A and Group B.Uses ttest_rel to compare before and after treatment readings.Prints the t-test statistics and p-values for both tests.
📋 What You'll Learn
Create lists with exact blood pressure values for Group A and Group B.
Create lists with exact blood pressure values for before and after treatment.
Import ttest_ind and ttest_rel from scipy.stats.
Use ttest_ind with Group A and Group B lists.
Use ttest_rel with before and after treatment lists.
Print the t-statistic and p-value for both tests.
💡 Why This Matters
🌍 Real World
T-tests are used in health studies, marketing, and social sciences to compare groups and measure effects of treatments or changes.
💼 Career
Data analysts and scientists use t-tests to validate hypotheses and support decision-making with data.
Progress0 / 4 steps
1
Create blood pressure data lists for two groups
Create two lists called group_a and group_b with these exact blood pressure readings: group_a = [120, 122, 119, 130, 128] and group_b = [115, 117, 113, 120, 118].
SciPy
Hint

Use square brackets to create lists with the exact numbers given.

2
Create blood pressure data lists for before and after treatment
Create two lists called before_treatment and after_treatment with these exact blood pressure readings: before_treatment = [130, 128, 135, 140, 138] and after_treatment = [125, 123, 130, 135, 132].
SciPy
Hint

Use the exact variable names and values given to create the lists.

3
Import t-test functions and perform tests
Import ttest_ind and ttest_rel from scipy.stats. Then create two variables: independent_test to store the result of ttest_ind(group_a, group_b) and paired_test to store the result of ttest_rel(before_treatment, after_treatment).
SciPy
Hint

Use the exact import statement and function calls as shown.

4
Print the t-test results
Print the t-statistic and p-value from independent_test and paired_test using these exact print statements:
print(f"Independent t-test: t = {independent_test.statistic:.3f}, p = {independent_test.pvalue:.3f}")
print(f"Paired t-test: t = {paired_test.statistic:.3f}, p = {paired_test.pvalue:.3f}")
SciPy
Hint

Use f-strings to format the output with 3 decimal places as shown.

Practice

(1/5)
1.

What is the main difference between ttest_ind and ttest_rel in SciPy?

easy
A. ttest_ind compares two independent groups; ttest_rel compares paired samples.
B. ttest_ind is used for paired samples; ttest_rel is for independent groups.
C. ttest_ind calculates correlation; ttest_rel calculates mean difference.
D. ttest_ind is for one sample; ttest_rel is for two samples.

Solution

  1. Step 1: Understand the purpose of ttest_ind

    ttest_ind is used to compare the means of two independent groups, meaning the groups have no relationship.
  2. Step 2: Understand the purpose of ttest_rel

    ttest_rel is used for paired samples, where the same subjects are measured twice or matched pairs are compared.
  3. Final Answer:

    ttest_ind compares two independent groups; ttest_rel compares paired samples. -> Option A
  4. Quick Check:

    Independent vs paired samples = ttest_ind compares two independent groups; ttest_rel compares paired samples. [OK]
Hint: Independent means separate groups; paired means related samples [OK]
Common Mistakes:
  • Confusing which test is for paired samples
  • Thinking both tests are interchangeable
  • Assuming ttest_ind is for paired data
2.

Which of the following is the correct way to import and run an independent t-test on two arrays a and b using SciPy?

import scipy.stats as stats

# Your code here
easy
A. stats.ttest_ind(a, b)
B. stats.ttest_rel(a, b)
C. stats.ttest_ind(a)
D. stats.ttest_rel(a)

Solution

  1. Step 1: Identify the correct function for independent samples

    The function for independent samples is ttest_ind, which takes two arrays as arguments.
  2. Step 2: Check the function call syntax

    The correct syntax is stats.ttest_ind(a, b). Using only one array or the paired test ttest_rel is incorrect here.
  3. Final Answer:

    stats.ttest_ind(a, b) -> Option A
  4. Quick Check:

    Independent test with two arrays = stats.ttest_ind(a, b) [OK]
Hint: Use ttest_ind for two arrays, both as arguments [OK]
Common Mistakes:
  • Using ttest_rel for independent samples
  • Passing only one array instead of two
  • Forgetting to import scipy.stats correctly
3.

What is the output of this code snippet?

from scipy.stats import ttest_rel

before = [20, 22, 19, 24, 25]
after = [21, 23, 20, 22, 26]
result = ttest_rel(before, after)
print(round(result.pvalue, 3))
medium
A. 0.900
B. 0.200
C. 0.531
D. 0.041

Solution

  1. Step 1: Understand the paired t-test calculation

    The ttest_rel compares paired samples 'before' and 'after'. The differences are small.
  2. Step 2: Calculate or trust the p-value output

    Running the code gives a p-value approximately 0.531.
  3. Final Answer:

    0.531 -> Option C
  4. Quick Check:

    Paired t-test p-value ~0.531 = 0.531 [OK]
Hint: Paired test with small differences yields high p-value [OK]
Common Mistakes:
  • Confusing p-value with t-statistic
  • Using ttest_ind instead of ttest_rel
  • Rounding errors in p-value
4.

Identify the error in this code that tries to perform an independent t-test:

from scipy.stats import ttest_ind

a = [5, 7, 8]
b = [6, 9]
result = ttest_ind(a)
print(result.pvalue)
medium
A. Lists a and b have different lengths.
B. Using ttest_ind instead of ttest_rel.
C. Incorrect import statement for ttest_ind.
D. Missing second argument for ttest_ind function.

Solution

  1. Step 1: Check function call parameters

    ttest_ind requires two arrays as arguments. Here only one array a is passed.
  2. Step 2: Verify other options

    Using ttest_ind is correct for independent samples. Different lengths are allowed. Import statement is correct.
  3. Final Answer:

    Missing second argument for ttest_ind function. -> Option D
  4. Quick Check:

    Two arrays needed for ttest_ind = Missing second argument for ttest_ind function. [OK]
Hint: ttest_ind needs two arrays as input [OK]
Common Mistakes:
  • Passing only one array to ttest_ind
  • Thinking arrays must be same length for independent test
  • Confusing ttest_ind and ttest_rel usage
5.

You have test scores of 10 students before and after a training program. You want to check if the training improved scores significantly. Which test should you use and why?

hard
A. Use ttest_ind because the groups are independent.
B. Use ttest_rel because the scores are paired measurements.
C. Use ttest_ind because it handles paired data better.
D. Use neither; a correlation test is needed.

Solution

  1. Step 1: Identify the data type

    Scores before and after training are from the same students, so data are paired.
  2. Step 2: Choose the correct t-test

    ttest_rel is designed for paired samples to compare means before and after treatment.
  3. Final Answer:

    Use ttest_rel because the scores are paired measurements. -> Option B
  4. Quick Check:

    Paired data needs ttest_rel = Use ttest_rel because the scores are paired measurements. [OK]
Hint: Paired before-after data use ttest_rel [OK]
Common Mistakes:
  • Using ttest_ind for paired data
  • Confusing correlation with mean comparison
  • Ignoring the pairing in data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard