numpy:np.random.choice()理解

拜读了这篇文章对np.random.choice()有所了解.本文部分引自这篇文章.
https://blog.csdn.net/wyx100/article/details/80639653?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

先了解一下这个 choice:

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中元素一一对应,表示该元素出现的概率,概率大的,出现较多)

有几点说明如下测试图:
1,a 必须是一维的
2,size 可以是多维的
3,p内的数字对应着a 内各个数字出现的概率,size 要和 a 一样,而且要相加为 1.也就是p 内所有的数字概率之和为 1.

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章