在 Python 中計算算術平均值
    
    
            Najwa Riyaz
    2023年1月30日
    
    Python
    Python Math
    
- 在 Python 中使用數學公式計算算術平均值
 - 
          
            在 Python 中使用 
numpy.mean()函式計算算術平均值 - 
          
            在 Python 中使用 
statistics.mean()函式計算算術平均值 - 
          
            在 Python 中使用 
scipy.mean()函式計算算術平均值 
術語算術平均值是數字的平均值。確定算術平均值的數學公式是將數字之和除以計數。它是在 Python 中通過以下方式確定的。
- 使用數學公式。
 - 使用 Python 標準庫中的 mean() 函式,例如 
NumPy、statistics、scipy。 
在 Python 中使用數學公式計算算術平均值
按照這個程式使用數學公式。
listnumbers = [1, 2, 4]
print("The mean is =", sum(listnumbers) / len(listnumbers))
輸出:
The mean is = 2.3333333333333335
在 Python 中使用 numpy.mean() 函式計算算術平均值
NumPy 標準庫包含用於在 Python 中確定算術平均值的 mean() 函式。為此,首先匯入 NumPy 庫。請參考下面的示例。
import numpy
listnumbers = [1, 2, 4]
print("The mean is =", numpy.mean(listnumbers))
輸出:
The mean is = 2.3333333333333335
在 Python 中使用 statistics.mean() 函式計算算術平均值
statistics 庫包含用於確定算術平均值的 mean() 函式。為此,首先匯入 statistics 庫。請按照以下示例進行操作。
import statistics
listnumbers = [1, 2, 4]
print("The mean is =", statistics.mean(listnumbers))
輸出:
The mean is = 2.3333333333333335
在 Python 中使用 scipy.mean() 函式計算算術平均值
scipy 庫包含用於確定均值的 mean() 函式。為此,首先匯入 scipy 庫。這是一個例子。
import scipy
listnumbers = [1, 2, 4]
print("The mean is =", scipy.mean(listnumbers))
輸出:
The mean is = 2.3333333333333335
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe