Search code examples
pythonnumpymathstatisticsstandard-deviation

Find standard deviation and coefficient of variation for a distribution using numpy.std()


Calculate the S.D. and coefficient of variation (C.V.) for the following table : Class : 0-10, 10-20, 20-30, 30-40, 40-50, 50-60, 60-70, 70-80 Frequency : 5, 10, 20, 40, 30, 20, 10, 5​

I know how to solve this problem using mathematical steps as mentioned here https://brainly.in/question/36413297. But how can I solve it using numpy.std(). From the official documentation https://numpy.org/doc/stable/reference/generated/numpy.std.html I can see that numpy.std() expects a list of values as input. But in my case, these are class ranges and corresponding frequencies. So how can I apply numpy.std() in this kind of non-list input problem?


Solution

  • Well if you are required to use only the .std() method and no other method, and if your data consist of whole numbers, I suggest you populate your midPoint class list and use the .std() on a single list.

    Here is what I mean:

    import numpy as np
    def formatL1n2(L1,L2):
      L3 = []
      for i in range(len(L1)):
        L3.append([L1[i]]*L2[i])
      L4 = [] # this is to format L3
      for i in L3:
        for j in i:
            L4.append(j)
      return L3
    L1 = np.array([5,15,25,35,45,55,65,75])
    L2 = np.array([5,10,20,40,30,20,10,5])
    L3 = formatL1n2(L1,L2)
    st = np.std(L3)
    print(L3)
    

    Output:

    15.91992077715905