Python numpy.random.normal只有正值

时间:2013-05-01 02:40:43

标签: python numpy normal-distribution

我想用numpy.random.normal创建一个只包含正值的普通分布式数组。 例如,以下说明它有时会给出负值,有时会给出正值。如何修改它以便只返回正值?

>>> import numpy
>>> numpy.random.normal(10,8,3)
array([ -4.98781629,  20.12995344,   4.7284051 ])
>>> numpy.random.normal(10,8,3)
array([ 17.71918829,  15.97617052,   1.2328115 ])
>>> 

我想我可以这样解决它:

myList = numpy.random.normal(10,8,3)

while item in myList <0:
       # run again until all items are positive values
       myList = numpy.random.normal(10,8,3)

8 个答案:

答案 0 :(得分:6)

根据定义,正态分布从-inf扩展到+ inf,因此您要求的内容在数学上没有意义。

您可以采用正态分布并将绝对值“剪切”为正值,或者只是丢弃负值,但您应该理解它将不再是正态分布。

答案 1 :(得分:1)

您可以将整个数组偏移到数组的最低值(最左侧)。你得到的可能不是真正的正态分布&#34;,但在你的工作范围内,处理有限数组,你可以确保这些值是正的并且适合在钟形曲线下。

>>> mu,sigma = (0,1.0)
>>> s = np.random.normal(mu, 1.0, 100)
>>> s
array([-0.58017653,  0.50991809, -1.13431539, -2.34436721, -1.20175652,
        0.56225648,  0.66032708, -0.98493441,  2.72538462, -1.28928887])
>>> np.min(s)
-2.3443672118476226
>>> abs(np.min(s))
2.3443672118476226
>>> np.add(s,abs(np.min(s)))
array([ 1.76419069,  2.85428531,  1.21005182,  0.        ,  1.14261069,
        2.90662369,  3.00469429,  1.3594328 ,  5.06975183,  1.05507835])

答案 2 :(得分:1)

我认为你的意思是你要修改概率密度,使其在正范围内与正常形状相同,在负数范围内为零。这是一个非常常见的实际案例。在这种情况下,您不能简单地采用生成的正态随机变量的绝对值。相反,你必须生成一个新的独立的正态分布数,直到你得出一个正数。一种方法是递归地,见下文。

import numpy as np def PosNormal(mean, sigma): x = np.random.normal(xbar,delta_xbar,1) return(x if x>=0 else PosNormal(mean,sigma))

答案 3 :(得分:0)

如何在这些方面使用lognormal:

    mu = np.mean(np.log(list))
    sigma = np.std(np.log(list))

    new_list = np.random.lognormal(mu, sigma, length_of_new_list)

答案 4 :(得分:0)

data = np.random.randint(low = 1,high = 100,size =(4,4),dtype ='int')

答案 5 :(得分:0)

或者您可以通过减去最小值(或加上最小值的绝对值)来将整个分布“右移”到“右”:

y = np.random.normal(0.0, 1.0, 10)

y
array([-0.16934484,  0.06163384, -0.29714508, -0.25917105, -0.0395456 ,
        0.17424635, -0.42289079,  0.71837785,  0.93113373,  1.12096384])

y - min(y)
array([0.25354595, 0.48452463, 0.12574571, 0.16371974, 0.38334519,
       0.59713714, 0.        , 1.14126864, 1.35402452, 1.54385463])

答案 6 :(得分:0)

这个问题是合理的。为了获得动力,请考虑simulations of biological cells。细胞中某种分子的数量分布可以通过正态分布进行近似估算,但必须非负值才能​​具有物理意义。

我的整个模拟器使用这种方法来采样分子计数的初始分布:

def non_neg_normal_sample(random_state, mean, std, max_iters=1000):
    """ Obtain a non-negative sample from a normal distribution

    The distribution returned is normal for 0 <= x, and 0 for x < 0

    Args:
        random_state (:obj:`numpy.random.RandomState`): a random state
        mean (:obj:`float`): mean of the normal dist. to sample
        std (:obj:`float`): std of the normal dist. to sample
        max_iters (:obj:`int`, optional): maximum number of draws of the true normal distribution

    Returns:
        :obj:`float`: a normal sample that is not negative

    Raises:
        :obj:`ValueError`: if taking `max_iters` normal sample does not obtain one that is not negative
    """
    iter = 0
    while True:
        sample = random_state.normal(mean, std)
        iter += 1
        if 0 <= sample:
            return sample
        if max_iters <= iter:
            raise ValueError(f"{iter} draws of a normal dist. with mean {mean:.2E} and std {std:.2E} "
                             f"fails to obtain a non-negative sample")

我通过两种方式扩展@ gena-kukartsev的答案:首先,我避免了可能会使调用堆栈溢出的递归。 (让我们避免使用可能使stackoverflow上的堆栈溢出的答案!)其次,我通过限制分布的样本数来捕获可能不好的输入。

答案 7 :(得分:0)

您可以使用低比例的高 loc:

dependencies{
   compile (':project A')
}
相关问题