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

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

    原文作者:cumian8165
    原文地址: https://blog.csdn.net/cumian8165/article/details/108098220
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