Issue
I am new to numpy. I want to generate random values from 0 to 1 with random distribution using numpy, the known input is the standard deviation = 0.2 and the Mean = 0.55 and no. of population = 1000. I used this code:
number = np.random.normal(avg, std_dev, num_pop).round(2)
However it generated number with negative values and also values greater than 1. How to limit the value from 0 to 1?
Solution
The normal distribution has no lower or upper bound, and sampling from it and discarding the results outside your bounds until you get the 1000 necessary points would be awkward. Luckily there's the truncated normal distribution:
from scipy import stats
low = 0
high = 1
mean = 0.55
stddev = 0.2
num_pop = 1000
number = stats.truncnorm.rvs(low, high,
loc = mean, scale = stddev,
size = num_pop)
Answered By - BatWannaBe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.