I want the values randomly between 0 and 1 on a normal distrubition and filling a 2x2 array
CodePudding user response:
You can use the softmax function.
import numpy as np
from scipy.special import softmax
x = np.random.rand(2,2)
x = softmax(x)
CodePudding user response:
This should do what you are requesting (see code comments for explanation):
from numpy.random import default_rng
#Create the random number generator
rng = default_rng()
#Create a 2x2 matrix of samples from a normal distribution.
#The values will be normalized later, so the default mean and standard deviation are okay.
vals = rng.standard_normal((2,2))
#Normalize values to be between 0 and 1
vals = (vals - vals.min()) / vals.ptp()
With this above method you will always have a zero and always have a 1 in your 2x2 matrix after normalization. I don't know what your use case is, but is this really what you want? Instead perhaps you could set your mean to 0.5, standard deviation to 0.17, and np.clip anything below 0 or above 1? Here's what that would look like:
import numpy as np
mean = 0.5
standard_deviation = 0.17
s = np.random.default_rng().normal(mean, standard_deviation, (2,2))
s = np.clip(s, 0.0, 1.0)
Reference Sources: