average

Calculate cumulative average (mean)

蹲街弑〆低调 提交于 2019-11-26 05:39:37
问题 I would like to know how to calculate the cumulative average for some numbers. I will give a simple example to describe what I am looking for. I have the following numbers vec <- c(1, 2, 3, 4, 5) If I do the average of these numbers I will get 3 as a result. Now, how to do the cumulative average of these numbers. 回答1: In analogy to the cumulative sum of a list I propose this: The cumulative average avg of a vector x would contain the averages from 1st position till position i . One method is

Calculating arithmetic mean (one type of average) in Python

元气小坏坏 提交于 2019-11-26 03:33:03
问题 Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers? 回答1: I am not aware of anything in the standard library. However, you could use something like: def mean(numbers): return float(sum(numbers)) / max(len(numbers), 1) >>> mean([1,2,3,4]) 2.5 >>> mean([]) 0.0 In numpy, there's numpy.mean(). 回答2: NumPy has a numpy.mean which is an arithmetic mean. Usage is as simple as this: >>> import numpy >>> a = [1, 2, 4] >>>

Finding the average of a list

一曲冷凌霜 提交于 2019-11-26 01:31:46
问题 I have to find the average of a list in Python. This is my code so far l = [15, 18, 2, 36, 12, 78, 5, 6, 9] print reduce(lambda x, y: x + y, l) I\'ve got it so it adds together the values in the list, but I don\'t know how to make it divide into them? 回答1: On Python 3.4+ you can use statistics.mean() l = [15, 18, 2, 36, 12, 78, 5, 6, 9] import statistics statistics.mean(l) # 20.11111111111111 On older versions of Python you can do sum(l) / len(l) On Python 2 you need to convert len to a float