How to realize Random sampling and probability Distribution by Python
This article mainly introduces Python how to achieve random sampling and probability distribution, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. Binomial (binomial) / Bernoulli (Bernoulli) Distribution 1.1 probability quality function (pmf)
The binomial distribution P (Xerox; n, p) can represent the probability of success x times for n independent repeated trials with two possible results of success and failure (corresponding to probability p and 1 − p, respectively).
1.2 function prototype random.binomial (n, p, size=None)
Parameters:
N: int or array_like of ints corresponds to the parameter n in the distribution function, > = 0, and the floating point number will be truncated to be shaped.
P: float or array_like of floats corresponds to the distribution function parameter p, > = 0 and 0, ∑ i=1kxi=1 α = (α 1, α 2 random.dirichlet, α k). α I > 04.2 function prototype random.dirichlet (alpha, size=None)
Parameters:
Alpha: sequence of floats, length k corresponds to the parameter vector α in the distribution function, and the length is k.
Size: int or tuple of ints, optional is the output shape size, because each sample collected is a random vector, and k is automatically added to the last dimension by default. If the given shape is (mline n), then m × n random vectors with dimension k will be extracted from it. The default is None, which returns a k-dimensional random vector.
Return:
Out: the sample taken by ndarray , the size is (size,k).
4.3 use samples
If α = (10 ~ 5 ~ 5 ~ 3), size= (2 ~ 2 ~ 2), then the samples collected are 2 × 2 random vectors with dimensions of Kwon _ 3.
S = np.random.dirichlet ((10,5,3), size= (2,2)) print (s) # [[0.82327647 0.09820451 0.07851902] # [0.50861077 0.4503409 0.04104833]] # [[0.31843167 0.22436547 0.45720285] # [0.40981943 0.40349597 0.1866846]
This function is used in the implementation code [2] of paper [1] to generate weight vectors that conform to Dirichlet distribution.
For cluster_id in range (n_clusters): # generate a weight vector for each client. The distribution parameter alpha in the article is the same in each dimension. Weights = np.random.dirichlet (alpha=alpha * np.ones (n_clients)) clients_ counts [cluster _ id] = np.random.multinomial (clusters_ sizes [cluster _ id], weights) Thank you for reading this article carefully. I hope the article "how to achieve random sampling and probability distribution of Python" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!