在Python中生成n维随机数
我正在尝试从高斯分布生成随机数。 Python 有非常有用的 random.gauss() 方法,但这只是一维随机变量。我如何以编程方式从 n 维分布中生成随机数?
例如,在二维中,此方法的返回值本质上是与平均值的距离,因此我仍然需要 (x,y) 坐标来确定实际的数据点。我想我可以再生成两个随机数,但我不确定如何设置约束。
我很欣赏任何见解。谢谢!
I'm trying to generate random numbers from a gaussian distribution. Python has the very useful random.gauss()
method, but this is only a one-dimensional random variable. How could I programmatically generate random numbers from this distribution in n-dimensions?
For example, in two dimensions, the return value of this method is essentially distance from the mean, so I would still need (x,y) coordinates to determine an actual data point. I suppose I could generate two more random numbers, but I'm not sure how to set up the constraints.
I appreciate any insights. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Numpy 具有与 random 模块中的函数等效的多维函数
您正在寻找的函数是 numpy.random.normal
Numpy has multidimensional equivalents to the functions in the random module
The function you're looking for is numpy.random.normal
您可以使用
np.random.multivariate_normal()
函数来执行此操作。它不仅适用于二维数据,而且适用于任意数量的维度。
例如,如果您希望有 100 个以点 (1,3) 为中心的二维点,您可以执行以下操作。
对于以点 (1,10,100) 为中心的 100 个 3 维点,您可以执行此操作。
有关更多信息,请参阅此处的文档,但您也可以询问我。 http://docs.scipy.org/doc/numpy /reference/ generated/numpy.random.multivariate_normal.html
You can do this using the
np.random.multivariate_normal()
function.It works not only for 2-dimensional data but for any number of dimensions.
For example if you would like to have 100 2-dimensional points centered around the point (1,3) you can do the following.
And for 100 3-dimensional points centered around the point (1,10,100) you can do this.
For more info here is the documentation but you can also ask me. http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.multivariate_normal.html
您需要将多维分布正确分解为一维分布的组合。例如,如果您想要一个距给定中心的高斯分布距离的点以及围绕它的均匀分布的角度,您将获得具有高斯 rho 和均匀 theta(介于 0 和 2 之间)的 delta 的极坐标。 pi),那么,如果你想要笛卡尔坐标,你当然要进行坐标变换。
You need to properly decompose your multi-dimensional distribution into a composition of one-dimensional distributions. For example, if you want a point at a Gaussian-distributed distance from a given center and a uniformly-distributed angle around it, you'll get the polar coordinates for the delta with a Gaussian rho and a uniform theta (between 0 and 2 pi), then, if you want cartesian coordinates, you of course do a coordinate transformation.
听起来您正在要求多元正态分布。要从该分布生成一个值,您需要一个协方差矩阵来阐明 x 和 y 之间的关系。你的x和y有什么关系?如果 x 和 y 是独立的,则可以使用 random.gauss() 生成两个值。
如果您不确定协方差矩阵是什么,那么您需要先解决一个数学问题,然后才能解决软件问题。如果您提供有关您尝试建模的内容的更多信息,我们也许能够提供帮助(我看到 Alex Martelli 刚刚发布了一些常见模型的解决方案)。
It sounds like you are asking for a Multivariate Normal Distribution. To generate a value from that distribution, you need to have a covariance matrix that spells out the relationship between x and y. How are your x and y related? If x and y are independent, you can just generate two values with random.gauss().
If you're not sure what your covariance matrix is, then you have a math problem that you need to solve before you can work on the software problem. If you provide more information about what you're trying to model, we might be able to help (and I see that Alex Martelli just posted some solutions for common models).