本文簡要介紹 python 語言中 numpy.random.dirichlet
的用法。
用法:
random.dirichlet(alpha, size=None)
從狄利克雷分布中抽取樣本。
從狄利克雷分布中繪製尺寸為 k 的
size
樣本。 Dirichlet-distributed 隨機變量可以看作是 Beta 分布的多元泛化。 Dirichlet 分布是貝葉斯推理中多項分布的共軛先驗。注意
新代碼應改為使用
default_rng()
實例的dirichlet
方法;請參閱快速入門。- alpha: 浮點數序列,長度 k
分布參數(長度為
k
的樣本長度為k
)。- size: int 或整數元組,可選
輸出形狀。例如,如果給定的形狀是
(m, n)
,則繪製m * n * k
樣本。默認為無,在這種情況下,將返回長度為k
的向量。
- samples: 數組,
繪製的樣本,形狀為
(size, k)
。
- ValueError
如果
alpha
中的任何值小於或等於零
參數:
返回:
拋出:
注意:
Dirichlet 分布是對滿足條件 和 的向量 的分布。
Dirichlet-distributed隨機向量 的概率密度函數 與
其中 是包含正濃度參數的向量。
該方法使用以下屬性進行計算:假設 是一個隨機向量,其分量遵循標準 gamma 分布,則 為 Dirichlet-distributed
參考:
David McKay,“信息理論、推理和學習算法”,第 23 章,http://www.inference.org.uk/mackay/itila/
維基百科,“Dirichlet distribution”,https://en.wikipedia.org/wiki/Dirichlet_distribution
1:
2:
例子:
以 Wikipedia 中引用的一個示例為例,如果要將字符串(每個初始長度為 1.0)切割成不同長度的 K 段,則可以使用此分布,其中每段平均具有指定的平均長度,但允許在件的相對尺寸。
>>> s = np.random.dirichlet((10, 5, 3), 20).transpose()
>>> import matplotlib.pyplot as plt >>> plt.barh(range(20), s[0]) >>> plt.barh(range(20), s[1], left=s[0], color='g') >>> plt.barh(range(20), s[2], left=s[0]+s[1], color='r') >>> plt.title("Lengths of Strings")
相關用法
- Python numpy random.mtrand.RandomState.wald用法及代碼示例
- Python numpy random.mtrand.RandomState.multivariate_normal用法及代碼示例
- Python numpy random.standard_exponential用法及代碼示例
- Python numpy random.mtrand.RandomState.gumbel用法及代碼示例
- Python numpy random.mtrand.RandomState.multinomial用法及代碼示例
- Python numpy random.rand用法及代碼示例
- Python numpy random.mtrand.RandomState.logistic用法及代碼示例
- Python numpy random.mtrand.RandomState.shuffle用法及代碼示例
- Python numpy random.triangular用法及代碼示例
- Python numpy random.noncentral_f用法及代碼示例
- Python numpy random.mtrand.RandomState.poisson用法及代碼示例
- Python numpy random.lognormal用法及代碼示例
- Python numpy random.mtrand.RandomState.seed用法及代碼示例
- Python numpy random.mtrand.RandomState.triangular用法及代碼示例
- Python numpy random.gumbel用法及代碼示例
- Python numpy random.mtrand.RandomState.weibull用法及代碼示例
- Python numpy random.shuffle用法及代碼示例
- Python numpy random.geometric用法及代碼示例
- Python numpy random.multinomial用法及代碼示例
- Python numpy random.logseries用法及代碼示例
- Python numpy random.mtrand.RandomState.rand用法及代碼示例
- Python numpy random.mtrand.RandomState.power用法及代碼示例
- Python numpy random.mtrand.RandomState.randint用法及代碼示例
- Python numpy random.chisquare用法及代碼示例
- Python numpy random.mtrand.RandomState.choice用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.random.dirichlet。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。