如何使用 NumPy 從列表中選擇具有不同機率的元素?
使用 numpy 庫有多種方法可以從列表中選擇具有不同機率的元素。
在 Python 中,NumPy 庫提供了一個名為 **random** 的模組,其中包含多個函式,例如 **choice()**、**multinomial()** 等,用於根據不同機率從陣列中選擇元素。列表中定義的所有機率值的總和應等於 1。讓我們逐一檢視每種方法。
使用 random.choice() 函式
random 模組提供了 **choice()** 函式,該函式用於根據指定的機率分佈從給定的 **1-d** 陣列中計算隨機樣本。以下是使用 **choice()** 函式的語法。
numpy.random.choice(list,size = (rows,columns),p = probability_values)
其中,
**numpy** 是庫。
**random** 是 numpy 中的模組。
**choice** 是獲取具有定義機率的元素的函式
**size** 是具有定義的行數和列數的陣列大小。
**list** 是元素列表。
**p** 是機率
示例
在下面的示例中,我們將不同的機率值和列表作為引數傳遞給 **choice()** 函式,並根據指定的機率獲取元素。
import numpy as np list = [10,12,4,5,98] probabilities = [0.1, 0.2, 0.3, 0.2, 0.2] arr = np.random.choice(list, size=8, p=probabilities) print("The array created with the defined probability of elements:", arr)
輸出
The array created with the defined probability of elements: [10 4 5 10 98 12 4 12]
示例
以下是另一個示例,它使用 numpy 庫的 **choice()** 函式建立具有指定元素列表和機率列表的二維陣列。
import numpy as np lst = [1,0,12,4,5,98,34] probabilities = [0.1, 0.1, 0.2, 0.2, 0.2,0.1,0.1] arr = np.random.choice(lst, size=8, p=probabilities) print("The array created with the defined probability of elements:",arr)
輸出
The array created with the defined probability of elements: [[ 0] [ 5] [ 5] [ 4] [12] [ 4] [34]]
使用 random.multinomial() 函式
random 模組中提供的另一個函式是 **multinomial()** 函式,該函式根據定義的試驗次數和機率生成多項分佈的元素。
語法
以下是 **multinomial()** 函式的語法。numpy.random.multinomial(n, pvals, size=None)
其中,
**numpy** 是庫。
**random** 是 numpy 中的模組。
**multinomial** 是獲取具有定義機率的多項分佈元素的函式。
**size** 是具有定義的行數和列數的陣列大小。
**n** 是試驗次數。
**pvals** 是機率。
示例
在下面的示例中,我們透過將試驗次數、機率列表和陣列大小作為輸入引數傳遞給 **multinomial()** 函式,建立一個 **1-d** 陣列。然後將根據多項分佈元素的定義機率建立陣列。
import numpy as np probabilities = [0.1, 0.2, 0.3, 0.2, 0.2] arr = np.random.multinomial(5, probabilities) print("The array created with the defined probability of elements:",arr)
輸出
The array created with the defined probability of elements: [0 1 1 3 0]
示例
以下是另一個示例,它根據定義的機率建立具有多項分佈元素的二維陣列。
import numpy as np probabilities = [0.1, 0.2, 0.3, 0.2, 0.2] arr = np.random.multinomial(5, probabilities,size = (2)) print("The array created with the defined probability of elements:",arr)
輸出
The array created with the defined probability of elements: [[1 0 2 2 0] [0 1 1 2 1]]
使用 random.default_rng().choice() 函式
此函式用於生成具有隨機選擇的元素的陣列,並使用定義的已提及機率列表。此函式僅在 1.17 或更高版本的 Numpy 中可用。
示例
以下是使用此函式生成一維陣列的示例。
import numpy as np rng = np.random.default_rng() elements = [1, 2, 3, 4,10,3,4] arr = rng.choice(elements, p=[0.1, 0.1, 0.2, 0.3,0.1,0.1,0.1],size = 10) print("The array created with the defined probability of elements:",arr)
輸出
以下是用於建立一維陣列的 **random.default_rng().choice()** 的輸出
The array created with the defined probability of elements: [3 3 4 4 1 4 4 3 4 4]
示例
讓我們再看一個使用 random 模組的 **random.default_rng().choice()** 函式根據定義的機率建立二維陣列的示例。
import numpy as np rng = np.random.default_rng() elements = [23,43,42,5,78,90] arr = rng.choice(elements, p=[0.1, 0.2, 0.2, 0.3,0.1,0.1],size = (5,5)) print("The array created with the defined probability of elements:",arr)
輸出
The array created with the defined probability of elements: [[ 5 78 78 90 43] [ 5 78 5 43 78] [42 5 42 5 90] [ 5 43 5 43 23] [78 42 5 5 78]]