Package Scientific :: Package Statistics :: Module Histogram :: Class Histogram
[frames] | no frames]

Class Histogram

Known Subclasses:

Histogram in one variable

The bin index and the number of points in a bin can be obtained by indexing the histogram with the bin number. Application of len() yields the number of bins. A histogram thus behaves like a sequence of bin index - bin count pairs.

Here is an example on usage:

>>> nsamples = 1000
>>> from numpy import random
>>> data = random.normal(1.0, 0.5, nsamples)
>>> import Scientific.Statistics as S
>>> S.mean(data)
0.9607056871982641
>>> S.standardDeviation(data)
0.50251811830486681
>>> S.median(data)
0.94853870756924152
>>> S.skewness(data)   # should be 0
0.038940041870334556
>>> S.kurtosis(data)   # should be 3
2.865582791273765
>>> 
>>> from Scientific.Statistics.Histogram import Histogram
>>> h = Histogram(data, 50)  # use 50 bins between min & max samples
>>> h.normalizeArea()        # make probabilities in histogram
>>> h[3]                     # bin index and frequency in the 4th bin
array([-0.45791018,  0.01553658])
>>> x = h.getBinIndices()
>>> y = h.getBinCounts()
>>> # can plot the y vector against the x vector (see below)
>>> 
>>> # add many more samples:
>>> nsamples2 = nsamples*100
>>> data = random.normal(1.0, 0.5, nsamples2)
>>> h.addData(data)
>>> h.normalizeArea()
>>> x2 = h.getBinIndices()
>>> y2 = h.getBinCounts()
>>> plot (x,y) and (x2,y2):
>>> import Gnuplot
>>> g = Gnuplot.Gnuplot(persist=1)
>>> g.xlabel('sample value');  g.ylabel('probability')
>>> d1 = Gnuplot.Data(x, y, with='lines',
...                   title='%d samples' % nsamples)
>>> d2 = Gnuplot.Data(x2, y2, with='lines',
...                   title='%d samples' % nsamples2)
>>> g.plot(d1,d2)
Instance Methods
 
__getitem__(self, index)
Returns: an array of shape (2,) containing the bin value and the bin count
 
__getslice__(self, first, last)
 
__init__(self, data, nbins, range=None)
int
__len__(self)
Returns: the number of bins
 
addData(self, data)
Add values to the originally supplied data sequence.
 
getBinCounts(self)
Return an array of all the bin counts.
 
getBinIndices(self)
Return an array of all the bin indices.
 
normalize(self, norm=1.0)
Scale all bin counts by the same factor
 
normalizeArea(self, norm=1.0)
Scale all bin counts by the same factor
Method Details

__getitem__(self, index)
(Indexing operator)

 
Parameters:
  • index (int) - a bin index
Returns:
an array of shape (2,) containing the bin value and the bin count

__init__(self, data, nbins, range=None)
(Constructor)

 
Parameters:
  • data (Numeric.array of float or int) - a sequence of data points
  • nbins (int) - the number of bins into which the data is to be sorted
  • range (tuple or NoneType) - a tuple of two values, specifying the lower and the upper end of the interval spanned by the bins. Any data point outside this interval will be ignored. If no range is given, the smallest and largest data values are used to define the interval.

__len__(self)
(Length operator)

 
Returns: int
the number of bins

addData(self, data)

 

Add values to the originally supplied data sequence. Use this method to feed long data sequences in multiple parts to avoid memory shortages.

Parameters:
  • data (Numeric.array) - a sequence of data points

Note: this does not affect the default range of the histogram, which is fixed when the histogram is created.

normalize(self, norm=1.0)

 

Scale all bin counts by the same factor

Parameters:
  • norm (float or int) - the sum of all bin counts after the rescaling

normalizeArea(self, norm=1.0)

 

Scale all bin counts by the same factor

Parameters:
  • norm (float or int) - the area under the histogram after the rescaling