numpy histogram cumulative density does not sum to 1

后端 未结 2 1871
误落风尘
误落风尘 2020-12-05 11:44

Taking a tip from another thread (@EnricoGiampieri\'s answer to cumulative distribution plots python), I wrote:

# plot cumulative density function of nearest         


        
2条回答
  •  执念已碎
    2020-12-05 12:01

    You need to make sure your bins are all width 1. That is:

    np.all(np.diff(base)==1)
    

    To achieve this, you have to manually specify your bins:

    bins = np.arange(np.floor(nearest.min()),np.ceil(nearest.max()))
    values, base = np.histogram(nearest, bins=bins, density=1)
    

    And you get:

    In [18]: np.all(np.diff(base)==1)
    Out[18]: True
    
    In [19]: np.sum(values)
    Out[19]: 0.99999999999999989
    

提交回复
热议问题