Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Performing a t-test in Python | Statistical Testing
Learning Statistics with Python
course content

Kursusindhold

Learning Statistics with Python

Learning Statistics with Python

1. Basic Concepts
2. Mean, Median and Mode with Python
3. Variance and Standard Deviation
4. Covariance vs Correlation
5. Confidence Interval
6. Statistical Testing

book
Performing a t-test in Python

To conduct a t-test in Python, all you have to do is specify the alternative hypothesis and indicate whether variances are roughly equal (homogeneous).

The ttest_ind() function within scipy.stats handles the rest. Below is the syntax:

python

Parameters:

  • a — the first sample;

  • b — the second sample;

  • equal_var — set to True if variances are approximately equal, and False if they are not;

  • alternative — the type of alternative hypothesis:

    • 'two-sided' — indicates that the means are not equal;

    • 'less' — implies that the first mean is less than the second;

    • 'greater' — implies that the first mean is greater than the second.

Return values:

  • statistic — the value of the t statistic;

  • pvalue — the p-value.

The focus is on the p-value. If the p-value is lower than α (usually 0.05), the t statistic falls within the critical region, leading to the acceptance of the alternative hypothesis. If the p-value is greater than α, the null hypothesis is accepted, indicating that the means are equal.

Here is an example of applying the t-test to the heights dataset:

123456789101112131415
import pandas as pd import scipy.stats as st # Load the data male = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a849660e-ddfa-4033-80a6-94a1b7772e23/Testing2.0/male.csv').squeeze() female = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a849660e-ddfa-4033-80a6-94a1b7772e23/Testing2.0/female.csv').squeeze() # Apply t-test t_stat, pvalue = st.ttest_ind(male, female, equal_var=True, alternative="greater") if pvalue > 0.05: # Check if we should support or not the null hypothesis if pvalue > 0.05: print("We support the null hypothesis, the mean values are equal") else: print("We reject the null hypothesis, males are taller")
copy
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 6. Kapitel 6

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

course content

Kursusindhold

Learning Statistics with Python

Learning Statistics with Python

1. Basic Concepts
2. Mean, Median and Mode with Python
3. Variance and Standard Deviation
4. Covariance vs Correlation
5. Confidence Interval
6. Statistical Testing

book
Performing a t-test in Python

To conduct a t-test in Python, all you have to do is specify the alternative hypothesis and indicate whether variances are roughly equal (homogeneous).

The ttest_ind() function within scipy.stats handles the rest. Below is the syntax:

python

Parameters:

  • a — the first sample;

  • b — the second sample;

  • equal_var — set to True if variances are approximately equal, and False if they are not;

  • alternative — the type of alternative hypothesis:

    • 'two-sided' — indicates that the means are not equal;

    • 'less' — implies that the first mean is less than the second;

    • 'greater' — implies that the first mean is greater than the second.

Return values:

  • statistic — the value of the t statistic;

  • pvalue — the p-value.

The focus is on the p-value. If the p-value is lower than α (usually 0.05), the t statistic falls within the critical region, leading to the acceptance of the alternative hypothesis. If the p-value is greater than α, the null hypothesis is accepted, indicating that the means are equal.

Here is an example of applying the t-test to the heights dataset:

123456789101112131415
import pandas as pd import scipy.stats as st # Load the data male = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a849660e-ddfa-4033-80a6-94a1b7772e23/Testing2.0/male.csv').squeeze() female = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/a849660e-ddfa-4033-80a6-94a1b7772e23/Testing2.0/female.csv').squeeze() # Apply t-test t_stat, pvalue = st.ttest_ind(male, female, equal_var=True, alternative="greater") if pvalue > 0.05: # Check if we should support or not the null hypothesis if pvalue > 0.05: print("We support the null hypothesis, the mean values are equal") else: print("We reject the null hypothesis, males are taller")
copy
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 6. Kapitel 6
Vi beklager, at noget gik galt. Hvad skete der?
some-alt