在Python中以不同的概率从 List 中选择元素
你有没有想过如何在Python中从不同概率的列表中选择随机元素?在本文中,我们将讨论如何做同样的事情。让我们首先考虑下面的例子。
Python3
import random
sam_Lst = [10, 20, 3, 4, 100]
ran = random.choice(sam_Lst)
print(ran)
Python3
import random
# Creating a number list
num_lst = [1, 22, 43, 19, 13, 29]
print(random.choices(num_lst, weights=(
14, 25, 30, 45, 55, 10), k=6))
Python3
import random
# Creating a name list
name_lst = ['October', 'November', 'December',
'January', 'March', 'June']
print(random.choices(name_lst, weights=(
40, 25, 30, 5, 15, 80), k=3))
Python3
import random
# Creating a name list
name_lst = ['Fit', 'Infected', 'Recovered', 'Danger']
# Using FOR loop
# to choose the element from list with
# different probability
for i in range(6):
print("Random choice", i+1)
randomele = random.choices(name_lst, weights=(
50, 80, 10, 5), k=1)
print(randomele[0])
Python3
import random
# Creating a number list
num_lst = [1, 22, 93, 19, 13, 25]
print(random.choices(num_lst, cum_weights=(
7, 13, 15, 20, 25, 20), k=6))
Python3
import random
# Creating a name list
name_lst = ['October', 'November', 'December',
'January', 'March', 'June']
print(random.choices(name_lst, cum_weights=(
40, 20, 3, 7, 15, 15), k=3))
Python3
import random
# Creating a name list
name_lst = ['October', 'November', 'December',
'January', 'March', 'June']
# Using FOR loop
# to choose the element from list with
# different probability
for i in range(6):
print("Random choice", i+1)
randomele = random.choices(name_lst, cum_weights=(
7, 13, 15, 20, 25, 20), k=1)
print(randomele[0])
在上面的例子中,从列表中获取任何元素的概率是相等的。但是我们想要这样的方法,其中从列表中选择一个元素的概率是不同的。这在Python中称为加权随机选择。为了在Python中找到加权随机选择,有两种方法:
- 相对权重
- 累积权重
在这种情况下可以帮助我们的函数是random.choices() 。此函数允许在Python中进行带替换的加权随机选择。
句法:
random.choices(population, weights=None, *, cum_weights=None, k=1)
在这里,“权重”参数起着重要作用。
案例 1:使用相对权重
分配给元素的权重称为相对权重。
示例 1:
蟒蛇3
import random
# Creating a number list
num_lst = [1, 22, 43, 19, 13, 29]
print(random.choices(num_lst, weights=(
14, 25, 30, 45, 55, 10), k=6))
输出:
[19, 19, 13, 22, 13, 13]
在上面的例子中,我们为列表的每个元素分配权重。元素' 13'的权重最高,即55,所以它出现的概率最大。正如我们在输出中看到的,元素 13 出现了 3 次,19 出现了 2 次,以此类推。所以,现在从列表中选择一个元素的概率是不同的。
示例 2:
蟒蛇3
import random
# Creating a name list
name_lst = ['October', 'November', 'December',
'January', 'March', 'June']
print(random.choices(name_lst, weights=(
40, 25, 30, 5, 15, 80), k=3))
输出:
['June', 'October', 'June']
在上面的例子中,元素' June'的权重最大,所以它被选中的概率最大。在这里,k=3 这意味着我们只从列表中选择前 3 个元素。
示例 3:
蟒蛇3
import random
# Creating a name list
name_lst = ['Fit', 'Infected', 'Recovered', 'Danger']
# Using FOR loop
# to choose the element from list with
# different probability
for i in range(6):
print("Random choice", i+1)
randomele = random.choices(name_lst, weights=(
50, 80, 10, 5), k=1)
print(randomele[0])
输出:
Random choice 1
Recovered
Random choice 2
Danger
Random choice 3
Infected
Random choice 4
Infected
Random choice 5
Infected
Random choice 6
Fit
在上面的例子中,我们使用 FOR 循环以不同的概率从列表中选择一个元素。
案例 2:使用累积权重
一个元素的累积权重是通过将其前一个元素的权重与其自身权重相加来确定的。
示例 1:
蟒蛇3
import random
# Creating a number list
num_lst = [1, 22, 93, 19, 13, 25]
print(random.choices(num_lst, cum_weights=(
7, 13, 15, 20, 25, 20), k=6))
输出:
[1, 22, 93, 22, 19, 1]
在上面的例子中,元素' 19'的累积权重最大,所以它被选中的概率也最大。
示例 2:
蟒蛇3
import random
# Creating a name list
name_lst = ['October', 'November', 'December',
'January', 'March', 'June']
print(random.choices(name_lst, cum_weights=(
40, 20, 3, 7, 15, 15), k=3))
输出:
['January', 'March', 'January']
在上面的例子中,我们选择 k=3,所以我们得到了具有最大选择概率的前 3 个元素。
示例 3:
蟒蛇3
import random
# Creating a name list
name_lst = ['October', 'November', 'December',
'January', 'March', 'June']
# Using FOR loop
# to choose the element from list with
# different probability
for i in range(6):
print("Random choice", i+1)
randomele = random.choices(name_lst, cum_weights=(
7, 13, 15, 20, 25, 20), k=1)
print(randomele[0])
输出:
Random choice 1
November
Random choice 2
January
Random choice 3
October
Random choice 4
December
Random choice 5
November
Random choice 6
January
在上面的例子中,我们使用 FOR 循环以不同的概率从列表中选择一个元素。
注意: k的值取决于用户,由于其相对权重,所以权重总和可以超过 100。