有条件概率的随机选择

时间:2019-06-27 21:24:18

标签: python numpy random choice

我有一个说y = [1, 2, 3, 4, 6, 7, 8, 9, 5, 23, 12, 24, 43, 10]的列表,我想有条件地从中进行随机选择。列表中大于10的数字表示被选中的概率为0.8,而其余的数字则为0.2。

2 个答案:

答案 0 :(得分:1)

由于random.choice提供了均匀的分布,因此您将必须分两步工作。首先在值的组之间选择(10以下和10以上)。然后在组中选择一个值。

要在组之间获得不同的概率,可以创建一个列表,其中包含每个组的适当重复次数。例如,对于0.2和0.8,列表中将有2个“ below10”组实例和8个“ above10”组实例。这样会将正态分布转换为相对于每个组的加权分布。

import random

treshold = 10
y       = [1, 2, 3, 4, 6, 7, 8, 9, 5, 23, 12, 24, 43, 10]
group1  = [v for v in y if v <  treshold]
group2  = [v for v in y if v >= treshold]

def getValue():
    group = random.choice([group1]*2 + [group2]*8)
    return random.choice(group)

要测试分布是否符合要求,可以多次使用该函数,并计算每个组中选择一个值的次数。

lowCount  = 0
highCount = 0
N         = 10000
for _ in range(N):
    v = getValue()
    if v < treshold:
        lowCount += 1
    else:
        highCount += 1
print(round(lowCount/N,2),round(highCount/N,2))

# 0.2 0.8

如果只有两个组,则可以使用简单的if-else语句进行选择:

def getValue():
    return random.choice(group1) if random.random() <= 0.2 else random.choice(group2)

编辑对于概率为0.9的单个值(假设为23),方法是相同的:

y       = [1, 2, 3, 4, 6, 7, 8, 9, 5, 23, 12, 24, 43, 10]
group1  = [23]
group2  = [v for v in y if v not in group1]
def getValue():
    return random.choice(group1) if random.random() <= 0.9 else random.choice(group2)

lowCount  = 0
highCount = 0
N         = 10000
for _ in range(N):
    v = getValue()
    if v == 23:        # <== same condition as the grouping rule.
        lowCount += 1
    else:
        highCount += 1
print(round(lowCount/N,2),round(highCount/N,2))

# 0.9 0.1

您必须相应地调整测试循环

答案 1 :(得分:0)

尝试一下: 它使用列表中的np.random.choice元素元素,该元素成员的列表满足每个元素的能力,for循环将从输入列表中生成该列表:

Sub ACount()
Dim FirstRowofI
Dim NbrOfA as Integer
Dim NbrOfOpenA as Integer
Row = 2

Set FirstRowofI = (Sheet2.Cells.Range(Row, 14))
NbrOfA = 0
NbrOfOpenA = 0
    If (Sheet2.Cells(Row, 14).Value <> "") Then
        NbrOfA = 1
    If (Sheet2.Cells(Row, 22) <> "Closed") Then
        NbrOfOpenA = 1
Set Row = FirstRowofI
Row = Row + 1

Do While (Sheet2.Cells(Row, 14) = (Sheet2.Cells(FirstRowofI, 14)))
    NbrOfOpenA = NbrOfOpenA + 1
    If (Sheet2.Cells(Row, 22) <> "Closed" Then
        NbrOfOpenA = NbrOfOpenA + 1
        Range(Row).EntireRow.Delete
    Return
    End If
Set Cell(Sheet2.Cells(FirstRowofI, 23)) = NbrOfA
Set Cell(Sheet2.Cells(FirstRowofI, 24)) = NbrOfOpenA

Loop

End Sub

输出

import numpy as np
y = [1, 2, 3, 4, 6, 7, 8, 9, 5, 23, 12, 24, 43, 10]
t=[]
sup=0
inf=0
for j in y:
    if j>10:
        sup=sup+1
    else:
        inf=inf+1
print(sup)
print(inf)
p=[]
for i in y:
    if i>10:
        p.append(1/sup)
    else:
        p.append(0/inf)
print(p)
x=np.random.choice(y, 100, p=p)

print(x)

另一个输出的概率为0.5和0.5:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.25, 0.25, 0.25, 0.0]
[12 23 23 43 23 12 12 43 24 12]

另一个与:

[0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.125, 0.125, 0.125, 0.125, 0.05]
[24 24 43  8  4 12 23 24  6  5]