有些统计学术语把握不是特别准确,担心有翻译错的,所以在不确定的地方保留了英文原文,如果有翻译错的也请路过的同行指出,多谢!
1、mean()
计算平均值
>>> import statistics
>>> statistics.mean([1, 2, 3, 4, 5, 6, 7, 8, 9])
5.0
>>> statistics.mean(range(1,10))
5.0
>>> import fractions
>>> x = [(3, 7), (1, 21), (5, 3), (1, 3)]
>>> y = [fractions.Fraction(*item) for item in x]
>>> y
[Fraction(3, 7), Fraction(1, 21), Fraction(5, 3), Fraction(1, 3)]
>>> statistics.mean(y)
Fraction(13, 21)
>>> import decimal
>>> x = ('0.5', '0.75', '0.625', '0.375')
>>> y = map(decimal.Decimal, x)
>>> y
<map object at 0x00000000033465C0>
>>> list(y)
[Decimal('0.5'), Decimal('0.75'), Decimal('0.625'), Decimal('0.375')]
>>> statistics.mean(y)
Traceback (most recent call last):
File "<pyshell#411>", line 1, in <module>
statistics.mean(y)
File "C:\Python 3.5\lib\statistics.py", line 292, in mean
raise StatisticsError('mean requires at least one data point')
statistics.StatisticsError: mean requires at least one data point
>>> list(y)
[]
>>> y = map(decimal.Decimal, x)
>>> statistics.mean(y)
Decimal('0.5625')
2、median()、median_low()、median_high()、median_grouped()
各种中位数
>>> statistics.median([1, 3, 5, 7])
4.0
>>> statistics.median_low([1, 3, 5, 7])
3
>>> statistics.median_high([1, 3, 5, 7])
5
>>> statistics.median([1, 3, 7])
3
>>> statistics.median([5, 3, 7])
5
>>> statistics.median(range(1,10))
5
>>> statistics.median_low([5, 3, 7])
5
>>> statistics.median_high([5, 3, 7])
5
>>> statistics.median_grouped([5, 3, 7])
5.0
>>> statistics.median_grouped([5, 3, 7, 1])
4.5
>>> statistics.median_grouped([52, 52, 53, 54])
52.5
>>> statistics.median_low([52, 52, 53, 54])
52
>>> statistics.median_high([52, 52, 53, 54])
53
>>> statistics.median_high([1, 3, 3, 5, 7])
3
>>> statistics.median_low([1, 3, 3, 5, 7])
3
>>> statistics.median_grouped([1, 3, 3, 5, 7])
3.25
>>> statistics.median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])
3.7
>>> statistics.median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5], interval=2)
3.4
3、mode()
返回最常见数据或出现次数最多的数据(most common data)
>>> statistics.mode([1, 3, 5, 7])
Traceback (most recent call last):
File "<pyshell#435>", line 1, in <module>
statistics.mode([1, 3, 5, 7])
File "C:\Python 3.5\lib\statistics.py", line 434, in mode
'no unique mode; found %d equally common values' % len(table)
statistics.StatisticsError: no unique mode; found 4 equally common values
>>> statistics.mode([1, 3, 5, 7, 3])
3
>>> statistics.mode([1, 3, 5, 7, 3, 5])
Traceback (most recent call last):
File "<pyshell#437>", line 1, in <module>
statistics.mode([1, 3, 5, 7, 3, 5])
File "C:\Python 3.5\lib\statistics.py", line 434, in mode
'no unique mode; found %d equally common values' % len(table)
statistics.StatisticsError: no unique mode; found 2 equally common values
>>> statistics.mode([1, 3, 5, 7, 3, 5, 5])
5
>>> statistics.mode(["red", "blue", "blue", "red", "green", "red", "red"])
'red'
>>> statistics.mode(list(range(5)) + [3])
3
4、pstdev()
返回总体标准差(population standard deviation ,the square root of the population variance)。
>>> statistics.pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
0.986893273527251
>>> statistics.pstdev(range(20))
5.766281297335398
>>> statistics.pstdev([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])
2.8722813232690143
5、pvariance()
返回总体方差(population variance)或二次矩(second moment)。
>>> statistics.pvariance([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
0.9739583333333334
>>> statistics.pvariance([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])
8.25
>>> x = [1, 2, 3, 4, 5, 10, 9, 8, 7, 6]
>>> mu = statistics.mean(x)
>>> mu
5.5
>>> statistics.pvariance([1, 2, 3, 4, 5, 10, 9, 8, 7, 6], mu)
8.25
>>> statistics.pvariance(range(20))
33.25
>>> statistics.pvariance((random.randint(1,10000) for i in range(30)))
10903549.933333334
6、variance()、stdev()
计算样本方差(sample variance)和样本标准差(sample standard deviation,the square root of the sample variance,也叫均方差)。
>>> statistics.variance((random.randint(1,10000) for i in range(30)))
10229013.655172413
>>> statistics.stdev((random.randint(1,10000) for i in range(30)))
3106.2902337180203
>>> _ * _ #注意,上面的两个样本数据并不一样,因为都是随机数
9649039.016091954
>>> statistics.variance(range(20))
35.0
>>> statistics.stdev(range(20))
5.916079783099616
>>> _ * _
35.0
>>> statistics.variance([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])
9.166666666666666
>>> statistics.stdev([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])
3.0276503540974917
>>> statistics.variance([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
1.16875
>>> statistics.stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
1.0810874155219827
>>> _ * _
1.1687500000000002
>>> statistics.variance([3, 3, 3, 3, 3, 3])
0.0
>>> statistics.stdev([3, 3, 3, 3, 3, 3])

0.0

原文地址:http://user.qzone.qq.com/306467355/blog/1446598412

更多精彩内容请访问作者QQ空间。

Python统计模块statistics用法精要相关推荐

  1. 四十一、Python统计模块statistics

    @Author:Runsen 文章目录 Python统计模块statistics median-中位数 方差 官网: https://docs.python.org/3/library/statist ...

  2. 微课|玩转Python轻松过二级(2.4节):常用内置函数用法精要2

    适用教材: 董付国.<玩转Python轻松过二级>.清华大学出版社,2018. 第2章  运算符.表达式与内置对象 2.4  Python常用内置函数用法精要 2.4.4  排序与逆序 2 ...

  3. 微课|玩转Python轻松过二级(2.4节):常用内置函数用法精要4

    适用教材: 董付国.<玩转Python轻松过二级>.清华大学出版社,2018. 第2章  运算符.表达式与内置对象 2.4  Python常用内置函数用法精要 2.4.7  range() ...

  4. 微课|玩转Python轻松过二级(2.4节):常用内置函数用法精要3

    适用教材: 董付国.<玩转Python轻松过二级>.清华大学出版社,2018. 第2章  运算符.表达式与内置对象 2.4  Python常用内置函数用法精要 2.4.6  map().r ...

  5. 微课|玩转Python轻松过二级(2.4节):常用内置函数用法精要1

    适用教材: 董付国.<玩转Python轻松过二级>.清华大学出版社,2018. 第2章  运算符.表达式与内置对象 2.4  Python常用内置函数用法精要 2.4.1  类型转换与类型 ...

  6. python中的statistics_Python统计分析模块statistics用法示例

    本文实例讲述了Python统计分析模块statistics用法.分享给大家供大家参考,具体如下: 一 计算平均数函数mean() >>>import statistics >& ...

  7. python:statistics --- 数学统计函数

    python:statistics --- 数学统计函数 平均值以及对中心位置的评估 对分散程度的评估 对两个输入之间关系的统计 函数细节 异常 NormalDist 对象 NormalDist 示例 ...

  8. 简单介绍Python中异常处理用法

    这篇文章主要给大家分享的是 Python中异常处理用法,为了保证程序的健壮性与容错性,即在遇到错误时候程序不会崩溃,我们需要对异常进行处理,下面来看看文章对此的用法,需要的朋友可以参考一下 为了保证程 ...

  9. python的继承用法_【后端开发】python中继承有什么用法?python继承的用法详解

    本篇文章给大家带来的内容是关于python中继承有什么用法?python继承的用法详解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 面向对象三大特征 1.封装:根据职责将属性和方法 ...

最新文章

  1. Asp.net调用RAR压缩 解压文件
  2. java EF6,EF Core 2.0和EF6(Entity Framework 6)中配置实体映射关系
  3. django新建php文件,在Python的Django框架中创建语言文件
  4. [转] React风格的企业前端技术
  5. 博客园在我的博客添加点击小心心特效
  6. HTML第6章简答题3(制作北大青鸟网站的中心开班信息模板)
  7. 高数习题第八章总练习题(上)
  8. 通过WIFI,访问一个网站
  9. APM 页面加载耗时校准
  10. Hibernate框架检索策略
  11. Grid++Report报表开发工具介绍
  12. 【JavaScript】多行文本省略,并且判断是否需要省略。
  13. 机器学习训练营--快来一起挖掘幸福感吧
  14. python3读取excel汉字_从excel文件python3读取汉字
  15. python:max函数
  16. ​汽车芯片的可靠性设计:控制亚稳态,提升稳定性
  17. git提交代码时出现Everything up-to-date的解决办法
  18. 景联文科技:深度了解语音识别之发音词典及语音数据采集标注
  19. 互联网快讯:武大研发核酸检测新方法;多款「隔离险」仓皇下架;刘传健出任中国民航飞行学院总飞行师
  20. vivoS7e和vivoS6哪个好(参数对比还是新机优势大)

热门文章

  1. mysql中触发器中分隔符_php – 在触发器中使用分隔符的Mysql错误
  2. hibernate mysql 视图_转:hibernate映射视图的两种方式
  3. html代码重排,是否有可能在html中格式化/重排html?
  4. java获取数据库当前时间_java中获取系统的当前时间
  5. 深度linux添加xp,Linux和Windos XP下向路由表添加路由
  6. 二维数组 赋值_数组,及二维数组
  7. java多线程详解 六_java多线程学习-java.util.concurrent详解(六) Exchanger
  8. 自定义Hibernate Validator规则注解
  9. Spring的注入方式详解
  10. mysql导出表数据