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列表平均值函数

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

  1. python中str函数_一文让你彻底搞懂Python中__str__和__repr__?

    __str__和__repr__的异同? 字符串的表示形式 我们都知道,Python的内置函数repr()能够把对象用字符串的形式表达出来,方便我们辨认.这就是"字符串表示形式". ...

  2. python延时us函数_树莓派高级GPIO库,wiringpi2 for python使用笔记(二)高精度计时、延时函数...

    学过单片机的同学应该清楚,我们在编写传感器驱动时,需要用到高精度的定时器.延时等功能,wiringpi提供了一组函数来实现这些功能,这些函数分别是: micros() #返回当前的微秒数,这个数在调用 ...

  3. 以下哪个不是python的内置函数_以下哪个 Python 内置函数可以返回列表对象中元素个数。...

    [多选题]假设 x=[0,1,2,3],执行哪些语句之后,x 的值为[0, 1, 2]. [多选题]以下哪些对象的分隔符为逗号. [单选题]已知列表 x=[0,1,2,1,4],那么执行语句 del ...

  4. python内置函数可以返回列表元组_Python内置函数()可以返回列表、元组、字典、集合、字符串以及range对象中元素个数....

    Python内置函数()可以返回列表.元组.字典.集合.字符串以及range对象中元素个数. 青岛远洋运输有限公司冷聚吉船长被评为全国十佳海员.()A:错B:对 有源逆变是将直流电逆变成其它频率的交流 ...

  5. Python内置函数sorted()和列表方法sort()排序规则不得不说的事

    Python内置函数sorted()和列表方法sort()可以使用key参数指定排序规则,并且都是稳定排序,也就是说,对于指定规则不能涵盖的元素,本来谁在前面,排好以后谁还是在前面. 直接用代码说话: ...

  6. linux uname内核,Linux下confstr与uname函数_获取C库与内核信息

    Linux下confstr与uname函数_获取C库与内核信息 #include #include  //uname int main(int argc, char **argv[]) { struc ...

  7. R语言使用skimr包的skim_with函数自定义指定需要查看的统计信息、统计口径、查看dataframe特定数据列的summary信息、统计汇总信息(Specify statistics)

    R语言使用skimr包的skim_with函数自定义指定需要查看的统计信息.统计口径.查看dataframe特定数据列的summary信息.统计汇总信息(Specify your own statis ...

  8. R语言使用epiDisplay包的aggregate.plot函数可视化每个子集的汇总统计信息(可视化基于双变量分组下的中位数及其95%置信区间、病例组和对照组的差异)、lwd参数自定义线条的粗细宽度

    ↵ R语言使用epiDisplay包的aggregate.plot函数可视化每个子集的汇总统计信息(可视化基于双变量分组下的中位数及其95%置信区间.基于线图可视化病例组和对照组的差异).lwd参数自 ...

  9. R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、使用summary函数查看简单模型的汇总统计信息

    R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型.使用step函数基于AIC指标实现逐步回归筛选最佳模型.使用summary函数查看简单模型的汇总统计信息 目录

最新文章

  1. 人工智能市场兴起,哪款聊天机器人能提供最好的投资回报?
  2. 电感发出声音怎么解决_开关电源有“声音”可以这样解决-电源啸叫解决方案...
  3. Android Studio:missing feature Watch
  4. 用Python爬网页需要了解什么背景知识
  5. 机房定期巡检报告报告,全面详实,可以参考
  6. DAG的深度优先搜索标记
  7. Maven打包排除某个资源或者目录
  8. new Class() 与 Class.newInstance()
  9. 如何使用 vSphere Certificate Manager 替换 SSL 证书 (2097936)
  10. R中安装LightGBM(Windows 64位)
  11. Ubuntu下安装repo
  12. Blender 精确建模3D打印注意事项
  13. Python初学者必刷的五个项目,你做过几个?
  14. 能切换双显卡的android,安卓模拟器如何把双显卡切换成高性能显卡
  15. Windows\Ubuntu修复UEFI引导
  16. 嵌入式linux根文件系统制作--busybox
  17. matlab地震p波,如何确定地震深度?试试远震P波
  18. 程序员面试时候出的一些逻辑问题
  19. Facebook广告投放的正确姿势:玩转目标定位
  20. 深入了解,学习线索二叉树

热门文章

  1. css dhtml动态网页教程,CSS/JSS DHTML动态网页制作
  2. Axure如何制作app底部弹框,固定在底部
  3. java insert row,POI ,Java 操作 Excel 实现行的插入(insert row)
  4. RabbitMQ 集群部署及配置
  5. Telephony基础之SIM卡知识
  6. APP开发外包注意事项
  7. iPhone6S发售,S6 edge+存在感爆棚,苹果与三星又一轮厮杀
  8. Win10黑屏卡死原因分析--罕见的内核pushlock死锁问题
  9. excel制作可模糊匹配的下拉框
  10. Windows 10 上的Yolo3环境安装与配置