Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编写程序,生成5个随机数并找出标准偏差_Python - Fatal编程技术网

Python 编写程序,生成5个随机数并找出标准偏差

Python 编写程序,生成5个随机数并找出标准偏差,python,Python,我在编写代码生成5个随机整数和查找st.dev时遇到问题。5个随机整数部分已经完成,但st.dev部分不正确 import random import math ranList = [] for x in range(0, 5): n = random.randint(1, 10) ranList.append(n) print("Here is the list of 5 random numbers:") print(ranList) print(&

我在编写代码生成5个随机整数和查找st.dev时遇到问题。5个随机整数部分已经完成,但st.dev部分不正确

import random
import math

ranList = []

for x in range(0, 5):
    n = random.randint(1, 10)
    ranList.append(n)

print("Here is the list of 5 random numbers:")
print(ranList)
print("The standard deviation is: ")

def stDev(ranList):      
    ranList = ranList.copy() 
    xbar = sum(ranList)/len(ranList)
    for i in range(len(ranList)):
        ranList[i] -=  xbar
        ranList[i] *= ranList[i]
    s = sum(ranList)
    s /= len(ranList)-1
    return math.sqrt(s)

print(stDev(ranList))

只需使用python提供的
statistics
模块,如下所示:

import statistics 
import random

# creating sample data 
lst = []
for x in range(0, 5):
   n = random.randint(1, 10)
   lst.append(n) # also don't add semicolons after lines of code, that's not proper formatting

# Prints standard deviation 
print("Standard Deviation of sample is:", statistics.stdev(lst))
如果您试图不使用内置函数,请尝试:

# Find the mean
total = 0
for num in lst:
   total += num

mean = total/len(lst)

# Subtract mean from each value and square
new_lst = []
new_sum = 0
for item in lst:
   square_diff = (item - mean)**2
   new_lst.append(square_diff)

# Find the average of all the values 
new_lst_sum = 0
for item in new_lst:
    new_lst_sum += item

# print results
standard_dev = float(new_lst_sum/len(new_lst))
print("Your standard deviation is:", standard_dev)

这会增加代码量,降低效率,但您可以清楚地看到逻辑。

您应该检查代码中的两行。第一个是:

s/=总和(ranList)-1

为了计算标准偏差,需要将总和除以N-1。而是将其除以sum-1。建议使用返回列表长度的函数
len(list)

第二点是:

数学.sqrt(s)


标准偏差公式要求返回变量
s
的平方根。实际上,函数
math.sqrt(float)
返回参数的根,但它不会将最终结果放入参数中。因此,您还应该将返回值分配到
s
最新版本的代码中的错误是
s/=len(ranList)-1
应该是
s/=len(ranList)
。有两件事要考虑。首先,不要为你的测试随机生成列表,而是使用硬编码的列表,以便于验证。其次,考虑在函数中制作第二个内部列表,这样就不会破坏传入的列表。

解决办法是

import math

def stDev(lst):
    xbar = sum(lst)/len(lst)
    mlst = [(v-xbar)**2 for v in lst]
    s = sum(mlst)/len(mlst)
    return math.sqrt(s)

test = [1, 7, 4, 1, 10]
result = stDev(test)
print(test, result)

你说的“搞砸”是什么意思?你期望得到什么样的结果,你实际上得到了什么?另外,您尝试了什么样的调试,以及您将问题缩小到了哪里?与您的问题无关,但您不需要在每行末尾加分号。您定义了一个函数
stDev
,但似乎从未调用它。另外,可能不要调用您的私有变量
list
(它与一个关键的内置类型冲突)
s/=[sum(list)-1]
我必须在没有诸如统计等内置功能的情况下完成这项工作,希望编辑的代码helps@Max这是一种不同的标准偏差公式(除以
len(new_lst)
,而不是
len(new_lst)-1
)-是样本标准偏差,而不是总体标准偏差的估计值。没有错,但清楚区分是很重要的。非常感谢!感谢您的反馈!请看我的更新代码,即使考虑到你的评论,它仍然给了我错误的st.dev。这个答案与@alaniwi的最后一条评论相匹配。就是这样!非常感谢@马里奥-可能是。我正在撰写和测试一个答案,而OP的评论和更改正在出现。要说使用
len(ranList)-1
而不是
len(ranList)
是一个“bug”,这有点夸张。这取决于是否打算考虑@alaniwi——有100种方法可以剥统计猫的皮(+/-10)。