0

Hell i have nonsense values of variance and standart deviation in numpy.

this documentation no make sense with this sample. https://numpy.org/doc/stable/reference/generated/numpy.std.html

enter image description here

youtube example calculation mean:9, var:9

>>> import numpy as np
>>> a=np.array([6,9,14,10,5,8,1])
>>> np.mean(a)
7.571428571428571
>>> np.var(a)
14.530612244897958
>>> np.std(a)
3.8119040183218096
>>> np.nanvar(a)
14.530612244897958

edit for youtubeissues:

a=np.array([5,6,8,9,10,11,14]) 
>>> np.var(a) 8.0 

edit for a smaller sample for apply the variance equation a=2,3,4,5 mean=3.5

var=((2-3.5)**2+(2-3.5)**2+(3-3.5)**2+(4-3.5)**2+(5-3.5)**2) / 3 = 2.4166666666666665

>>> a=np.array([2,3,4,5])
>>> np.var(a)
1.25
>>> np.mean(a)
3.5
>>> np.std(a)
1.118033988749895

How i fix this value of variance? what is the problem with numpy?

  • Because they're using the "Data" column from the table on the left with data points {5, 6, 8, 9, 10, 11, 14}. – Eric Perkerson Mar 11 '21 at 03:40
  • a=np.array([5,6,8,9,10,11,14]) np.var(a)

    8.0

    i can make smaller: 2,3,4,5.mean=3.5.std=((2-3.5)2+(2-3.5)2+(3-3.5)2+(4-3.5)2+(5-3.5)**2) / 3 = 2.4166666666666665

    a=np.array([2,3,4,5]) np.var(a)

    1.25. you see it?

    – rubengavidia0x Mar 11 '21 at 04:17
  • 1
    Numpy uses the biased formula for standard deviation and variance by default instead of the unbiased one using Bessel's correction: https://stackoverflow.com/questions/27600207/why-does-numpy-std-give-a-different-result-to-matlab-std – Eric Perkerson Mar 11 '21 at 06:07
  • 1
    See this as well: https://en.wikipedia.org/wiki/Bessel%27s_correction – Eric Perkerson Mar 11 '21 at 06:08
  • What is the question? – whuber Mar 11 '21 at 13:36
  • @whuber is not variance different deffinitions take a look this:

    ((2-3.5)2+(2-3.5)2+(3-3.5)2+(4-3.5)2+(5-3.5)**2)/4

    =1.8125 can you open again the question? for i answer my own queston and giving all the credit to Eric Perkerson for the help!.

    – rubengavidia0x Mar 11 '21 at 18:46
  • @EricPerkerson

    import numpy as np a=np.array([2,3,4,5]) np.std(a, ddof=3)

    2.23606797749979 still nothing oh my god why

    – rubengavidia0x Mar 11 '21 at 19:05
  • a = np.array([5, 6, 8, 9, 10, 11, 14]); >>> np.var(a, ddof=1) = 9.3333333333333339, as in their example. Use 1 degree of freedom for the corrected formula, not 3.

    – Eric Perkerson Mar 11 '21 at 19:39

0 Answers0