先决条件:统计函数简介

在数据分析和统计方面,Python是一种非常流行的语言。幸运的是,Python3提供了statistics模块,它具有非常有用的功能,例如mean(),median(),mode()等等

mean()函数可用于计算给定数字列表的均值/平均值。它返回作为参数传递的数据集的平均值。

算术平均值是数据的总和除以data-points的数量。它是一组范围内变化的值中数据中心位置的度量。在Python中,我们通常通过将给定数字的总和除以当前数字进行计数。

Given set of numbers:[n1, n2, n3, n5, n6]

Sum of data-set = (n1 + n2 + n3 + n4 + n5)

Number of data produced = 5

Average or arithmetic mean = (n1 + n2 + n3 + n4 + n5) / 5

用法:mean([data-set])

Parameters:

[data-set]:一组数字的列表或元组。

Returns:提供的data-set的样本算术平均值。

Exceptions:

TypeError当数值以外的任何其他参数作为参数传递时。

代码1:工作中

# Python program to demonstrate mean()

# function from the statistics module

# Importing the statistics module

import statistics

# list of positive integer numbers

data1 = [1, 3, 4, 5, 7, 9, 2]

x = statistics.mean(data1)

# Printing the mean

print("Mean is:", x)

输出:

Mean is:4.428571428571429

代码2:工作中

# Python program to demonstrate mean()

# function from the statistics module

# Importing the statistics module

from statistics import mean

# Importing fractions module as fr

# Enables to calculate mean of a

# set in Fraction

from fractions import Fraction as fr

# tuple of positive integer numbers

data1 = (11, 3, 4, 5, 7, 9, 2)

# tuple of a negative set of integers

data2 = (-1, -2, -4, -7, -12, -19)

# tuple of mixed range of numbers

data3 = (-1, -13, -6, 4, 5, 19, 9)

# tuple of a set of fractional numbers

data4 = (fr(1, 2), fr(44, 12), fr(10, 3), fr(2, 3))

# dictionary of a set of values

# Only the keys are taken in

# consideration by mean()

data5 = {1:"one", 2:"two", 3:"three"}

# Printing the mean of above datsets

print("Mean of data set 1 is % s" % (mean(data1)))

print("Mean of data set 2 is % s" % (mean(data2)))

print("Mean of data set 3 is % s" % (mean(data3)))

print("Mean of data set 4 is % s" % (mean(data4)))

print("Mean of data set 5 is % s" % (mean(data5)))

输出:

Mean of data set 1 is 5.857142857142857

Mean of data set 2 is -7.5

Mean of data set 3 is 2.4285714285714284

Mean of data set 4 is 49/24

Mean of data set 5 is 2

代码3:TypeError

# Python3 code to demonstrate TypeError

# importing statistics module

from statistics import mean

# While using dictionaries, only keys are

# taken into consideration by mean()

dic = {"one":1, "three":3, "seven":7,

"twenty":20, "nine":9, "six":6}

# Will raise TypeError

print(mean(dic))

输出:

Traceback (most recent call last):

File "/home/9f8a941703745a24ddce5b5f6f211e6f.py", line 29, in

print(mean(dic))

File "/usr/lib/python3.5/statistics.py", line 331, in mean

T, total, count = _sum(data)

File "/usr/lib/python3.5/statistics.py", line 161, in _sum

for n, d in map(_exact_ratio, values):

File "/usr/lib/python3.5/statistics.py", line 247, in _exact_ratio

raise TypeError(msg.format(type(x).__name__))

TypeError:can't convert type 'str' to numerator/denominator

应用范围:

平均值/算术平均值是非常重要的功能之一,同时可以处理统计数据和大数值。因此,借助mean()之类的功能,可以从大型数据集中提取趋势和特征值。

