如何使用 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]]
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP