python列表平均值函数_如何计算列表的平均值-统计信息和Python的均值函数详细解释

2023-05-16

python列表平均值函数

Mathematics and programming go hand in hand. If you are a programmer, at some point you will have to use mathematics.

数学和程序设计齐头并进。 如果您是程序员,则有时必须使用数学。

Data science, machine learning, artificial intelligence, and cryptocurrencies are all based on complex underlying mathematical principles.

数据科学,机器学习,人工智能和加密货币均基于复杂的基础数学原理。

However, using math functions doesn't have to be complex! Python abstracts everything away, so once you understand the concepts, you will not need to understand the full details of the implementation.

但是,使用数学函数不必太复杂! Python将所有内容抽象化,因此,一旦您理解了这些概念,就无需了解实现的全部细节。

数学不必太吓人 (Math doesn't have to be scary)

There are a lot of mathematical functions you will come across. If you are working with data or analytics, it's important that you understand some mathematical principles and functions.

您将遇到很多数学函数。 如果您正在使用数据或分析,那么了解一些数学原理和函数很重要。

One such function you must understand is the mean function.

您必须了解的一种这样的功能是mean功能。

Don't be put off by the name – there is nothing mean (pun intended) about the mean function in Python.

不要被名字拖延– Python中的mean函数没有任何意义(双关语)。

This post is self contained, but I expect you have some experience working with Python and that you know what a Python list is. If not, check out this article before moving on.

这篇文章是独立的,但是我希望您有使用Python的经验,并且知道Python列表是什么。 如果不是这样,请先阅读本文,然后再继续。

Once you're finished, come back and join me for a deep dive into the mean function.

完成后,请返回并加入我,深入研究mean函数。

统计 (Statistics )

So you want to learn about the mean function. That's great! But before we look at this function its important to look at the discipline from which it originates: statistics.

因此,您想了解mean函数。 那很棒! 但是,在我们研究此功能之前,先了解其起源的学科很重要:统计学。

In the image above we see a graph. A graph is a pictorial representation that shows the relationship of one variable in relation to another.

在上图中,我们看到了一个图形。 图形是一种图形表示形式,显示了一个变量与另一个变量之间的关系。

Graphs are useful because they allows us to organize data so that we can quickly see trends and relationships between the data. A graph is just one tool that we can use to visualize and analyze data.

图形之所以有用,是因为它们使我们能够组织数据,以便我们可以快速查看数据之间的趋势和关系。 图形只是我们可以用来可视化和分析数据的一种工具。

Statistics is a branch of mathematics that allows us to have a systematic way of classifying, analyzing and interpreting data. This is important because with statistics, we have a collection of ready made tools to do each of those things.

统计学是数学的一个分支,它使我们能够有系统地分类,分析和解释数据。 这一点很重要,因为有了统计信息,我们就可以使用现成的工具来完成所有这些事情。

Imagine if you needed to reinvent a saw every time you needed to cut a piece of wood. We would many people calling saws by different names, even though they do the same thing. To avoid this problem, we gave the saw a name that everyone can refer to it by.

想象一下,如果您每次需要切割一块木头时都需要重新制作锯。 即使他们做同样的事情,我们也会有许多人用不同的名字称锯。 为避免出现此问题,我们给了锯一个名称,每个人都可以引用它。

The same happens in statistics — we have tools well known tools that everyone is familiar with. One such tool is the mean.

统计数据中也是如此-我们拥有大家都熟悉的众所周知的工具。 平均值就是这样一种工具。

模式,中位数和均值 (Mode, Median and Mean)

Though mean is perfectly capable of standing on its own, it's usually taught as part of a trio, which includes the mode, median, and mean.

尽管平均数完全有能力独立存在,但通常将其作为三重奏的一部分进行讲授,包括模式,中位数和均值。

Let's look at a group of numbers so you'll understand what is happening here. Imagine you have the numbers below:

让我们看一组数字,以便您了解这里发生的情况。 假设您有以下数字:

1,2,3,3,4,6,9 (1, 2, 3, 3, 4, 6, 9)

Say we wanted to express which number occurs the most times. It would be 3, and the name we give this property is mode. The mode is the number which is the most frequent in a set we are examining.

假设我们想表达哪个数字出现次数最多。 它将是3,而我们给此属性的名称是mode。 模式是我们正在检查的集合中最频繁的数字。

The number in the middle of an ordered set is called the median. To find the median of a numerical set, arrange the numbers from smallest to largest and then look at the number in the middle. The set of numbers above is already arranged from least to greatest, so the median number is also 3.

有序集合中间的数字称为中位数。 要查找数值集的中位数,请从最小到最大排列数字,然后查看中间的数字。 上面的数字集已经按照从最小到最大的顺序排列,因此中位数也是3。

Finally, the mean is another way to refer to the average of the set. To find the mean, just add all the numbers together and divide it by the total number of elements in the set. In the case of the numbers above, if we add them all together, we get 28. The total number of items in the set is  7, so our mean is 4.

最后,均值是引用集合平均值的另一种方法。 要找到均值,只需将所有数字加在一起,然后除以集合中元素的总数即可。 在上述数字的情况下,如果将它们全部加在一起,我们将得到28。集合中的项目总数为7,因此我们的均值为4。

为什么我们需要中庸? (Why Do We Need the Mean?)

So at this point you may be wondering why would we need to find the mean of a number anyway.

因此,在这一点上,您可能想知道为什么我们仍然需要找到数字的均值。

The thing is, even statistics itself is subdivided into several groups. Just as you have tools that are used for working with wood and others for working with metal, some tools in statistics are grouped into classes since they are used for a similar purpose.

事实是,甚至统计本身也被细分为几组。 就像您拥有用于木材加工的工具和其他用于金属加工的工具一样,统计中的某些工具也归类为类,因为它们用于类似目的。

One such group in statistics is called summary statistics. One of the things statistics is used for is to describe data, and summary statistics is a collection of tools used for that purpose. One of the items in that class of tools is the mean.

统计中的一种这样的组称为汇总统计。 统计信息的用途之一是描述数据,摘要统计信息是用于此目的的工具的集合。 该类工具中的一项是平均值。

The mean is important due to helping us analyze what is known as a distribution. In statistics, a distribution is a method we use to look at a variable we want information on. Using a distribution we will look at the values of this variable and how often it occurs.

由于有助于我们分析所谓的分布,因此平均值很重要。 在统计数据中,分布是一种用于查看需要信息的变量的方法。 使用分布,我们将查看此变量的值及其发生的频率。

If we collect data, a common type of distribution we see is the normal distribution which takes the form of the bell curve:

如果我们收集数据,我们会看到一种常见的分布类型,它是呈钟形曲线形式的正态分布:

That is to say the variable will have a common value toward which it tends, as well as a starting point and an end point.

也就是说,变量将具有其趋向于的共同值,以及起点和终点。

What the mean does is that it allows us to take a distribution like this and look at the central tendency of the variable, which is the point at which the values of the variable tend to cluster.

平均值的意思是,它允许我们采用这样的分布并查看变量的集中趋势,即变量值趋于聚集的点。

Thus we can say the mean describes the central tendency of the distribution.

因此,我们可以说均值描述了分布的集中趋势。

用Python计算平均值 (Calculating the Mean in Python )

We can manually calculate the mean if we have a small numerical data set it we have a few values to work with. However, when we have hundreds or thousands of values in a data set it becomes impossible to calculate it by hand.

如果我们有一个小的数值数据集,我们可以手动计算平均值,但我们需要处理一些值。 但是,当我们在一个数据集中有成百上千个值时,就无法手动计算它。

Since Python is a "batteries included" language, the way we can do this is to use the mean function of the statistics module within Python.

由于Python是一种“包含电池”的语言,因此,我们可以使用Python中统计模块的mean函数来实现。

Let's use the mean function to calculate the mean of the numerical data set we had earlier in the post:

让我们使用mean函数来计算我们之前发布的数值数据集的均值:

# 1. import the statistics module
import statistics

# 2. list containing our numerical data set
numerical_data_set = [1, 2, 3, 3, 4, 6, 9]

# 3. calculate the mean
calc_mean = statistics.mean(numerical_data_set)

# 4. print our calculated mean
print("Mean is: ", calc_mean)

Our code consists of a 4 step sequence that we can use to calculate the mean:

我们的代码由4个步骤组成,可用于计算均值:

  1. We import the statistics module that contains our mean function

    我们导入包含平均值函数的统计模块
  2. We create a Python list containing the numerical data set of which we would like to calculate the mean

    我们创建一个Python列表,其中包含我们想要计算均值的数值数据集
  3. We calculate the mean and store the result in a variable, calc_mean

    我们计算平均值并将结果存储在变量calc_mean

  4. We output our calculated mean so that we can get visual feedback

    我们输出计算出的平均值,以便获得视觉反馈

When we run the code, we will get the following output:

运行代码时,将获得以下输出:

The program outputs the same value as our manual calculations. When we are working with large data sets, this function will be able to scale to handle whatever we can throw at it.

该程序输出与我们的手动计算相同的值。 当我们处理大型数据集时,此函数将能够扩展以处理我们可以扔给它的任何东西。

结语 (Wrapping Up)

In this post we looked at the mean function in Python. We began by discussing statistics as a whole, then took a deep dive into mean.

在这篇文章中,我们研究了Python中的mean函数。 我们从讨论整个统计数据开始,然后深入探讨均值。

Now that you have a solid understanding of statistics and the mean function in Python, you can use it in your own programs.

既然您对Python的统计数据和mean函数有了深入的了解,就可以在自己的程序中使用它。

If you liked this article, then you may also be curious about learning about data structures and algorithms. If you want a simple, clear, step by step guide to learning about data structures and algorithms without having to write a single line of code, then you can check out the book Codeless Data Structures and Algorithms.

如果您喜欢本文,那么您也可能对学习数据结构和算法感到好奇。 如果您想要一个简单,清晰,分步的指南来学习数据结构和算法,而不必编写任何代码,那么可以参考《无代码数据结构和算法》一书。

Read the book here:

在这里阅读这本书:

翻译自: https://www.freecodecamp.org/news/how-to-calculate-the-average-of-a-list-statistics-and-pythons-mean-function-explained-in-detail/

python列表平均值函数

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python列表平均值函数_如何计算列表的平均值-统计信息和Python的均值函数详细解释 的相关文章

随机推荐

  • Linux 配置Gradle

    一 下载gradle 如果windows中有可以直接拷贝 xff0c 如果没有可以去官网下载 http www gradle org downloads 二 解压下载得到的gradle unzip gradle 2 2 1 all zip
  • gitlab配置通过smtp发送邮件(QQ exmail腾讯企业为例)

    首先祭出官网文档链接 xff1a https docs gitlab com omnibus settings smtp html 其实官网已经说的很清楚了 xff0c 并且给出了QQ邮箱的范例 xff08 BAT还是屌的 xff09 1
  • 文本编辑器Notepad++使用技巧

    除了语法高亮 xff0c 一般不用操作 还有两点经常使用的 xff1a 正则表达式查找替换和列模式编辑 这些可以在VS Eclipse Word等里也有 xff0c 但是有时打开一个文件就慢了 本来想总结记录一下技巧的 xff0c 却无意中
  • linux系统磁盘block、inode占满处理

    1 磁盘的block占满 xff0c 查看命令 df vh 然后查看占用百分比 2 磁盘inode占满 xff0c 查看命令df ih 同样也是查看占用百分比 block占满处理办法 需要用到的命令如下 LL 列出当前目录下的文件 df v
  • Code::Blocks平台下Fortran的编译

    问题背景 xff1a 因为之前学习数值方法 xff0c 有用到Fortran的地方 xff0c 所以上网查了一些资料 关于Fortran语言的编辑器安装 xff0c 目前本人接触到的支持Fortran的编辑器有VisualStdio和Cod
  • powershell远程连接

    在Linux中 xff0c 我们可以使用安全的SSH方便的进行远程管理 但在Windows下 xff0c 除了不安全的Telnet以外 xff0c 从Windows Server 2008开始提供了另外一种命令行原创管理方式 xff0c 那
  • 2022年学习总结暨2023年规划

    2022年总结 2022年是我在C站的创作元年 xff0c 在第一年也收获了不少成就 xff0c 比如 Java领域新星创作者 发布100篇博文 拿到了C站的书包 吃到了C站的月饼 成功上榜了330 43 截止目前收获粉丝8600 43 在
  • 《Prometheus+Grafana 实践派》专栏介绍

    专栏名称 Prometheus 43 Grafana 实践派 专栏介绍 本专栏根据本公司统一监控落地实践编写 在该专栏您将学到 企业级监控的选型Prometheus的基础知识Grafana的基础知识快速搭建Prometheus 43 Gra
  • 泊松分布–计算概率分布的公式

    Probability Distributions play an important role in our daily lives We commonly use them when trying to summarise and ga
  • Prometheus 的介绍和安装

    介绍 Prometheus 是一个开源的监控和报警系统 最初由SoundCloud于2012年创建 随着越来越多的公司采用Prometheus以及非常活跃的社区 Prometheus于2016年加入云原生基金会 成为Kubernetes之后
  • 因为锁的问题,我们被扣了1万

    前言 春节放假期间 xff0c 一个项目上的积分接口被刷 xff0c 而且不止一个人在刷 xff0c 并且东西也被兑走 xff0c 放假晚上被人叫起来排查问题 xff0c 通过这个人的积分明细观察 xff0c 基本一秒就能获取一次 xff0
  • Prometheus 告警机制介绍及命令解读

    本文您将了解到Prometheus 告警模块Alertmanager的架构介绍 核心概念 命令解析和AMTool的基本使用 Prometheus的告警模块并不存在于Prometheus中 而是 以独立项目Alertmanager存在 Pro
  • Prometheus 告警模块配置深度解析

    本文您将了解到Prometheus 告警模块Alertmanager 配置的深度解析 Alertmanager 配置解析 Alertmanager 配置可以用命令行配置 也可以通过配置文件配置 命令行用来配置不可变的系统参数 配置文件用来定
  • 不会前端没事,用GWT Boot和Spring Boot构建Web程序

    本文介绍了一种使用Java构建Web应用程序的方式 xff0c 其中GWT或者J2CL是必不可少的 xff0c 另外还有多个UI框架可以配套使用 xff0c 比如Domino UI VueGWT GWT Material Design GM
  • Prometheus 监控云Mysql和自建Mysql(多实例)

    本文您将了解到 Prometheus如何配置才能监控云Mysql 包括阿里云 腾讯云 华为云 和自建Mysql Prometheus 提供了很多种Exporter 用于监控第三方系统指标 如果没有提供也可以根据Exporter规范自定义Ex
  • 2023年 Java 发展趋势

    GitHub 语言统计表明 xff0c Java在编程语言中排名第二 xff0c 而在2022年的TIOBE指数中 xff0c Java排在第四 抛开排名 xff0c Java是自诞生以来企业使用率最高的编程语言 xff0c 作为一种编程语
  • Spring Boot 中文参考指南(二)-Web

    Spring Boot 版本 2 7 8 原文 xff1a https docs spring io spring boot docs 2 7 8 reference htmlsingle Spring Boot 3 x 第一个 GA 版本
  • Java 编写Vue组件(VueGWT的初尝试)

    在之前 xff0c 我曾写过这样的文章 不会前端没事 xff0c 用GWT Boot和Spring Boot构建Web程序 xff0c 这篇文字使用的Domino UI来做前端页面 xff0c 由于现在更流行VUE xff0c 并且VUE的
  • Spring Boot 源码阅读初始化环境搭建

    在开始源码阅读之前 xff0c 需要先搭建一个简易的SSM环境用于测试 xff0c 这里不过解释怎么搭建 xff0c 相信都开始看源码了 xff0c 还不会搭建Demo环境就先去学习下基础 demo环境地址 xff1a https gith
  • python列表平均值函数_如何计算列表的平均值-统计信息和Python的均值函数详细解释

    python列表平均值函数 Mathematics and programming go hand in hand If you are a programmer at some point you will have to use mat