當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.random.dirichlet()用法及代碼示例

借助於dirichlet()方法,我們可以從dirichlet分布中獲取隨機樣本,並使用該方法返回一些隨機樣本的numpy數組。

用法:numpy.random.dirichlet(alpha, size=None)

參數:

1)alpha-樣本數。

2)size-numpy數組的輸出形狀。



返回:返回隨機樣本數組。

範例1:

在此示例中,我們可以看到,通過使用random.dirichlet()方法,我們能夠獲取dirichlet分布的隨機樣本,並返回具有參數中定義的大小的numpy數組。

Python3

# import dirichlet 
import numpy as np 
import matplotlib.pyplot as plt 
  
# Using dirichlet() method 
gfg = np.random.dirichlet((3, 4, 5, 19), size = 1000) 
  
count, bins, ignored = plt.hist(gfg, 30, density = True) 
plt.show()

輸出:

範例2:

Python3

# import dirichlet 
import numpy as np 
import matplotlib.pyplot as plt 
  
# Using dirichlet() method 
gfg = np.random.dirichlet((6, 5, 4, 3, 2, 1), 1000) 
  
count, bins, ignored = plt.hist(gfg, 30, density = True) 
plt.show()

輸出:

相關用法


注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 numpy.random.dirichlet() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。