np.random.choice用法中文和样例详解

官方介绍

numpy.random.choice

numpy.random. choice ( asize=Nonereplace=Truep=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


np.random.choice
RandomState.choice(a, size=None, replace=True, p=None)
a : 1-D array-like or int    If an ndarray, a random sample is generated from its elements. 
    如果是ndarray数组,随机样本在该数组获取(取数据元素),    If an int, the random sample is generated as if a was np.arange(n)
如果是整型数据随机样本生成类似np.arange(n)    
size : int or tuple of ints, optional 
    大小:整型或 整型元组中元素个数,可选
replace : boolean, optional
    替换:布尔型,可选    Whether the sample is with or without replacement
    样本是否有重复值(False,没有;True,有;默认:True)
见例子3
p : 1-D array-like, optional
    1维数组,可选
    The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
和a里的每个输入联系,如果没有该参数,默认 假设 a里的每个输入等概率出现。(和a中元素一一对应,表示该元素出现的概率,概率大的,出现较多)
    见例子2

实例
例子1: np.arange(5) 中产生一个size为3的随机采样:
>>> np.random.choice( 5 , 3 )
array([ 0 , 3 , 4 ])
>>> #This is equivalent to np.random.randint(0,5,3)
例子2:从 np.arange(5) 中产生一个非标准的 size为 3的随机采样:
>>> np.random.choice( 5 , 3 , p = [ 0.1 , 0 , 0.3 , 0.6 , 0 ])
array([ 3 , 3 , 0 ])
说明:3出现概率最大,所以array([3, 3, 0])中,3最多

a: 0 1 2 3 4
p: 0.1 0 0.3 0.6 0
例子3: np.arange(5) 产生一个标准分布、size为 3、没有重复替换的随机采样:
>>> np.random.choice( 5 , 3 , replace = False )
array([ 3 , 1 , 0 ])
>>> #This is equivalent to np.random.permutation(np.arange(5))[:3]
例子4:也可以这样,不必一定是整型数字:
>>> aa_milne_arr = [ 'pooh' , 'rabbit' , 'piglet' , 'Christopher' ]
>>> np.random.choice(aa_milne_arr, 5 , p = [ 0.5 , 0.1 , 0.1 , 0.3 ])
array([ 'pooh' , 'pooh' , 'pooh' , 'Christopher' , 'piglet' ], dtype = '|S11' )
例子5:实际使用中,首先创建一个mask变量,然后通过mask来对需要采样的数据进行采样:
... mask = np.random.choice(split_size, batch_size)
captions = data[ '%s_captions' % split][mask]
image_idxs = data[ '%s_image_idxs' % split][mask]...

参考:
https://oldpan.me/archives/python-numpy-choice

猜你喜欢

转载自blog.csdn.net/wyx100/article/details/80639653
今日推荐