pylab.hist(data, normed=1). Normalization seems to work incorrect

后端 未结 7 1285
感情败类
感情败类 2020-12-01 07:40

I\'m trying to create a histogram with argument normed=1

For instance:

import pylab

data = ([1,1,2,3,3,3,3,3,4,5.1])    
pylab.hist(data, normed=1)
         


        
7条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 08:09

    According to documentation normed: If True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability mass function. This is from numpy doc, but should be the same for pylab.

    In []: data= array([1,1,2,3,3,3,3,3,4,5.1])
    In []: counts, bins= histogram(data, normed= True)
    In []: counts
    Out[]: array([ 0.488,  0.,  0.244,  0.,  1.22,  0.,  0.,  0.244,  0.,  0.244])
    In []: sum(counts* diff(bins))
    Out[]: 0.99999999999999989
    

    So simply normalization is done according to the documentation like:

    In []: counts, bins= histogram(data, normed= False)
    In []: counts
    Out[]: array([2, 0, 1, 0, 5, 0, 0, 1, 0, 1])
    In []: counts_n= counts/ sum(counts* diff(bins))
    In []: counts_n
    Out[]: array([ 0.488,  0.,  0.244,  0.,  1.22 ,  0.,  0.,  0.244,  0.,  0.244])
    

提交回复
热议问题