【统计学】Python计算并绘制样本数据的经验分布函数
参考文献:
一篇绘制累积经验分布函数图像的博客
如何得到样本数据的经验分布函数?
from statsmodels.distributions.empirical_distribution import ECDF
ecdf = ECDF([3,3,1,4]) # 返回了一个分布函数,我是说数学书上的函数
type(ecdf)
Out[19]: statsmodels.distributions.empirical_distribution.ECDF
ecdf(3) # 往这个分布函数中输入自变量,会得到其分布函数值
Out[20]: 0.75
ecdf(1)
Out[21]: 0.25
help(ECDF) # 看看介绍,我也没太懂。。。
Help on class ECDF in module statsmodels.distributions.empirical_distribution:
class ECDF(StepFunction)| ECDF(x, side='right')| | Return the Empirical CDF of an array as a step function.| | Parameters| ----------| x : array_like| Observations| side : {'left', 'right'}, optional| Default is 'right'. Defines the shape of the intervals constituting the| steps. 'right' correspond to [a, b) intervals and 'left' to (a, b].| | Returns| -------| Empirical CDF as a step function.| | Examples| --------| >>> import numpy as np| >>> from statsmodels.distributions.empirical_distribution import ECDF| >>>| >>> ecdf = ECDF([3, 3, 1, 4])| >>>| >>> ecdf([3, 55, 0.5, 1.5])| array([ 0.75, 1. , 0. , 0.25])| | Method resolution order:| ECDF| StepFunction| builtins.object| | Methods defined here:| | __init__(self, x, side='right')| Initialize self. See help(type(self)) for accurate signature.| | ----------------------------------------------------------------------| Methods inherited from StepFunction:| | __call__(self, time)| Call self as a function.| | ----------------------------------------------------------------------| Data descriptors inherited from StepFunction:| | __dict__| dictionary for instance variables (if defined)| | __weakref__| list of weak references to the object (if defined)
绘制样本的经验分布函数
方法一:
博客原文
# 这也是借鉴别人博客的代码,我再补充点自己的看法
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltdatas = np.array([64.3, 65.0, 65.0, 67.2, 67.3, 67.3, 67.3, 67.3, 68.0, 68.0, 68.8, 68.8, 68.8, 69.7,\69.7, 69.7, 70.3,70.4, 70.4, 70.4, 70.4, 70.4,70.4, 70.4, 71.2, 71.2, 71.2, 71.2,\72.0, 72.0, 72.0, 72.0, 72.0, 72.0, 72.0, 72.7, 72.7, 72.7, 72.7, 72.7, 72.7, 72.7,\73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5, 73.5,73.5, 73.5, 74.3, 74.3, 74.3,\74.3, 74.3, 74.3, 74.3, 74.3, 74.7, 75.0, 75.0, 75.0, 75.0, 75.0, 75.0, 75.0, 75.4,\75.6, 75.8, 75.8, 75.8, 75.8, 75.8, 76.5, 76.5, 76.5, 76.5, 76.5, 76.5, 76.5, 77.2,\77.2,77.6, 78.0, 78.8, 78.8, 78.8, 79.5, 79.5, 79.5, 80.3, 80.5, 80.5, 81.2, 81.6,\81.6, 84.3])#数据特征计算
s = np.std(datas, ddof=1)#样本标准差
xbar = np.mean(datas)#样本均值#数据可视化 画数据经验分布曲线图
nt, bins, patches = plt.hist(datas, bins=10, histtype='step', \
cumulative=True, density=True, color='darkcyan')#datas是数据,bins是分组数
plt.title('bins = 10')
plt.savefig('经验函数分布图1.jpg', dpi=200)
plt.show()#数据可视化 画数据经验分布曲线图
nt, bins, patches = plt.hist(datas, bins=15, histtype='step', \
cumulative=True, density=True, color='darkcyan')#datas是数据,bins是分组数
plt.title('bins = 15')#正态分布函数曲线拟合
y = (1 / (np.sqrt(2 * np.pi) * s)) * np.exp(-0.5 * ((bins - xbar) ** 2 / s ** 2))
y = y.cumsum()
y = y / y[-1]
plt.plot(bins, y, 'tomato', linewidth = 1.5, label = 'Theoretical')
plt.savefig('经验函数分布图2.jpg', dpi=200)
plt.show()# 这种方法有点奇怪,指定bins参数之后,就不能说绘制出来的经验分布函数是原样本的经验分布函数了
# 自己试试看,图太多了不方便一一往上贴,看看返回数组
plt.hist([3,3,1,4],histtype='step',cumulative=True,density=True)
Out[25]:
(array([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.75, 0.75, 0.75, 1. ]),array([1. , 1.3, 1.6, 1.9, 2.2, 2.5, 2.8, 3.1, 3.4, 3.7, 4. ]),[<matplotlib.patches.Polygon at 0x14fb356be20>])
plt.hist([3,3,1,4],histtype='step',cumulative=True) # 没有density参数表示计数,而非计算频率
Out[26]:
(array([1., 1., 1., 1., 1., 1., 3., 3., 3., 4.]),array([1. , 1.3, 1.6, 1.9, 2.2, 2.5, 2.8, 3.1, 3.4, 3.7, 4. ]),[<matplotlib.patches.Polygon at 0x14fb4c47d90>])plt.hist([3,3,1,4],histtype='step',cumulative=True,bins=4,density=True)
Out[28]:
(array([0.25, 0.25, 0.75, 1. ]),array([1. , 1.75, 2.5 , 3.25, 4. ]),[<matplotlib.patches.Polygon at 0x14fb6e5bb20>])
plt.hist([3,3,1,4],histtype='step',cumulative=True,bins=3,density=True)
Out[29]:
(array([0.25, 0.25, 1. ]),array([1., 2., 3., 4.]),[<matplotlib.patches.Polygon at 0x14fb4c31850>])
plt.hist([3,3,1,4],histtype='step',cumulative=True,bins=5,density=True)
Out[30]:
(array([0.25, 0.25, 0.25, 0.75, 1. ]),array([1. , 1.6, 2.2, 2.8, 3.4, 4. ]),[<matplotlib.patches.Polygon at 0x14fb4c37820>])# 也就是说,bins参数是平均分原数据的份数。由原数据的最大值和最小值以及bins参数共同决定各个子区间的范围。
方法二:
from statsmodels.distributions.empirical_distribution import ECDF
x = [3,3,1,4]
ecdf = ECDF([3,3,1,4])
type(ecdf)
Out[37]: statsmodels.distributions.empirical_distribution.ECDF
y = ecdf(x) # 计算分布函数值
x.sort()
x
Out[43]: [1, 3, 3, 4]
y.sort()
# 画阶梯函数之前一定要记得排序,不然就是乱七八糟的回字形
plt.step(x,y)
Out[45]: [<matplotlib.lines.Line2D at 0x14fc03b6850>]
这就是我想要的了
【统计学】Python计算并绘制样本数据的经验分布函数相关推荐
- 概率统计Python计算:样本数据的经验分布函数
设 ( x 1 , x 2 , ⋯ , x n ) (x_1,x_2,\cdots,x_n) (x1,x2,⋯,xn)是总体 X X X的一个样本观测值.与绘制直方图相仿,记 a = m i n ...
- 【数据挖掘】Python绘制一组样本的CDF概率分布函数(经验分布函数)
CDF是什么?简单对概率分布函数进行一个描述,在概率论中要研究一个随机变量ξ取值小于某一数值x的概率,这概率是x的函数,称这种函数为随机变量ξ的分布函数. F ( x ) = P ( X < x ...
- 使用Python,Opencv进行二维直方图的计算及绘制
使用Python,Opencv进行二维直方图的计算及绘制 1. 效果图 2. 源码 参考 这篇博客将介绍如何使用Python,Opencv进行二维直方图的计算及绘制(分别用Opencv和Numpy计算 ...
- python 曲线分析_大数据分析之Python计算KS值并绘制KS曲线
本篇教程探讨了大数据分析之Python计算KS值并绘制KS曲线,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入. python实现KS曲线,相关使用方法请参考上篇博客-R语言实现K ...
- R语言经验累积分布函数计算和绘制实战
R语言经验累积分布函数计算和绘制实战 目录 R语言经验累积分布函数计算和绘制实战 #基本语法
- Python快速编程入门#学习笔记02# |第十章 :Python计算生态与常用库(附.小猴子接香蕉、双人乒乓球小游戏源码)
全文目录 学习目标 1. Python计算生态概述 1.1 Python计算生态概述 2. Python生态库的构建与发布 2.1 模块的构建与使用 * 2.1.1第三方库/模块导入的格式 2.2 包 ...
- 【Python】数理统计理论:常用统计量、经验分布函数
一.常用统计量 数理统计学中常用的统计量有:样本均值.样本方差.顺序统计量.中位数.众数等. 其中可以反映总体位置特征的有数据的中心位置是均值(Mean),中位数(Median),众数(Mode).其 ...
- python基本图形绘制_【Python】Python基本图形绘制-Go语言中文社区
1.Python蟒蛇图形绘制: 代码: #PythonDraw.py import turtle turtle.setup(650, 350, 200, 200) turtle.penup() tur ...
- python turtle库画图案-Python基础图形绘制库——turtle
(1).介绍 turtle库也叫海龟库,是turtle绘图体系的Python实现.turtle库是Python语言的标准库之一,是入门级的图形绘制函数库. turtle绘图体系:也叫海龟绘图系统,它是 ...
最新文章
- 基于Numpy构建全连接前馈神经网络进行手写数字识别
- 软考程序员2009年下午试题是否有误?
- 使用cordova serve build以及tomcat进行web 调试。
- 卷积神经网络中不同类型的卷积方式介绍
- 细聊.NET6 ConfigurationManager的实现
- 倒数日电脑版_应用日报|iOS 或更名为 iPhoneOS,倒数日 Mac 版上线限时免费
- php 模板解析,关于模板的原理和解析
- 尚学堂java 参考答案 第八章
- c语言实训报告银行存款利息利税,银行存款利息和利税的调查总结.docx
- Eclipse中 Project facet jst.web.jstl has not been defined.解决方案
- 【数据科学】探索性数据分析
- Deploying Exchange 2010(三):在Windows Server 2008上安装Mailbox
- ios 的ASIHTTPRequest学习
- 到底啥是平台,到底啥是中台?李鬼太多,不得不说
- css 都有哪些字体,css字体有哪些
- 动手学深度学习pytorch入门
- JDK包括的Java基础类库_问:JDK是Java平台的核心,Java运行环境、Java工具、Java基础类库(rt.jar)。J...
- C3AE: Exploring the Limits of Compact Model for Age Estimation
- 【3小时学会C语言】横向对比/纵向剖析,轻松学习C语言
- 程序员遇到有远见的丈母娘