【发布时间】:2016-01-14 14:38:00
【问题描述】:
用numpy 生成一组正态分布整数的最佳方法是什么?我知道我可以用这样的东西得到花车:
In [31]: import numpy as np
In [32]: import matplotlib.pyplot as plt
In [33]: plt.hist(np.random.normal(250, 1, 100))
Out[33]:
(array([ 2., 5., 9., 10., 19., 21., 13., 10., 6., 5.]),
array([ 247.52972483, 247.9913017 , 248.45287858, 248.91445546,
249.37603233, 249.83760921, 250.29918608, 250.76076296,
251.22233984, 251.68391671, 252.14549359]),
<a list of 10 Patch objects>)
【问题讨论】:
-
除非你真的需要高精度,否则我只会对浮点数进行四舍五入。
-
好的,就像这样:
np.random.normal(250, 1, 100).round(0)? -
如果您需要实际整数:
np.random.normal(250, 1, 100).round().astype(np.int)。 (0 是np.round的默认值,顺便说一句。) -
你可以在浮点数之上绘制整数分布,看看你是否满意。
-
@Evert 在这种情况下,“高精度”意味着什么?根据定义,正态分布是连续的。
标签: python numpy statistics scipy