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.

在這裏插入圖片描述

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