Numpy: Add mass keyword to np.hist()

Created on 25 Oct 2017  路  3Comments  路  Source: numpy/numpy

A lot of people want to calculate the probability mass function using numpy.hist and are confused that it cannot do that.
It can however calculate the probability density function and https://github.com/numpy/numpy/issues/1043 did a great job by renaming normed to density.
Analogous to that, I propose to add a mass keyword that (if True) returns the probability mass function.

I would also implement this function, once I got positive feedback.

Most helpful comment

Considering how frequently normalizations are used in histograms, it would make sense to me to allow for the functionality from simply a convenience point of view. Sure an extra line isn't a big deal once, but it adds up and the current implementation can be frustrating when looking for norm and realizing the parameter does not do what you need it to. Personally, I'm in favor of a norm_type parameter rather than adding a new one.

It should be noted though, that Matplotlib accepts the return structure of np.histogram directly as inputs to plot/bar/etc. Things become a little more troubling when working with Pandas DataFrames, since you have to manually slice them, run through np.histogram, then plot, but it's also doable.

Bottom line, implementing a count/sum/pmf normalization into np.histogram is trivial (one argument against it), but would provide convenience in modules building upon it.

All 3 comments

We need to discuss the fact that normalization and binning are two independent factors of a histogram. PDF and PMF are impropper categories, as you can have binned continuous variables that are normalized by the area under the curve (PDF), those that aren't normalized at all (count), those that are normalized by the sum of the bin values (not really a PMF since it's binned), those that are normalized by the sum of the bin values for non continuous variables (Chevy, Volvo, Ford, etc. PMF), and probably others. Some tools apparently use a string input as the selection.

Ive been arguing with matplotlib about this. I don't think it's numpy's job to do this because normalization is easy to do mathematically post-hoc. It's just the graphing that's hard. It's their wrapper that makes it difficult to do intermediate steps on the process like normalization. But I digress.

Considering how frequently normalizations are used in histograms, it would make sense to me to allow for the functionality from simply a convenience point of view. Sure an extra line isn't a big deal once, but it adds up and the current implementation can be frustrating when looking for norm and realizing the parameter does not do what you need it to. Personally, I'm in favor of a norm_type parameter rather than adding a new one.

It should be noted though, that Matplotlib accepts the return structure of np.histogram directly as inputs to plot/bar/etc. Things become a little more troubling when working with Pandas DataFrames, since you have to manually slice them, run through np.histogram, then plot, but it's also doable.

Bottom line, implementing a count/sum/pmf normalization into np.histogram is trivial (one argument against it), but would provide convenience in modules building upon it.

There's a lot of discussion at matplotlib/matplotlib#10398 about adding such a keyword that might be worth reviewing

Was this page helpful?
0 / 5 - 0 ratings