How to output a dataset when using a histogram in math

In Mma, the Histogram function only generates graphics. I am wondering how I can get a dataset; is there any convenient built-in function for this?

Many thanks.

+2
source share
3 answers

A HistogramList has been added to Mathematica V8 to expose binning and height calculations.

HistogramList[a]

enter image description here

For V7, you can crack the third argument to get bins and counts.

Histogram[a, Automatic, (Print[{##}]; #2) &]

enter image description here

+12
source

Internet help may also help. This was my answer (June 18, 2010) to a similar question in the Mathematica newsgroup comp.soft-sys.math.mathematica:

data = RandomReal[NormalDistribution[0, 1], 200];
res = Reap[Histogram[data, Automatic, (Sow[{#1, #2}]; #2) &]]

enter image description here

, , Brett, .


, , . :

bins = Union[ Flatten[res[[2, 1, 1, 1]]]];
counts = res[[2, 1, 1, 2]];
Histogram[data, {bins}, counts &]

, , Union ( ), DeleteDuplicates.

count & - . , , . .

+3

If you use V7 and you are upset that with this trick you cannot use the built-in bunker height specification ("Count", "Probability", "ProbabilityDensity", etc.), you can change the Sjord answers above to return the bins, normalized, but you want to. For example, if you want bit heights to be used with

data = RandomReal[NormalDistribution[0, 1], 200];
Histogram[data, Automatic, "Probability"]

you can use instead

res = Reap[Histogram[data, Automatic, 
           (ret = #2/Length[data]; 
            Sow[{#1, ret}]; ret) &
      ]]

The counterpart to "ProbabilityDensity" is

res = Reap[Histogram[data, Automatic, 
           (binWidth = #1[[1]][[2]] - #1[[1]][[1]];
           ret = #2/(Length[data]*binWidth);
           Sow[{#1, ret}]; ret) &
     ]]
+2
source

All Articles