numpy.random.choice
-
numpy.random.
choice
(
a,
size=None,
replace=True,
p=None
)
-
Generates a random sample from a given 1-D array
New in version 1.7.0.
Parameters: a : 1-D array-like or int
If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a was np.arange(n)
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
replace : boolean, optional
Whether the sample is with or without replacement
p : 1-D array-like, optional
The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
Returns: samples : 1-D ndarray, shape (size,)
The generated random samples
Raises: ValueError
If a is an int and less than zero, if a or p are not 1-dimensional, if a is an array-like of size 0, if p is not a vector of probabilities, if a and p have different lengths, or if replace=False and the sample size is greater than the population size
如果是ndarray数组,随机样本在该数组获取(取数据元素), If an int, the random sample is generated as if a was np.arange(n)
size : int or tuple of ints, optional
大小:整型或 整型元组中元素个数,可选
替换:布尔型,可选 Whether the sample is with or without replacement
样本是否有重复值(False,没有;True,有;默认:True)
1维数组,可选
和a里的每个输入联系,如果没有该参数,默认 假设 a里的每个输入等概率出现。(和a中元素一一对应,表示该元素出现的概率,概率大的,出现较多)
a: | 0 | 1 | 2 | 3 | 4 |
p: | 0.1 | 0 | 0.3 | 0.6 | 0 |