0
0
Data Analysis Pythondata~10 mins

t-test with scipy.stats in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - t-test with scipy.stats
Start: Two data samples
Choose t-test type
Calculate t-statistic and p-value
Compare p-value to threshold
Reject null
End
We start with two data samples, choose the t-test type, calculate the t-statistic and p-value, then decide if the difference is significant by comparing p-value to a threshold.
Execution Sample
Data Analysis Python
from scipy import stats
sample1 = [5, 6, 7, 8, 9]
sample2 = [5, 5, 5, 5, 6]
t_stat, p_val = stats.ttest_ind(sample1, sample2)
print(t_stat, p_val)
This code runs an independent t-test on two small samples and prints the t-statistic and p-value.
Execution Table
StepActionInputOutputExplanation
1Import scipy.statsNonescipy.stats module readyPrepare to use t-test functions
2Define sample1[5,6,7,8,9]sample1 list createdFirst data sample stored
3Define sample2[5,5,5,5,6]sample2 list createdSecond data sample stored
4Call ttest_ind(sample1, sample2)sample1, sample2(t_stat=2.449, p_val=0.041)Calculate t-statistic and p-value for independent samples
5Print results(2.449, 0.041)2.449 0.041Show t-statistic and p-value
6Compare p_val < 0.050.041 < 0.05Truep-value is less than 0.05, reject null hypothesis
7EndDecision madeReject null hypothesisSamples differ significantly
💡 Execution stops after printing results and deciding significance based on p-value.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
sample1None[5,6,7,8,9][5,6,7,8,9][5,6,7,8,9][5,6,7,8,9]
sample2NoneNone[5,5,5,5,6][5,5,5,5,6][5,5,5,5,6]
t_statNoneNoneNone2.4492.449
p_valNoneNoneNone0.0410.041
Key Moments - 3 Insights
Why do we compare the p-value to 0.05?
The p-value tells us how likely the observed difference happened by chance. If it's less than 0.05 (5%), we say the difference is statistically significant and reject the null hypothesis. See execution_table step 6.
What does the t-statistic number mean?
The t-statistic measures how big the difference between samples is relative to their variation. A larger absolute t-statistic means a bigger difference. The exact number helps calculate the p-value. See execution_table step 4.
Can we use ttest_ind for samples that are related?
No, ttest_ind assumes samples are independent. For related samples, use ttest_rel instead. This example uses independent samples as shown in the code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what are the values of t_stat and p_val?
At_stat=5, p_val=6
Bt_stat=0.041, p_val=2.449
Ct_stat=2.449, p_val=0.041
Dt_stat=1, p_val=0.5
💡 Hint
Check the Output column in execution_table row for step 4.
At which step does the code decide to reject the null hypothesis?
AStep 6
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where p_val is compared to 0.05 in execution_table.
If the p-value was 0.1 instead of 0.038, what would change in the execution_table?
AStep 2 sample1 would change
BStep 6 would say 'False' and we would fail to reject null
CStep 4 t_stat would change
DNo change at all
💡 Hint
Look at the comparison and decision in step 6 of execution_table.
Concept Snapshot
t-test with scipy.stats:
- Use stats.ttest_ind(sample1, sample2) for independent samples
- Returns t-statistic and p-value
- If p-value < 0.05, reject null hypothesis (samples differ)
- Use ttest_rel for related samples
- Helps test if two groups differ significantly
Full Transcript
This visual execution shows how to perform a t-test using scipy.stats in Python. We start by importing the module, then define two data samples. We run the independent t-test function which returns a t-statistic and a p-value. The p-value tells us if the difference between samples is statistically significant. If the p-value is less than 0.05, we reject the null hypothesis, meaning the samples differ significantly. The variable tracker shows how sample data and results change step-by-step. Key moments clarify why we compare p-value to 0.05 and what the t-statistic means. The quiz tests understanding of the execution steps and decision points.