Issue
Suppose i have the following two arrays with means and standard deviations:
mu = np.array([2000, 3000, 5000, 1000])
sigma = np.array([250, 152, 397, 180])
Then:
a = np.random.normal(mu, sigma)
In [1]: a
Out[1]: array([1715.6903716 , 3028.54168667, 4731.34048645, 933.18903575])
However, if i ask for 100 draws for each element of mu, sigma:
a = np.random.normal(mu, sigma, 100)
a = np.random.normal(mu, sigma, 100)
Traceback (most recent call last):
File "<ipython-input-417-4aadd7d15875>", line 1, in <module>
a = np.random.normal(mu, sigma, 100)
File "mtrand.pyx", line 1652, in mtrand.RandomState.normal
File "mtrand.pyx", line 265, in mtrand.cont2_array
ValueError: shape mismatch: objects cannot be broadcast to a single shape
I have also tried using a tuple for size(s):
s = (100, 100, 100, 100)
a = np.random.normal(mu, sigma, s)
What am i missing?
Solution
I don't believe you can control the size parameter when you pass a list/vector of values for the mean and std. Instead, you can iterate over each pair and then concatenate:
np.concatenate(
[np.random.normal(m, s, 100) for m, s in zip(mu, sigma)]
)
This gives you a (400, )
array. If you want a (4, 100)
array instead, call np.array
instead of np.concatenate
.
Answered By - cs95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.