lee-romantic 's Blog
Everything is OK!
Toggle navigation
lee-romantic 's Blog
主页
About Me
归档
标签
numpy中的choice()函数
2018-12-03 15:46:05
364
0
0
lee-romantic
`RandomState.choice(a, size=None, replace=True, p=None)` –通过给定的一维数组数据产生随机采样 **参数:** `a:`一维数组或者int型变量,如果是数组,就按照里面的范围来进行采样,如果是单个变量,则采用`np.arange(a)`的形式 `size `: `int `或者 `tuple of ints,` 可选参数 决定了输出的shape. 如果给定的是, `(m, n, k),` 那么` m * n * k` 个采样点将会被采样. 默认为零,也就是只有一个采样点会被采样回来。 `replace `: 布尔参数,可选参数 决定采样中是否有重复值 `p `:一维数组参数,可选参数 对应着a中每个采样点的概率分布,如果没有标出,则使用标准分布。 返回值: `samples : single item or ndarray` 容易引发的错误 `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.arange(5) `中产生一个size为3的随机采样: ``` >>> np.random.choice(5, 3) array([0, 3, 4]) >>> #This is equivalent to np.random.randint(0,5,3) ``` 从` np.arange(5) `中产生一个非标准的 size为 3的随机采样: ``` >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0]) array([3, 3, 0]) ``` 从 `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] ``` 也可以这样,不必一定是整型数字: ``` >>> 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') ``` 实际使用中,首先创建一个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
上一篇:
python中的迭代器和生成器
下一篇:
python中的字符串分割函数split()
0
赞
364 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
提交评论
立即登录
, 发表评论.
没有帐号?
立即注册
0
条评论
More...
文档导航
没有帐号? 立即注册