如何在数据集中的python中通过加权概率获得均匀分布?

Jef*_*eff 2 python numpy probability pandas uniform-distribution

我已经看过这个问题并且知道numpy.random.choice,但是我的问题略有不同。

鉴于此,我有一个数据集,如下所示:

dict ={"Number of polyps":[10,8,3,1,2,6,13],
        "Right ":[3,2,3,1,0,3,3],
        "Left":[2,2,4,15,6,7,1] }

dt = pd.DataFrame(dict)
Run Code Online (Sandbox Code Playgroud)

因此,它是:

Number of polyps  Right   Left
            10       3     2
             8       2     2
             3       3     4
             1       1    15
             2       0     6
             6       3     7
            13       3     1
Run Code Online (Sandbox Code Playgroud)

我需要按以下要求重新填充RightLeft

  1. 的总和Right,并Left等于Number of polyps
  2. 的值RightLeft来自加权概率它们的当前

例如,对于给定的行,如下所示:

Number of polyps  Right   Left
            10       3     2
Run Code Online (Sandbox Code Playgroud)

因此,对于此行,可能如下所示。这里0.6= 3/(3+2)0.4= 2/(3+2)

nr = np.random.choice(["Right","Left"],size=10, replace=True,p=[0.6,0.4])
rightCount = list.count('Right')
leftCount = list.count('Left')
print(rightCount)
print(leftCount)
Run Code Online (Sandbox Code Playgroud)

更新后,该行将是:

Number of polyps  Right   Left
            10       3     7
Run Code Online (Sandbox Code Playgroud)

问题是,我必须对数据集中的所有行都执行此操作,但是我不确定该怎么做!

NPE*_*NPE 5

您实际上是从二项式分布中提取的。它在NumPy中实现为numpy.random.binomial

>>> dt["Right"] = np.random.binomial(dt["Number of polyps"], dt["Right"]/(dt["Right"]+dt["Left"]))
>>> dt["Left"] = dt["Number of polyps"] - dt["Right"]
Run Code Online (Sandbox Code Playgroud)

在这里,对于每一行,我们执行dt["Number of polyps"]二进制选择试验,每个试验均以Right概率dt["Right"]/(dt["Right"]+dt["Left"])Left其他方式进行选择。