Multi-term sample generation in python

I can generate binomial samples from a probability array at the desired size (the output must be the same size as the input probability array shape) using the following lines of code

prob_list = [[0.3,0.3,0.4],[0.4,0.3,0.3]]
prob_array = np.asarray(prob_list)
y_sample = np.random.binomial(size=prob_array.shape, n=1, p=prob_array)
print(y_sample)

      

Output signal

[[0 0 0]
[1 1 1]]

      

The shape of the input probabilities (2 * 3) and the output samples (2 * 3) are the same.

Can you do the same with a polynomial? You can create multi-term patterns for one line.

y_sample = np.random.multinomial(size=1, n=1, pvals=prob_array[0])
print(y_sample)
[[1 0 0]]

      

How can one generalize this to get the output in the same way as binomial (output sample form = input probability form?

+3


source to share


1 answer


I'm afraid you'll have to manually iterate over prob_array

. pvals

currently limited to a single 1D array of probabilities.



+3


source







All Articles