0
0
SciPydata~10 mins

t-test (ttest_ind, ttest_rel) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - t-test (ttest_ind, ttest_rel)
Start: Two Data Samples
Choose Test Type
Independent
Calculate t-statistic and p-value
Compare p-value to alpha
Reject H0
Conclude groups differ
Start with two data samples, choose independent or related t-test, calculate statistics, then decide if groups differ based on p-value.
Execution Sample
SciPy
from scipy.stats import ttest_ind, ttest_rel

# Sample data
sample1 = [5, 7, 8, 6, 9]
sample2 = [6, 9, 7, 10, 8]

# Independent t-test
result = ttest_ind(sample1, sample2)
print(result)
This code runs an independent t-test on two samples and prints the t-statistic and p-value.
Execution Table
StepActionCalculation/ConditionResult
1Input samplessample1 = [5,7,8,6,9], sample2 = [6,9,7,10,8]Samples ready
2Choose testIndependent samples testttest_ind selected
3Calculate meansmean1=7.0, mean2=8.0Means computed
4Calculate variancesvar1=2.5, var2=2.5Variances computed
5Calculate t-statistict = (7.0-8.0)/sqrt(2.5/5 + 2.5/5) = -1.0t = -1.0
6Calculate degrees of freedomdf = 8df = 8
7Calculate p-valuep ≈ 0.34p ≈ 0.34
8Compare p-value to alpha=0.050.34 > 0.05Fail to reject H0
9ConclusionNo evidence groups differEnd
💡 p-value is greater than 0.05, so we fail to reject the null hypothesis.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 7Final
sample1[5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9][5,7,8,6,9]
sample2[6,9,7,10,8][6,9,7,10,8][6,9,7,10,8][6,9,7,10,8][6,9,7,10,8][6,9,7,10,8]
mean1N/A7.07.07.07.07.0
mean2N/A8.08.08.08.08.0
var1N/AN/A2.52.52.52.5
var2N/AN/A2.52.52.52.5
t-statisticN/AN/AN/A-1.0-1.0-1.0
p-valueN/AN/AN/AN/A0.340.34
Key Moments - 3 Insights
Why do we choose between independent and related t-tests?
Because independent t-tests compare two separate groups, while related t-tests compare paired or matched samples. This choice affects how the test calculates differences, as shown in Step 2 of the execution_table.
What does the p-value tell us in the t-test?
The p-value shows the chance of seeing the data if the groups are actually the same. If p is less than 0.05 (Step 8), we say groups differ; otherwise, we say no evidence of difference.
Why do we calculate degrees of freedom (df)?
Degrees of freedom help determine the shape of the t-distribution used to find the p-value. It depends on sample sizes and is calculated in Step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 5, what is the approximate t-statistic value?
A-1.0
B0.707
C-1.5
D1.5
💡 Hint
Check the 'Result' column in Step 5 of the execution_table.
At which step does the test decide to fail to reject the null hypothesis?
AStep 7
BStep 8
CStep 6
DStep 9
💡 Hint
Look for the comparison of p-value to alpha in the execution_table.
If sample sizes increased, how would the degrees of freedom change in the execution_table?
ADegrees of freedom would decrease
BDegrees of freedom would stay the same
CDegrees of freedom would increase
DDegrees of freedom would become zero
💡 Hint
Degrees of freedom depend on sample sizes, see Step 6 in execution_table.
Concept Snapshot
t-test compares means of two samples.
Use ttest_ind for independent groups.
Use ttest_rel for paired samples.
Calculate t-statistic and p-value.
If p < 0.05, groups differ significantly.
Otherwise, no evidence of difference.
Full Transcript
A t-test compares two groups to see if their averages differ. We start with two data samples. We pick the test type: independent if groups are separate, related if paired. Then we calculate the t-statistic, which measures difference size relative to variation. We find the p-value, which tells us how likely the difference is by chance. If p-value is less than 0.05, we say the groups differ. Otherwise, we say there is no evidence they differ. This process helps us make decisions about data differences.