How can I calculate the variance of a list in python?

前端 未结 7 872
醉酒成梦
醉酒成梦 2020-12-29 04:58

If I have a list like this:

results=[-14.82381293, -0.29423447, -13.56067979, -1.6288903, -0.31632439,
          0.53459687, -1.34069996, -1.61042692, -4.032         


        
7条回答
  •  没有蜡笔的小新
    2020-12-29 05:12

    Without imports, I would use the following python3 script:

    #!/usr/bin/env python3
    
    def createData():
        data1=[12,54,60,3,15,6,36]
        data2=[1,2,3,4,5]
        data3=[100,30000,1567,3467,20000,23457,400,1,15]
    
        dataset=[]
        dataset.append(data1)
        dataset.append(data2)
        dataset.append(data3)
    
        return dataset
    
    def calculateMean(data):
        means=[]
        # one list of the nested list
        for oneDataset in data:
            sum=0
            mean=0
            # one datapoint in one inner list
            for number in oneDataset:
                # summing up
                sum+=number
            # mean for one inner list
            mean=sum/len(oneDataset)
            # adding a tuples of the original data and their mean to
            # a list of tuples
            item=(oneDataset, mean)
            means.append(item)
    
        return means
    
    # to do: substract mean from each element and square the result
    # sum up the square results and divide by number of elements
    def calculateVariance(meanData):
        variances=[]
        # meanData is the list of tuples
        # pair is one tuple
        for pair in meanData:
            # pair[0] is the original data
            interResult=0
            squareSum=0
            for element in pair[0]:
                interResult=(element-pair[1])**2
                squareSum+=interResult
            variance=squareSum/len(pair[0])
            variances.append((pair[0], pair[1], variance))
    
        return variances
    
    
    
    
    
    def main():
        my_data=createData()
        my_means=calculateMean(my_data)
        my_variances=calculateVariance(my_means)
        print(my_variances)
    
    if __name__ == "__main__":
        main()
    

    here you get a print of the original data, their mean and the variance. I know this approach covers a list of several datasets, yet I think you can adapt it quickly for your purpose ;)

提交回复
热议问题