Python Standard Deviation

Last Updated On Sunday 27th Mar 2022

Python Standard Deviation

• Standard deviation in statistics measures the spreaness of data values with respect to mean and mathematically, is calculated as square root of variance.
• Basically It is the Root Over of Variance.
• Standard Deviation is a measure of spread in Statistics. It is used to quantify the measure of spread, variation of a set of data values. It is very much similar to variance, gives the measure of deviation whereas variance provides the squared value.

Standard Deviation Python

	# importing Statistics module
import statistics

# creating a simple data - set
sample = [1, 2, 3, 4, 5]

# Prints standard deviation
# xbar is set to default value of 1
print("Standard Deviation of sample is % s "% (statistics.stdev(sample)))


• Standard Deviation is highly essential in the field of statistical maths and statistical study.
• It is very useful in the field of financial studies as well as it helps to determine the margin of profit and loss.

Numpy standard deviation function

Numpy standard deviation function is useful in finding the spread of a distribution of array values.

	numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=some_value)



Python Calculate Standard Deviation

• a:array-like – Input array or object that can be converted to an array, values of this array will be used for finding the median.
• axis: int or sequence of int or None (optional) – Axis or axes along which the medians are computed. The default is to compute the median along a flattened version of the array.
• out: ndarray (optional) – Alternative output array in which to place the result. It must have the same shape as the expected output.
• ddof:int (optional) – This means delta degrees of freedom. The divisor used in calculations is N – ddof, where N represents the number of elements. By default ddof is zero.
• keepdims – bool (optional) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.
	import numpy as np

x = [22,23,25,27,28,35,32,28,30,40,24,26,27,29,31]

a = np.sqrt(np.sum((x - np.mean(x))**2)/len(x))

b = np.sqrt(np.sum((x - np.mean(x))**2)/len(x)-1)

print("")
print("X",format(a,'.2f'))
print("")
print("X-1", format(b,'.2f'))


	# X  4.56
# X-1 4.45


	a = np.std([22,23,25,27,28,35,32,28,30,40,24,26,27,29,31])
print('Population std deviation is:', format(a,'.2f'))

b = np.std([[22,23,25,27,28,35,32,28,30,40,24,26,27,29,31]], ddof=1)
print('Sample std deviation is:',format(b,'.2f'))


	Population std deviation is: 4.56
Sample std deviation is: 4.72