# Compute the standard error of the mean of list elements in Python

The standard error(SE) measures the spread of the data. The bigger the SE, the more spread the data have. The standard error (SE) of a statistic is the standard deviation of its sampling distribution or an estimate of that standard deviation. If the statistic is the sample mean, it is called the standard error of the mean (SEM).

#### The formula for the standard error of the mean:

If a statistically independent sample of n observations x1, x2, …, xn is taken from a statistical population with a sample standard deviation of σ, the standard error of the mean of the sample is given by $SEM = \frac{\sigma}{ \sqrt{n}}$

To compute SEM, you can use either scipy or numpy Python package.

#### Using scipy:

The scipy.stats module has a function sem() that calculates the standard error of the mean (or standard error of measurement) of the values in the input array. The format of the function is as follows:

scipy.stats.sem(a, axis=0, ddof=1, nan_policy='propagate')

In the following code, I am randomly generating 10 numbers to compute SEM. To use the following code, you can replace list ‘a’ with your list of values.

from scipy import stats as st
a=np.random.random(10)
se = st.sem(a)

#### Using numpy:

If you are not interested in the sem() function of scipy, you can compute the sample standard deviation for the values in the list and divide the sample standard deviation by the square root of the number of values in the list. The default ddof value for the numpy std() function is zero, which computes population standard deviation. You need to use ddof=1 to compute the sample standard deviation. The format of the numpy std() function is as follows:

numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>)

In the following code, I am using the same list ‘a’ and have set ddof to 1.

se = np.std(a, ddof=1)/np.sqrt(len(a))

Output

List ‘a’ had the following values:

array([0.23840975, 0.68159684, 0.747814 , 0.27296423, 0.07580225,
0.8901718 , 0.83292882, 0.03398631, 0.29072783, 0.22097503])

The value of SEM was 0.10247672761861855

Please let me know if you have any queries related to the above Python codes.

References:

This site uses Akismet to reduce spam. Learn how your comment data is processed.