NumPy 如何从列表中以不同的概率选择元素

NumPy 如何从列表中以不同的概率选择元素

使用NumPy库有多种方法可以从列表中以不同的概率选择元素。

在Python中,NumPy库提供了一个名为 random 的模块,其中包含了几个函数,如 choice(),multinomial() 等,用于根据不同的概率从数组中选择元素。在列表中定义的所有概率值之和应该等于1。接下来我们逐个了解每种方式。

使用random.choice()函数

random模块提供了 choice() 函数,用于根据指定的概率分布从给定的 一维 数组中计算一个随机样本。以下是使用 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()函数,使用指定的元素列表和概率列表创建2维数组。

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)

在下面的例子中,我们使用multinomial()函数创建一个1维数组,通过传入试验次数、概率列表和数组大小作为输入参数。然后将根据多项式分布元素的定义概率创建数组。

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()函数

此函数用于根据定义的概率列表生成具有随机选择元素的数组。此函数仅适用于Numpy 1.17版本或更高版本。

示例

以下是使用该函数生成一维数组的示例。

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]]

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程