【问题标题】:Was trying to validate the Central Limit Theorem, the plot generated is a bit confusing试图验证中心极限定理,生成的图有点混乱
【发布时间】:2019-07-05 23:03:03
【问题描述】:

Periodic gaps in the plot

正在执行中心极限定理的证明

X = [0]*10000000
i = 100
for j in range(i):
    a = (np.random.randint(0, high = 4, size = 10000000 ))
    X = X+a
plt.hist(X, bins='auto')
plt.show()
# print (Counter(X))

【问题讨论】:

  • 看起来像一个绘图神器。打开绘图窗口,抓住窗口的边缘并将其放大。当您改变窗口大小时,您会看到细垂直线出现和消失。
  • 我想到了这一点,但由于间隙看起来是周期性的,我认为这是由于代码造成的。
  • 这是hist 情节中的一个小故障。 matplotlib 在光栅化细垂直线方面并不总是做得很好。

标签: python python-3.x numpy multidimensional-array


【解决方案1】:

它是hist 命令的绘图神器。在绘图窗口打开的情况下,更改窗口的大小。您会看到细竖条出现又消失。

如果我将hist 命令更改为

plt.hist(X, bins=np.arange(X.min()-1, X.max()+2)

我明白了

【讨论】:

    【解决方案2】:

    plt.hist 的自动分箱与 randint 交互导致此工件。使用np.random.random 来克服这个问题。

    import numpy as np
    import matplotlib.pyplot as plt
    
    X = np.array([0]*10000000)
    i = 100
    for j in range(i):
        a = np.random.random(10000000) * 4 # multiplying by 4 to simulate high=4 in OP's code
        X = X+a
    plt.hist(X, bins='auto')
    plt.show()
    

    更新:正如@Warren 提到的,问题在于plt.hist 的自动分箱行为。通过统一选择数字而不是整数,这个问题就消失了。如果您必须使用 randint,他的解决方案有效。

    【讨论】:

    • 这段代码确实很好地展示了中心极限定理,但是使用整数而不是统一浮点数并不是问题的真正原因。
    猜你喜欢
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2012-02-21
    • 2014-02-15
    • 2015-05-09
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多