Python:定义数字列表的方差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16670658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:19:39  来源:igfitidea点击:

Python: Variance of a list of defined numbers

pythonlistnumbersvariancedefined

提问by GiamPy

I am trying to make a function that prints the variance of a list of defined numbers:

我正在尝试制作一个打印定义数字列表方差的函数:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

So far, I have tried proceeding on making these three functions:

到目前为止,我已经尝试继续制作这三个功能:

def grades_sum(my_list):
    total = 0
    for grade in my_list: 
        total += grade
    return total

def grades_average(my_list):
    sum_of_grades = grades_sum(my_list)
    average = sum_of_grades / len(my_list)
    return average

def grades_variance(my_list, average):
    variance = 0
    for i in my_list:
        variance += (average - my_list[i]) ** 2
    return variance / len(my_list)

When I try to execute the code, however, it gives me the following error at the following line:

但是,当我尝试执行代码时,它在以下行出现以下错误:

Line: variance += (average - my_list[i]) ** 2
Error: list index out of range

Apologies if my current Python knowledges are limited, but I am still learning - so please if you wish to help solving this issue try not to suggest extremely-complicated ways on how to solve this, thank you really much.

抱歉,如果我目前的 Python 知识有限,但我仍在学习 - 所以如果你想帮助解决这个问题,请尽量不要提出关于如何解决这个问题的极其复杂的方法,非常感谢你。

采纳答案by Magsol

First I would suggest using Python's built-in summethod to replace your first custom method. grades_averagethen becomes:

首先,我建议使用 Python 的内置sum方法来替换您的第一个自定义方法。grades_average然后变成:

def grades_average(my_list):
    sum_of_grades = sum(my_list)
    average = sum_of_grades / len(my_list)
    return average

Second, I would strongly recommend looking into the NumPy library, as it has these methods built-in. numpy.mean()and numpy.std()would cover both these cases.

其次,我强烈建议查看NumPy 库,因为它内置了这些方法。numpy.mean()并且numpy.std()将涵盖这两种情况。

If you're interested in writing the code for yourself first, that's totally fine too. As for your specific error, I believe @gnibbler above nailed it. If you want to loop using an index, you can restructure the line in grades_varianceto be:

如果您有兴趣先为自己编写代码,那也完全没问题。至于你的具体错误,我相信上面的@gnibbler 已经解决了。如果要使用索引进行循环,可以将 in 行重组grades_variance为:

for i in range(0, len(my_list)):

As Lattywarenoted, looping by index is not particularly "Pythonic"; the way you're currently doing it is generally superior. This is just for your reference.

正如Lattyware 所指出的,按索引循环并不是特别“Pythonic”;你目前的做法通常更胜一筹。这仅供您参考。

回答by John La Rooy

When you say

当你说

 for i in my_list:

iisn't the indexof the item. iisthe item

i不是项目的索引i项目

for i in my_list:
    variance += (average - i) ** 2

回答by Gareth Latty

While gnibbler has solved the problem with your code, you can achieve this much more easily using built-in functionsand a generator expression:

虽然gnibbler 已经用您的代码解决了这个问题,但您可以使用内置函数生成器表达式更轻松地实现这一点:

average = sum(grades) / len(grades)
varience = sum((average - value) ** 2 for value in grades) / len(grades)

It might look a little scary at first, but if you watch the video I link about list comprehensions and generator expressions - they are actually really simple and useful.

乍一看可能有点吓人,但是如果您观看我链接的有关列表推导式和生成器表达式的视频 - 它们实际上非常简单且有用。

回答by robinfang

Try numpy.

尝试numpy

import numpy as np
variance = np.var(grades)

回答by zengr

python 3.4 has a statistics lib which does this.

python 3.4 有一个统计库可以做到这一点。

   import statistics
   grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
   statistics.pvariance(grades)
=> 334.07100591715977

https://docs.python.org/3/library/statistics.html#statistics.pvariance

https://docs.python.org/3/library/statistics.html#statistics.pvariance

回答by Bharatwaja

the below code is used to get the average of values

下面的代码用于获取值的平均值

def grades_average(my_list):
    sum_of_grades = sum(my_list)
    average = sum(my_list) / len(my_list)
    return average

variance formula -> The average of the squared differences from the Mean. This code below is used to get the variance of values

方差公式 -> 均值的平方差的平均值。下面的代码用于获取值的方差

def grades_variance(my_list, average):
    variance = 0
    for i in my_list:
         variance += (average - i) ** 2
    return variance / len(my_list)

回答by Don Juan

I suppose you would like the sample variance i.e. the unbiased estimator of the variance. I think this function might do the job. It will print the variance and the mean of a vector n.

我想你想要样本方差,即方差的无偏估计量。我认为这个功能可以完成这项工作。它将打印向量 n 的方差和均值。

n = [5, 3, 1, 2, 4]

def variance1337(n):
    var1 = []
    mean1 = sum(n)/len(n)
    for xs in n:
        var1.append((xs - mean1) ** 2)
    print(sum(var1)/(len(n) - 1))
    print(mean1)

回答by TRINADH NAGUBADI

The below code is used to get the variance I create a custom function

下面的代码用于获取方差我创建了一个自定义函数

   def variance(val):
       total_sum=sum(val)
       average=total_sum/len(val)
       a=[]
       for i in val:
           a.append((i-average)**2)
       return sum(a)/len(a)

   val=[2.18,2.22,2.24,1.62,1.32,1.85,1.85,2.70,3.60,4.60,1.38,2.34,2.71]
   variance(val)