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

时间:2018-09-26 19:12:30

标签: python matplotlib cdf

我需要绘制从文件读取的整数值的cdf。我正在关注示例here。我不确定如何将pdf数据标准化然后计算cdf。

import numpy as np
from 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 个答案:

答案 0 :(得分:1)

我可以提出一个答案,在这里您可以使用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。