我有一个名为d
的无序列表,看起来像:
[0.0000, 123.9877,0.0000,9870.9876, ...]
我只想使用Python中的Matplotlib基于这个列表绘制一个cdf图。但不知道我能不能用
d = []
d_sorted = []
for line in fd.readlines():
(addr, videoid, userag, usertp, timeinterval) = line.split()
d.append(float(timeinterval))
d_sorted = sorted(d)
class discrete_cdf:
def __init__(data):
self._data = data # must be sorted
self._data_len = float(len(data))
def __call__(point):
return (len(self._data[:bisect_left(self._data, point)]) /
self._data_len)
cdf = discrete_cdf(d_sorted)
xvalues = range(0, max(d_sorted))
yvalues = [cdf(point) for point in xvalues]
plt.plot(xvalues, yvalues)
现在我正在使用此代码,但错误消息是:
Traceback (most recent call last):
File "hitratioparea_0117.py", line 43, in <module>
cdf = discrete_cdf(d_sorted)
TypeError: __init__() takes exactly 1 argument (2 given)
我知道我去晚会迟到了。但是,如果您只想让cdf用于绘图而不用于将来的计算,则有一种更简单的方法:
例如,生成one of these graphs的相关代码是:
从matplotlib文档中编辑:This example可能更有帮助。
计算累积和
cumsum
的numpy函数在这里很有用如前所述,来自
numpy
的cumsum
工作良好。确保您的数据是正确的PDF(即总和为1),否则CDF不会以unityas it should结尾。下面是一个最小的工作示例:相关问题 更多 >
编程相关推荐