numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=)
Return standard deviation of elements across given axis.
a | array, elements to get the std value |
axis | Int (optional ), or tuple, default is None, stdvalue among all the elements. If axis given then values across the axis is returned. |
out | Optional. If given then output to be stored. Must be of same shape as of the output |
keepdims | Bool ( Optional ), output matches to the input array dimension. |
ddof | Delta Degrees of Freedom ( default is 0 ) , N - ddof is used where N is the number of elements in computing the standard deviation |
import numpy as np
# my_data=np.random.randint(2,high=7,size=(3,3),dtype='int16')
my_data=np.array([[6, 3, 2], [7, 2, 2], [6, 2, 9]])
print(my_data)
Output
[[6 3 2]
[7 2 2]
[6 2 9]]
print("std() : ", my_data.std())
print("std(axis=0) : ", my_data.std(axis=0))
print("std(axis=1) : ", my_data.std(axis=1))
Output
std() : 2.5385910352879693
std(axis=0) : [0.47140452 0.47140452 3.29983165]
std(axis=1) : [1.69967317 2.3570226 2.86744176]
print("std(keepdims=True) : ", my_data.std(keepdims=True))
print("std(keepdims=False) : ", my_data.std(keepdims=False))
Output
std(keepdims=True) : [[2.53859104]]
std(keepdims=False) : 2.5385910352879693
ddof = 0 ( default) , this is Population Standard Deviationddof = 1 , this is Sample Standard Deviationimport numpy as np
list1=[12,13,15,11,9,12,13,10,11,12,13,7,8]
my_data=np.array(list1)
print(my_data.std(ddof=0)) # 2.153846153846154
print(my_data.std(ddof=1)) # 2.2417941532712202
Comparison of Standard Deviation using Python, Pandas, Numpy and Statistics library
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.