为数字列表编写自定义求和函数

时间:2018-12-09 11:15:19

标签: python

我有一个列表,我想计算其平均值。得到平均值后,我想用平均值减去列表中的每个项目。得到所有值后,我要求和并求和。

x=[17,13,12,15,16,14,16,16,18,19]
Average is 15.6
For example list look like: x=[17,13,12,15,16,14,16,16,18,19]

步骤1:

Find average value
sum(x)/len(x) 
average value is 15.6

步骤2: 用平均值减去列表中的每个项目

17-15.6  =  1.4
13-15.6  = -2.6
12-15.6  = -3.6
15-15.6  = -0.6
16-15.6  =  0.4
14-15.6  = -1.6
16-15.6  =  0.4
16-15.6  =  0.4
18-15.6  =  2.4
19-15.6  =  3.4

步骤3: 之后,我要应用每个结果的平方

1.4   * 1.4    = 1.96
-2.6  * -2.6   = 6.76
-3.6  * -3.6   = 12.96
-0.6  * -0.6   = 0.36
0.4   * 0.4    = 0.16
-1.6  * -1.6   = 2.56
0.4   * 0.4    = 0.16
0.4   * 0.4    = 0.16
2.4   * 2.4    = 5.76
3.4   * 3.4    = 11.56

第4步: 之后,我想对平方求和

1.96 + 6.76 + 12.96 0.36 +0.16 + 2.56 + 0.16 + 0.16 + 5.76 + 11.56 = 42.4

我尝试了这种方法,我能够进行到步骤3

def sumx(x):
    for i in x:                        #
        result=i-sum(x)/len(x)         #
        result=result*result           #
        #result="{:.2f}".format(result)
        print("{:.2f}".format(result))
        total=0
        for i in result:
            total +=i
    return (total)
sumx(x)

错误消息

  

关于结果i的错误消息:TypeError:'float'对象不是   可迭代的

所需的输出是42.4

5 个答案:

答案 0 :(得分:0)

基本上,您想计算列表x的均值和方差。

x = [17, 13, 12, 15, 16, 14, 16, 16, 18, 19]
n = len(x)
mean = sum(x) / n
var = sum((t - mean)**2 for t in x) / n

如果只需要平方和,则在计算n时将var除以除法。

因此,如果您只想返回平方和:

def ssq(x):
    mean = sum(x) / len(x)
    return sum((t - mean)**2 for t in x)

答案 1 :(得分:0)

您可以使用numpy并列出理解:

import numpy as np
x=[17,13,12,15,16,14,16,16,18,19]

# using mean function from numpy
sum((y - np.mean(x))**2 for y in x) # 42.4

# calculating mean on our own (pure python)
sum((y - (sum(x)/float(len(x))))**2 for y in x) # 42.4

# function to calculate sse
def sse(x):
    m = np.mean(x)
    return sum((y - m)**2 for y in x)

答案 2 :(得分:0)

您的错误在这里:

total=0
for i in result:
    total +=i

这里的结果只是一个数字,而不是一个列表。因此,整个代码写错了。

相反,您可以在函数的起始位置初始化total并摆脱for循环。

这是一个简单的代码:

x = [17,13,12,15,16,14,16,16,18,19]

average = sum (x) / float (len (x))
total = 0

for number in x:
    result = pow ((number - average), 2)
    total += result
print total

如果您希望将其用作功能:

x = [17,13,12,15,16,14,16,16,18,19]

def sumx(x):
    average = sum (x) / float (len (x))
    total = 0
    for i in x:                        
        result = i - average
        result = result * result           
        total += result
    return (total)

print sumx(x)

答案 3 :(得分:0)

我对您的问题的尝试虽然没有我想像的其他答案那么复杂,但基本上是逐步进行的:D

import numpy as np

def std(x = [], to = None):

    if to is None:
        to = []
        to.append(x)

    y = np.mean(to)

    z = []
    for item in to:
        z.append((item - y)**2)

    print(sum(z[0]))

std([17,13,12,15,16,14,16,16,18,19])

答案 4 :(得分:0)

您可以使用numpy并将计算矢量化:

import numpy as np

x = np.array([17,13,12,15,16,14,16,16,18,19])
normalized_vector = x - np.mean(x)
result = np.dot(normalized_vector, normalized_vector)

...因为点积等于平方元素的总和。为了更加简洁:

result = np.var(x) * x.size

...计算的 n 方差应该等于您想要的。