Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to find the mean, median and modes

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to use Python to find the mean, median and mode, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Let's calculate the mean, median, and mode of a list of integers. The following code snippet creates a list named grades, and then uses the built-in sum and len functions to "manually" calculate the average-- sum calculates the sum of grade (397), and len calculates the number of grade (5):

Grades = [85, 93, 45, 89, 85] sum (grades) / len (grades)

Similar to the functions min and max (introduced in the article "hand-in-hand teaching you to find maximum and minimum values with Python"), sum and len are examples of reduction in functional programming, reducing the set of values to a single value-the sum and number of values.

The statistics module of the Python standard library provides functions for calculating the mean, median, and mode, which are also reductions. To use these features, you first need to import the statistics module, as follows:

Import statistics

Then, you can use "statistics." Add the name of the function to be called to access the function of the module. The following code uses the mean, median, and mode functions of the statistics module to calculate the mean (79.4), median (85), and mode (85) of the list grades, respectively:

Statistics.mean (grades) statistics.median (grades) statistics.mode (grades)

Where the argument to each function must be iterable, in this case the list grades. To verify that the median and mode are correct, you can use the built-in sorted function to get a copy of the list grades in the order in which the values are incremented:

Sorted (grades)

Results:

[45, 85, 85, 89, 93]

The list grades has an odd number of values (5), so median returns an intermediate value (85). If the list contains an even number of values, median returns the average of the two intermediate values. From the sorted list, you can see that 85 is the mode because it appears the most (twice). A list similar to the following causes the mode function to produce a StatisticsError:

[85, 93, 45, 89, 85, 93]

Because there are two or more "most" values. Such a set of values is bimodal, with 85 and 93 appearing twice.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report