【问题标题】:How do I count data in each bin to get the PDF of a specific dataset?如何计算每个 bin 中的数据以获取特定数据集的 PDF?
【发布时间】:2021-12-28 20:45:02
【问题描述】:

我有一个介于 0 和 1 之间的 1024x1024 值数组,我想将它们分成大小为 0.05 的 bin(第一个 bin 以 0 为中心,第二个 bin 以 0.05 ecc 为中心...)来计算我的数据集的 PDF .我在计算每个 bin 中的数据数量时遇到问题。我试过使用 np.histogram 但我得到一个计数数组,其大小与我的实际 bin 数不同。

我也尝试使用 for 循环计算 PDF,但我认为我也没有使用正确的方法,如果有任何建议可能会有所帮助,我将不胜感激。

目前我的代码如下所示:

bins = np.arange(-0.025, 1.05, .05)
bin_width = bins[1]-bins[0]
num_bins = 22
counts, bin_edges = np.histogram(Flux, bins=bins, 
     range=(-0.025, bin_width*num_bins), density=False)
PDF = counts / (np.sum(counts)*Delta_F) #Delta_F = 0.05 is a normalisation factor 

非常感谢任何帮助,谢谢。 :)

【问题讨论】:

  • 欢迎来到Stack Overflow.。为了让我们为您提供帮助,请提供一个最小的可重现问题集,其中包括样本输入、预期输出、实际输出以及重现该示例所需的所有相关代码。你所提供的没有达到这个目标。请编辑您的问题以显示最小的可重现集。有关详细信息,请参阅Minimal Reproducible Example。请至少定义Fluxbind_edges

标签: python histogram probability bins


【解决方案1】:

TL;DR :您的分箱创建 22 个点,因此分箱有 21 个间隔,因此您应该获得 21 个计数值。 一切都如预期的那样


了解 PDF 的一种简单方法是使用参数density = True

rng = np.random.RandomState(10)  # deterministic random data
Flux = np.hstack((rng.normal(size=1024), rng.normal(size=1024)) )
np.histogram(Flux)

> (array([  6,  24, 110, 253, 410, 517, 406, 223,  70,  29]),
> array([-3.31766905, -2.70658557, -2.09550209, -1.48441861, -0.87333513,
        -0.26225165,  0.34883184,  0.95991532,  1.5709988 ,  2.18208228,
         2.79316576]))

使用您声明的可重现代码的 bin 如下所示:

rng = np.random.RandomState(10)  # deterministic random data
Flux = np.hstack((rng.normal(size=1024), rng.normal(size=1024)) )
bins = np.arange(-0.025, 1.05, .05)
bin_width = bins[1]-bins[0]
num_bins = 22
Delta_F = 0.05
counts, bin_edges = np.histogram(Flux, bins=bins, 
     range=(-0.025, bin_width*num_bins), density=False)
PDF = counts / (np.sum(counts)*Delta_F) #Delta_F = 0.05 is a normalisation factor
print(f"length PDF = {PDF.size}, \n PDF = {PDF} \n")
print(f"length bins = {bins.size}, \n bins = {bins}")

> length PDF = 21, 
> PDF = [0.85561497 1.25668449 0.90909091 1.25668449 1.25668449 1.22994652
 1.01604278 1.17647059 0.98930481 0.93582888 1.3368984  0.90909091
 0.93582888 0.82887701 0.82887701 0.80213904 0.80213904 0.72192513
 0.56149733 0.72192513 0.6684492 ] 

> length bins = 22, 
> bins = [-0.025  0.025  0.075  0.125  0.175  0.225  0.275  0.325  0.375  0.425
  0.475  0.525  0.575  0.625  0.675  0.725  0.775  0.825  0.875  0.925
  0.975  1.025]
```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2020-04-10
    • 1970-01-01
    • 2018-06-22
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多