python中mean的用法_Python statistics mean()用法及代码示例相关推荐

  1. python中count的作用_python count函数用法详解

    在python中可以使用"count()"函数统计字符串里某个字符出现的次数,该函数用于统计次数,其语法是"count(sub, start= 0,end=len(str ...

  2. python中图例legend标签内容_matplotlib设置legend图例代码示例

    matplotlib设置legend图例代码示例 本文主要是关于matplotlib的一些基本用法. Demo import matplotlib.pyplot as plt import numpy ...

  3. python画折线图虚线_python绘制简单折线图代码示例

    1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...

  4. python中的get函数_python之函数用法get()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法get() #http://www.runoob.com/python/att-dic ...

  5. python中sinh是什么_Python PyTorch sinh()用法及代码示例

    PyTorch是由Facebook开发的开源机器学习库.它用于深度神经网络和自然语言处理. 功能torch.sinh()为PyTorch中的双曲正弦函数提供支持.它期望以弧度形式输入.输入类型为张量, ...

  6. python中argv的使用_python sys.argv[]用法

    sys.argv变量是一个字符串的列表.特别地,sys.argv包含了命令行参数 的列表,即使用命令行传递给你的程序的参数. 这里,当我们执行python using_sys.py we are ar ...

  7. python中ln怎么写_Python Decimal ln()用法及代码示例

    Decimal#ln():ln()是一个Decimal类方法,它返回Decimal值的自然(对数e)对数. 用法:Decimal.ln() 参数:十进制值 返回:十进制值的自然(以e为底)对数. 代码 ...

  8. python中tan怎么表示_Python numpy.tan()用法及代码示例

    numpy.tan(array [,out])= ufunc'tan'):此数学函数可帮助用户计算所有x(作为数组元素)的三角切线. 参数: array :[array_like]elements a ...

  9. python中res代表什么_Python数据类型的用法

    字符串的用法 res ='hellow,world'print(res) #res.显示的都是它的方法,下划线的除外 1 判断字符串的结尾字符,返回的值的布尔形式 endswith 判断字符串的开头字 ...

最新文章

  1. 理解并自定义HttpHandler
  2. python3.7下载安装教程-CentOS 7 下 安装 Python3.7
  3. 网络爬虫(urllib超详细使用指南)
  4. 【Python基础】Python正则表达式,从入门到实战,精华都在这里!
  5. 【Python基础】利用 Python 搞定精美网络图!
  6. 兄弟机cnc系统面板图解_FANUC软操作面板的应用介绍,真的太详细了
  7. java多态和泛型_Java面向对象(二) 接口、多态和泛型
  8. NSStringUIImage~NSData的相互转换以及中文转码
  9. 盘点旷视14篇CVPR 2019论文,都有哪些亮点?
  10. Linux系统下xampp集成环境安装
  11. WIN10安装VS2013出现兼容性问题解决
  12. 【开发工具集】显示设备驱动程序列表——DriverView
  13. Protues构建最小系统
  14. SAS安装后无法使用增强型编辑器问题解决方法
  15. 全志和瑞芯微比较_哪家强_华为海思/全志/瑞芯微终极PK 智能芯片哪家强?
  16. 白细胞直方图C语言,白细胞三分群及其直方图
  17. 实验十三:配置STP、RSTP以及负载均衡(生成树负载均衡)
  18. 五、NLP聊天语料处理
  19. 蚂蚁金服微贷事业群电话面试分享
  20. Android RSA加密解密的 工具类的使用

热门文章

  1. 更快地重复访问Java的Java类名?
  2. Java 10及更高版本的思考
  3. Drools可执行模型还活着
  4. CDI中的事务异常处理
  5. 线程同步,线程不同步_同步多线程集成测试
  6. 有效的Java –所有对象通用的方法
  7. 在您的构建过程中添加微基准测试
  8. 在没有复杂插件的情况下从Eclipse启动和调试Tomcat
  9. 使用WildFly 9和Jolokia监视DevOps样式
  10. 如何使用Java泛型映射不同的值类型