Python is among the most popular programming languages when it comes to statistics and artificial intelligence (AI) development. The fact that Python already offers several methods and modules intended to greatly facilitate data processing is therefore not surprising. Discover Python's mean method with us, and learn how to determine the average value of all kinds of numbers.
Quick overview of Python's mean method¶
Syntax and operation¶
It's really simple to explain how the mean method works: it uses a set of numbers to return their average value to you. To do this, the numbers in question must be grouped within the same list, transmitted as a single argument. The method can be used with integers as well as with floating point numbers. Moreover, the calculation result always appears in the form of a floating number. The example below illustrates the syntax and operation of this method.
import numpy as np
x = np.mean([1, 3, 2])
numbers = [1, 1.3, 4, 2.1, 1.0]
y = np.mean(numbers)
print(x) # output: 2.0
print(y) # output: 1.8800000000000001
Python
As shown in the example above, the method works exactly as we explained: a list of numbers is passed to it, and it takes care of returning their average value. This result is always in the form of a floating point number, as you can see in the example (“np.mean([1, 3, 2])”)). 2 is the corresponding average value; and even if it is an integer, the result is still “2.0”. This example further shows that you can choose to pass the list directly, or as an already created variable.
This example also contains two other important details about Python's mean method: the first is related to the precision of floating point numbers, and the second concerns the “numpy” module.
Displaying floating point numbers¶
If you manually perform the second calculation in the example above, then you will get an exact result of 1.88. However, this is not what the program displays. This difference is explained by the display of floating point numbers as part of the binary number system, which forms the basis of all modern computers. As is also the case in the usual decimal system, it is impossible to accurately represent certain fractional numbers in the binary system. In the decimal system, we could take the value “0.3333…” as an example. You can add as many “3s” as you want, but you will never get exactly to a third.
This problem cannot be avoided, but the floating point numbers nevertheless remain precise enough that in most cases the difference is tiny. You should, however, be aware of the precision issues associated with floating point numbers when working with them.
The “numpy” module¶
As you can see in the example above, the mean method does not not part of the Python standard library. Indeed, to use this method, you must import it from an external module such as “numpy” or “statistics”. You may have to install these modules, but once you do, they will integrate very easily into your program. To do this, simply add the “import numpy” line of code to the beginning of your program. To reference this module under another name in the future, do not hesitate to also write “import numpy as x”, “x” corresponding to the name you want to give to the module.
Looking to quickly and easily, or even directly, publish your web application using Git? Then Deploy Now is really the IONOS solution you need!
Alternatives to Python's mean method¶
As we have seen together, the mean method does not appear in the standard Python library; to obtain it, you must import external modules such as “numpy”. If this solution is impossible or you do not wish to use it, you can also choose to implement the mean method yourself. To do this, the following few lines of code are enough:
def mean(numbers):
return sum(numbers)/len(numbers)
Python
The “sum” and “len” methods used in this implementation are integrated into the standard Python library, and can therefore be called without the need to import them. As shown in the example below, this implementation works exactly the same as the “mean” method of the “numpy” module.
def mean(numbers):
return sum(numbers)/len(numbers)
x = mean([1, 3, 2])
numbers = [1, 1.3, 4, 2.1, 1.0]
y = mean(numbers)
print(x) # output: 2.0
print(y) # output: 1.8800000000000001
Python
In addition to methods like “mean”, Python operators are essential for processing datasets. Discover our article on this subject, with a presentation of each operator and the possibilities they offer.
Examples of applying Python's mean method¶
Finally, let's review some examples of how the mean method can be used. In the following program, the user is asked several times to enter a number. Through a conversion, it goes from a String to an integer, before being added to a list. The average value of the elements present in this list is always displayed, and it is constantly updated each time the user enters a new number.
import numpy as np
list = []
while(True):
list.append(int(input('add number to list: ')))
print(np.mean(list))
Python
The example below is for three people, each with an “x”, “y”, and “z” coordinate. The mean method is then applied to calculate and display the central point for these three people.
import numpy as np
person1 = [1.5, 6.0, 4.2]
person2 = [10.0, 9.0, 7.7]
person3 = [15.5, 0.0, -5.0]
people = [person1, person2, person3]
Average position = []
i = 0
while(i < len(person1)):
temp = []
for x in people:
temp.append(x[i])
average position.append(np.mean(temp))
i = i + 1
print(average position) # output: [9.0, 5.0, 2.3000000000000003]
Python