迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

在 Python 中计算算术平均值

作者:迹忆客 最近更新:2023/12/21 浏览次数:

术语算术平均值是数字的平均值。确定算术平均值的数学公式是将数字之和除以计数。它是在 Python 中通过以下方式确定的。

  • 使用数学公式。
  • 使用 Python 标准库中的 mean() 函数,例如 NumPystatisticsscipy

在 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便