For this section of code, I am trying to randomly choose either theta or mu to be zero. When one variable is zero, then I need the other one uniformly randomized (and vice versa).
N = 10000
random = np.arccos(np.random.uniform(-1, 1, N))
zero = 0
choices = [random, zero]
theta = np.random.choice(choices)
if theta == random:
mu = zero
else:
mu = random
I know that random and zero do not have a homogenous shape.
This is why I got the error:
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.
However, I do not know how to fix this (I am still very new to programming). Any thoughts would be appreciated.
You wouldn't really want to use
np.random.choice()here, because it doesn't take anaxisparameter, so it can't be vectorized. It would be better to create a boolean mask from a random variable, and use that to select whether the random value goes into the mu or theta variable.np.where()can do that.Example: