How to calculate standard error of mean in Python?

By Shivang Yadav Last updated : November 22, 2023

Standard Error of Mean is the measure of values by which the sample mean of values deviates from the true population mean.

The mathematical formula of the standard error of the mean is: SEM = s/√n

Where,

  • s is the sample standard deviation
  • n is the sample size.

Method 1: Using the direct formula

To calculate the standard Error of Mean, use the direct formula: SEM = s/√n

Example: Python program to calculate standard Error of Mean using direct formula

import numpy as np

# define data
data = [2, 5, 7, 1, 7, 4, 8, 11, 6, 8, 3, 10]
print("The data in the dataset is", data)

sampleVariance = np.std(data, ddof=1) / np.sqrt(np.size(data))
print("Standard error of mean : ", sampleVariance)

The output is:

The data in the dataset is [2, 5, 7, 1, 7, 4, 8, 11, 6, 8, 3, 10]
Standard error of mean :  0.896119580764924

Method 2: Using SciPy

Another method is to calculate the standard error of the mean, use the sem() method of the scipy.stats library. It accepts array-like data and calculates the standard error of the mean.

Example: Python program to calculate standard Error of Mean using SciPy

# Import the library to use sem() method
from scipy.stats import sem

data = [2, 5, 7, 1, 7, 4, 8, 11, 6, 8, 3, 10]
print("The data in the dataset is", data)

sampleVariance = sem(data)
print("Standard error of mean : ", sampleVariance)

The output is:

The data in the dataset is [2, 5, 7, 1, 7, 4, 8, 11, 6, 8, 3, 10]
Standard error of mean :  0.896119580764924

Python SciPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.