在 Python 中计算算术平均值
术语算术平均值是数字的平均值。确定算术平均值的数学公式是将数字之和除以计数。它是在 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
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串