Calculating the arithmetic mean in Python
The term arithmetic mean is the average of the numbers. The mathematical formula to determine the arithmetic mean is to divide the sum of the numbers by the count. It is determined in Python in the following way.
- Use mathematical formulas.
-
Use the mean() function from the Python standard library, for example
NumPy
,statistics
,scipy
.
Calculate the arithmetic mean using mathematical formulas in Python
Follow this procedure using mathematical formulas.
listnumbers = [1, 2, 4]
print("The mean is =", sum(listnumbers) / len(listnumbers))
Output:
The mean is = 2.3333333333333335
numpy.mean()
Calculate the arithmetic mean
using the function in Python
NumPy
The standard library contains a function for determining the arithmetic mean in Python mean()
. To do this, first import NumPy
the library. Refer to the example below.
import numpy
listnumbers = [1, 2, 4]
print("The mean is =", numpy.mean(listnumbers))
Output:
The mean is = 2.3333333333333335
statistics.mean()
Calculate the arithmetic mean
using the function in Python
statistics
The library contains mean()
a function for determining the arithmetic mean. To do this, first import statistics
the library. Follow the example below.
import statistics
listnumbers = [1, 2, 4]
print("The mean is =", statistics.mean(listnumbers))
Output:
The mean is = 2.3333333333333335
scipy.mean()
Calculate the arithmetic mean
using the function in Python
scipy
The library contains mean()
a function for determining the mean. To do this, first import scipy
the library. Here is an example.
import scipy
listnumbers = [1, 2, 4]
print("The mean is =", scipy.mean(listnumbers))
Output:
The mean is = 2.3333333333333335
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Read the first line of a file in Python
Publish Date:2025/05/05 Views:192 Category:Python
-
In Python, we have built-in functions to handle different file operations. A text file contains a sequence of strings where each line \n is terminated by a newline character. In this tutorial, we will learn how to read the first line of a t
Cosine Similarity in Python
Publish Date:2025/05/05 Views:81 Category:Python
-
Cosine similarity measures the similarity between two lists of vectors by calculating the cosine angle between them. If you consider the cosine function, it has a value of 1 at 0 degrees and -1 at 180 degrees. This means that for two overla
Exponential and Logarithmic Curve Fitting in Python
Publish Date:2025/05/05 Views:79 Category:Python
-
Curve fitting is a very effective tool that is widely used in analysis. Curve fitting method studies the relationship between independent variables (also called predictor variables) and dependent variable (called response variable). This me
Calculating Slope in Python
Publish Date:2025/05/05 Views:60 Category:Python
-
In mathematics, the slope of a given line is the value at which its steepness is calculated. It also helps to characterize the direction of a given line. The extent of a line can also be calculated using Python programming language. This ar
How to Randomly Select Elements from a List in Python
Publish Date:2025/05/05 Views:68 Category:Python
-
This tutorial showed you how to randomly select an element from a list in Python. There are multiple simple ways to achieve this goal, all of which involve importing Python modules. This tutorial will cover solutions that require the random
How to Replace an Element in a Python List
Publish Date:2025/05/05 Views:109 Category:Python
-
We can replace elements in Python lists in many ways. We can use Python list element indexing, for loops, map functions, and list comprehensions. This article will discuss the above methods to find and replace elements in Python lists. Sear
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:86 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:194 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra