SciPy scipy.stats.sem Function

Bhuwan Bhatt Jan 30, 2023
  1. Syntax of scipy.stats.sem():
  2. Example Codes : scipy.stats.sem() Method to Find Mean With no axis set
  3. Example Codes: Set axis=None in scipy.stats.sem() Function
  4. Example Codes: Set axis=1 in scipy.stats.sem() Function
  5. Example Codes: Set ddof=0 in scipy.stats.sem() Function
SciPy scipy.stats.sem Function

Python Scipy scipy.stats.sem() function calculates the standard error of the mean of the provided data. The Standard error of mean shows us how far the sampled mean is from the actual population mean.

Syntax of scipy.stats.sem():

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

Parameters

a An array having elements whose standard error is to be calculated.
axis An optional parameter. The axis along which the standard error of the mean is to be calculated. By default, axis=0(horizontal axis)
ddof An optional parameter. It stands for the degree of freedom which is equal to the sample size minus the number of parameters that need to be calculated during the operation. Its default value is 1.

Return

It returns the standard error of the mean. The value may be an array of elements or a single float number based on the parameters and degree of freedom used.

Example Codes : scipy.stats.sem() Method to Find Mean With no axis set

import numpy as np
import scipy
from scipy import stats

arr = np.array([[2, 5, 6, 8], [3, 7, 3, 0], [1, 1, 4, 4], [9, 5, 0, 5], [6, 4, 2, 2]])


result = scipy.stats.sem(arr)
print("The standard error of given data is:\n", result)

Output:

The standard error of given data is:
 [1.46287388 0.9797959  1.         1.356466  ]

Here, an array arr is created having multi-dimensional data. The array is passed as an argument into the stats.sem function, which produces the output, and is stored in variable result.

Since no axis parameter is defined in this condition, the mean operation takes place in the horizontal axis as default.
Each of the five elements of the column is treated at once. At first, their standard deviation is calculated, and then the standard error formula is used. For example, the first element in the output array value 1.46287388 is the outcome from the first column element 2, 3, 1, 9, 6.

Example Codes: Set axis=None in scipy.stats.sem() Function

import numpy as np
import scipy
from scipy import stats

arr = np.array([[2, 5, 6, 8], [3, 7, 3, 0], [1, 1, 4, 4], [9, 5, 0, 5], [6, 4, 2, 2]])

result = scipy.stats.sem(arr, axis=None)
print("The standard error of given data is:\n", result)

Output:

The standard error of given data is:
 0.5725060330640515

Here, the output shows a single float value because whenever the axis is set to None, the operation takes place in the entire array element rather than a particular row or column field.

Example Codes: Set axis=1 in scipy.stats.sem() Function

import numpy as np
import scipy
from scipy import stats

arr = np.array([[2, 5, 6, 8], [3, 7, 3, 0], [1, 1, 4, 4], [9, 5, 0, 5], [6, 4, 2, 2]])


result = scipy.stats.sem(arr, axis=1)
print("The standard error of given data is:\n", result)

Output:

The standard error of given data is:
 [1.25       1.43614066 0.8660254  1.8427787  0.95742711]

Here, the axis is set to 1, meaning the operation will take on the vertical axis, thus producing five elements in the outputted array field.

Example Codes: Set ddof=0 in scipy.stats.sem() Function

import numpy as np
import scipy
from scipy import stats

arr = np.array([[2, 5, 6, 8], [3, 7, 3, 0], [1, 1, 4, 4], [9, 5, 0, 5], [6, 4, 2, 2]])

result = scipy.stats.sem(arr, axis=1, ddof=0)
print("The standard error of given data is:\n", result)

Output:

The standard error of given data is:
 [1.08253175 1.2437343  0.75       1.59589317 0.8291562 ]

Here, the axis operation is similar to the previous example, but ddof=0 brings out some changes. We know that before calculating standard error, we need to have the value of the standard deviations. To calculate standard deviation, the mean of the squared differences are evaluated using number of sample size minus ddof. Thus by default, ddof’s value is 1, but whenever kept 0, the standard errors value decreases.

Whenever sample data grows larger, the standard error of mean decreases in reference to standard deviation. Meaning, with larger sample data, the sample mean calculates the actual population mean more accurately and precisely.

Related Article - SciPy Stats