Python numpy.random.normal only positive values

I want to create a normal distributed array with numpy.random.normal, which consists of only positive values. For example, the following illustrates that it sometimes returns negative values, and sometimes positives. How can I change it so that it returns only positive values?

>>> 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 ]) >>> 

I think I could somehow solve this:

 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) 
+9
source share
6 answers

The normal distribution, by definition, extends from -inf to + inf, so what you are asking for does not make sense mathematically.

You can accept the normal distribution and accept the absolute value of the “clip” as positive values ​​or just drop the negative values, but you must understand that it will no longer be the normal distribution.

+6
source

You can compensate the entire array by the smallest (left left) array. What you get may not really be a “normal distribution,” but as part of your work on the final array, you can make sure that the values ​​are positive and that they correspond to the bell curve.

 >>> 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]) 
+1
source

I assume that you mean that you want to change the probability density so that it is the same as normal in the positive range, and zero is negative. This is a fairly common practical case. In this case, you cannot just take the absolute value of the generated normal random variables. Instead, you should create a new independent normally distributed number until you come up with a positive one. One way to do this recursively, see below.

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))

+1
source

how about using lognormal in these lines:

  mu = np.mean(np.log(list)) sigma = np.std(np.log(list)) new_list = np.random.lognormal(mu, sigma, length_of_new_list) 
0
source

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

0
source

Or maybe you could just “shift” your entire distribution to the right by subtracting the minimum (or adding the absolute value of your minimum):

 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]) 
0
source

All Articles