How to use the Python mean method
Python is one of the most popular programming languages for statistics and AI development. So it’s hardly surprising that Python has many methods and modules to help you process massive amounts of data. Here, we’ll look at the Python mean, a method that lets you determine the mean value of several numbers.
What is the Python mean method?
Syntax and functionality
Python’s mean method is simple: It takes a set of numbers and gives you back their mean. The numbers must be summarised in a list as a single argument. Both integers and floating-point numbers can be used. The result of the calculation is always output as a floating-point number. Follow its syntax and operation in the following example.
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
PythonAs you can see in the example above, the method works exactly as described – it gets a list of numbers and returns their mean. This result is always output as a floating-point number, displayed as ‘np.mean([1, 3, 2])’. The mean value of these numbers is exactly 2 although it’s displayed as ‘2.0’. In addition, this example shows that you can pass the list on directly or as a variable that has already been created.
Two other important details about the Python mean method can be taken from this example. The first refers to the precision of floating-point numbers and the second to the ‘NumPy’ module.
Displaying floating-point numbers
If you do the second calculation from the example above manually, you’ll get exactly 1.88. Even though the program doesn’t show this result. This is due to how floating-point numbers are represented in the binary number system, on which all modern calculators are based. Just like in the ordinary decimal system there are some fractional numbers that can’t be accurately represented in the binary system. An example of this in the decimal system is 0.3333…. You can always add another 3, but you can never reach exactly one third.
Although this problem is unavoidable, the precision of floating-point numbers is high enough that it won’t make much of a difference in most cases. Still, it’s good to keep these accuracy problems in mind when you’re working with floating-point numbers.
The NumPy module
As you can see in the example above, the mean method does not belong to the Python standard library. To use this method, you need to import it from an external module, like ‘NumPy’ or ‘statistics’. You may need to install these modules, but once that’s done, you can easily integrate them. To do this, simply add the code line ‘import numpy’ at the start of your program. Alternatively, if you want to reference the module under a different name in the future, you can write ‘import numpy as x’, where ‘x’ is your desired name.
Are you looking to publish your web application quickly, easily, and even directly via Git? Then Deploy Now from IONOS could be the ideal solution for you!
Python mean alternatives
As mentioned, the mean method doesn’t belong to Python’s standard library, but to external modules such as ‘NumPy’ that you need to import. If it’s not possible or you don’t want to, you can also implement the mean method yourself. All you need to do is write a few lines of code:
def mean(numbers):
return sum(numbers)/len(numbers)
PythonThe ‘sum’ and ‘len’ methods used here are built into Python’s standard library, meaning you don’t need to import them. As you can see in the following example, implementing it works exactly like ‘mean’ from ‘numpy’.
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
PythonIn addition to methods like ‘mean’, Python operators are essential for the processing of data sets. In our article on the topic, we look at each operator and what they offer.
Python mean use cases
Now we’ll show you some examples of the mean method in practice. In the following program, the user is repeatedly asked to enter a number. This number is converted from a string in an integer and added to a list. The average value of the elements in this list is then continuously updated and output with each new entry.
import numpy as np
list = []
while(True):
list.append(int(input('add number to list: ')))
print(np.mean(liste))
PythonIn the next example, there are three people, each with an x, y, and z coordinate. The mean method is applied, and the meeting point of the three people is calculated and output.
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