Math and Science with Python

T-Test in Python with scipy.stats

Check whether two group averages really differ with scipy.stats.ttest_ind, and read the p-value correctly.

  • Intermédiaire
  • 2 min de lecture
  • 1 exemple exécutable
Ce que vous allez apprendre
  • Independent samples with ttest_ind
  • Welch's test with equal_var=False
  • One-sample tests and the p-value
class A vs class B
Means
  • 82.0
  • 74.5
Welch t-test
  • t = 4.728
  • p = 0.00020

Is the difference bigger than chance?

Découvrez T-Test in Python with SciPy avec un exemple pratique modifiable et exécutable en ligne.

Points clés

  1. ttest_ind is for two independent groups; equal_var=False runs Welch's test.
  2. Here t = 4.728 and p = 0.00020, below 0.05, so the difference is significant.
  3. ttest_1samp(class_b, popmean=80) compares one group with a fixed value.
  4. A small p-value does not say the difference is large or important, only that it is unlikely to be chance.

Quand l’utiliser ?

  • Solve equations, integrals and differential equations in a few lines.
  • Fit models to measured data and analyse signals in science and engineering classes.
  • Check homework answers numerically and see where a hand calculation went wrong.

Code d’exemple

main.py
from scipy import stats

# Test scores of two classes taught with different methods
class_a = [78, 82, 85, 88, 75, 80, 84, 79, 86, 83]
class_b = [72, 75, 78, 70, 74, 77, 73, 76, 71, 79]

# Independent two-sample t-test (Welch's version: equal_var=False)
result = stats.ttest_ind(class_a, class_b, equal_var=False)
print(f"Mean A = {sum(class_a) / len(class_a):.1f}, mean B = {sum(class_b) / len(class_b):.1f}")
print(f"t = {result.statistic:.3f}, p = {result.pvalue:.5f}")

alpha = 0.05
if result.pvalue < alpha:
    print("p < 0.05: the difference is statistically significant")
else:
    print("p >= 0.05: no significant difference")

# One-sample t-test: is class B's mean different from 80?
one = stats.ttest_1samp(class_b, popmean=80)
print(f"Class B vs 80: t = {one.statistic:.3f}, p = {one.pvalue:.6f}")
Obtenir un bouton Run pour cet exemple

Résultat de l’exécution

Mean A = 82.0, mean B = 74.5
t = 4.728, p = 0.00020
p < 0.05: the difference is statistically significant
Class B vs 80: t = -5.745, p = 0.000278

Explication du code

Cet exemple montre T-Test in Python with SciPy en pratique. Modifiez les valeurs d’entrée et relancez le code pour voir ce qui change.

Modifiez les valeurs et exécutez le programme avec le compilateur Python en ligne CodeUtility, sans installation locale.

Exercices d’approfondissement

Essayez de modifier le code en suivant les pistes ci-dessous. Changer le code et observer le résultat vous fera bien mieux comprendre la leçon que la simple lecture.

  1. Change the function or the data and predict the result before running.
  2. Compare the numerical answer with the exact one from SymPy or a hand calculation.
  3. Plot the result with Matplotlib to see what the numbers mean.
Exécuter dans le compilateur Python →

Leçon terminée ?

Marquez-la comme apprise pour suivre votre progression.

S'entraîner avec un défi

Utilisez le contenu de cette leçon pour résoudre un vrai problème. Chaque défi s'exécute dans le navigateur, avec des tests instantanés et une revue par l'IA.

Tous les défis

Outils pour cette leçon