为了账号安全,请及时绑定邮箱和手机立即绑定

通过从文件中读取值来绘制 CDF:matplotlib

通过从文件中读取值来绘制 CDF:matplotlib

GCT1015 2021-06-24 10:21:10
我需要绘制从文件中读取的整数值的 cdf。我正在按照此处的示例进行操作。我不确定如何规范化 pdf 的数据,然后计算 cdf。import numpy as npfrom pylab import *with open ("D:/input_file.txt", "r+") as f:    data = f.readlines()    X = [int(line.strip()) for line in data]    Y  = exp([-x**2 for x in X])  # is this correct?     # Normalize the data to a proper PDF    Y /= ... # not sure what to write here    # Compute the CDF    CY = ... # not sure what to write here    # Plot both    plot(X,Y)    plot(X,CY,'r--')    show()
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

我可以提出一个答案,您可以在其中使用 NumPy 确定概率密度函数 (PDF) 和累积分布函数 (CDF)。


import numpy as np

# -----------------

data = [88,93,184,91,107,170,88,107,167,90];

# -----------------

# get PDF:

ydata,xdata = np.histogram(data,bins=np.size(data),normed=True);

# ----------------

# get CDF:

cdf = np.cumsum(ydata*np.diff(xdata));

# -----------------

print 'Sum:',np.sum(ydata*np.diff(xdata))

我正在使用 Numpy 方法直方图,它会给我 PDF,然后我将从 PDF 计算 CDF。


查看完整回答
反对 回复 2021-06-29
  • 1 回答
  • 0 关注
  • 135 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信