简体   繁体   English

np.random.choice 在直方图中有间隙

[英]np.random.choice has a gap in histogram

I am using np.random.choice to construct a histogram of the sum of throwing 2 evenly weighted dice.我正在使用 np.random.choice 来构建投掷 2 个均匀加权骰子之和的直方图。 However, when I run the code, the values for 7, which should have the most returns, are missing.但是,当我运行代码时,应该有最多回报的 7 的值丢失了。

import numpy as np
import matplotlib.pyplot as plt

values = [1, 2, 3, 4, 5, 6]
z = 1/6
x = np.random.choice(values, 1000000, p=[z, z, z, z, z, z])
y = np.random.choice(values, 1000000, p=[z, z, z, z, z, z])

plt.hist(x + y, 12, color="green", edgecolor='black', linewidth=1.2, label="Uniform Dist", rwidth=.75)
plt.show()

np.random.choice:2个骰子的总和

Any suggestions on what is going wrong?关于出了什么问题的任何建议?

There are no values inthe interval 6.167 <= x < 7.0 .区间6.167 <= x < 7.0中没有值。 The numbers 7 are included in the interval 7.0 <= x < 7.833 .数字7包含在区间7.0 <= x < 7.833中。

I'd recommend to plot a bar plot of the discrete frequencies.我建议 plot 一个酒吧 plot 的离散频率。

plt.bar(*np.unique(x+y, return_counts=True))

在此处输入图像描述

The problem is that plt.hist binning algorithm is suitable for real values, not for integer (discrete) values.问题是plt.hist分箱算法适用于实际值,而不适用于integer (离散)值。

Explanation解释

Let's see bins proposed by matplotlib:让我们看看 matplotlib 提出的 bin:

n, bins, _ = plt.hist(x + y, bb, color="green", edgecolor='black', linewidth=1.2, label="Uniform Dist", rwidth=.75)

The bins is: bins是:

array([ 2.        ,  2.83333333,  3.66666667,  4.5       ,  5.33333333,
        6.16666667,  7.        ,  7.83333333,  8.66666667,  9.5       ,
       10.33333333, 11.16666667, 12.        ])

Sixth bar has range [bins[5], bins[6]) equal to [6.17, 7.00) - notice, that it is half-open.第六个柱的范围[bins[5], bins[6])等于[6.17, 7.00) - 注意,它是半开的。 Therefore no integer belongs to this range.因此没有integer 属于这个范围。

Solution解决方案

The solution is to manually set bins:解决方案是手动设置垃圾箱:

values = x + y
bins = np.arange(np.min(values) - .5, np.max(values) + 0.5, 1)

plt.hist(values, bins, color="green", edgecolor='black', linewidth=1.2, label="Uniform Dist", rwidth=.75)

bins is equal to array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, ..., 10.5, 11.5, 12.5]) . bins等于array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, ..., 10.5, 11.5, 12.5])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM