Home Â» How to Conduct a One Sample T-Test in Python

# How to Conduct a One Sample T-Test in Python

A one sample t-test is used to determine whether or not the mean of a population is equal to some value.

This tutorial explains how to conduct a one sample t-test in Python.

### Example: One Sample t-Test in Python

Suppose a botanist wants to know if the mean height of a certain species of plant is equal to 15 inches. She collects a random sample of 12 plants and records each of their heights in inches.

Use the following steps to conduct a one sample t-test to determine if the mean height for this species of plant is actually equal to 15 inches.

Step 1: Create the data.

First, weâ€™ll create an array to hold the measurements of the 12 plants:

`data = [14, 14, 16, 13, 12, 17, 15, 14, 15, 13, 15, 14]`

Step 2: Conduct a one sample t-test.

Next, weâ€™ll use the ttest_1samp() function from the scipy.stats library to conduct a one sample t-test, which uses the following syntax:

ttest_1samp(a, popmean)

where:

• a:Â an array of sample observations
• popmean:Â the expected population mean

Hereâ€™s how to use this function in our specific example:

```import scipy.stats as stats

#perform one sample t-test
stats.ttest_1samp(a=data, popmean=15)

(statistic=-1.6848, pvalue=0.1201)
```

The t test statistic isÂ -1.6848Â and the corresponding two-sided p-value isÂ 0.1201.

Step 3: Interpret the results.

The two hypotheses for this particular one sample t-test are as follows:

H0:Â Âµ = 15 (the mean height for this species of plant is 15 inches)

HA:Â ÂµÂ â‰ 15 (the mean height isÂ notÂ 15 inches)

Because the p-value of our testÂ (0.1201)Â is greater than alpha = 0.05, we fail to reject the null hypothesis of the test. We do not have sufficient evidence to say that the mean height for this particular species of plant is different from 15 inches.