how to output the data set when using histogram in mathematica

百般思念 提交于 2019-12-10 10:32:20

问题


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

Many thanks.


回答1:


HistogramList was added to Mathematica V8 to expose the binning and height calculations.

HistogramList[a]

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

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




回答2:


Perhaps a bit of internet searching would help as well. This was my answer (of 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) &]]

I feel this solution is slightly better than Brett's because it returns the data in a readily usable format.

EDIT
To recreate the histogram following any bin or count manipulation you'd proceed as suggested by Brett below. Just have the bin and count ready as follows:

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

I'm not sure whether the bins are guaranteed to be in ascending order, so instead of Union (which sorts), you might want to use DeleteDuplicates.

The count & is a trick here. As explained in the help page, a function in the third position is expected to take a bin and count list and returns a height list. In this case it just gobbles up the lists and returns counts.




回答3:


If you're using V7 and you're upset that by using this trick you can't use a built-in bin height specification ("Count", "Probability", "ProbabilityDensity", etc), you can modify Sjord's answer above to return bins normalized however you want. For example, if you want bin heights using

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

you could instead use

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

The analog for "ProbabilityDensity" is

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


来源:https://stackoverflow.com/questions/5239205/how-to-output-the-data-set-when-using-histogram-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!