简体   繁体   English

需要帮助改进此 Python function 以确定平均绝对偏差 (MAD)

[英]Need help improving this Python function for determining Mean Absolute Deviation (MAD)

def what_is_mad(list_of_nums):
    xbar = what_is_mean(list_of_nums)
    for num in list_of_nums:
        value1 = list_of_nums[0]
        value2 = list_of_nums[1]
        value3 = list_of_nums[2]
        value4 = list_of_nums[3]
        test = abs(value1 - xbar) + abs(value2 - xbar) + abs(value3 - xbar) + abs(value4 - xbar)
        mad = test / xbar
    return int(mad)

what_is_mad(2, 2, 4, 4)

I'm trying to build my own purely Python function for determining Mean Absolute Deviation (MAD) and would like some recommendations on how I can enhance this code.我正在尝试构建自己的纯粹 Python function 来确定平均绝对偏差(MAD),并希望获得一些关于如何增强此代码的建议。 It is currently limited to four values, as seen above, but I'd like to be able to have the function ingest any number of values.它目前仅限于四个值,如上所示,但我希望能够让 function 摄取任意数量的值。 Thanks in advance.提前致谢。

The way(formula) of calculating the Mean absolute deviation is wrong in your code.您的代码中计算平均绝对偏差的方式(公式)是错误的。 You should calculate all your absolute deviations, sum them up and divide by the number of elements in the list_of_sums, not by the mean.您应该计算所有绝对偏差,将它们相加并除以 list_of_sums 中的元素数量,而不是均值。 If you mean pure python as not using any libraries this code will do.如果您的意思是纯 python 不使用任何库,则此代码将执行此操作。

xbar = sum(list_of_nums)/len(list_of_nums)
dev = 0
for num in list_of_nums:
    dev = dev + abs(num - xbar)
mad = dev/len(list_of_nums)
print(mad)

You can use list comprehension and write the code in less number of lines.您可以使用列表推导并以更少的行数编写代码。 But it would create another list which will cost space to store it, hence not optimized.但它会创建另一个列表,这将花费空间来存储它,因此没有优化。

Otherwise you can use numpy, scipy or pandas and they have inbuilt functions to calculate MAD否则,您可以使用 numpy、scipy 或 pandas 并且它们具有计算 MAD 的内置函数

xbar = mean(list_of_nums)
test=0
for num in list of nums:
    test = test + abs(num - xbar)
mad = test/xbar
return int(mad)

This loops through the list and sums up the absolute deviations one at a time, then does the mad calculation.这循环遍历列表并一次总结绝对偏差,然后进行疯狂计算。

In terms of an optimized solution, pandas has a method for this.在优化解决方案方面, pandas对此有方法。

mad_in = [1,2,3,4...]

mad_out = pandas.Series(mad_in).mad()

This can be accomplished in a couple lines using python's built-in sum operation and list comprehension这可以使用 python 的内置sum操作和列表理解在几行中完成

def what_is_mad(*list_of_nums):
    count = len(list_of_nums)
    mean = sum(list_of_nums) / count
    mad = sum((abs(num - mean) for num in list_of_nums)) / count
    return mad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM