【问题标题】:How to get uniform distribution by weighted probability in python in a dataset?如何通过数据集中python中的加权概率获得均匀分布?
【发布时间】:2019-09-29 18:57:34
【问题描述】:

我已经看过this question,我知道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)

原来如此:

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

我需要按照以下要求重新填写RightLeft

  1. RightLeft 之和等于Number of polyps
  2. RightLeft 的值来自它们当前值的加权概率

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

Number of polyps  Right   Left
            10       3     2

所以,对于这一行,它可能如下所示。这里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)

更新此行后将是:

Number of polyps  Right   Left
            10       3     7

问题是,我必须对数据集中的所有行都这样做,但我不知道该怎么做!

【问题讨论】:

  • 如果它适用于一行,将其放入 for 循环将使其适用于所有行...?

标签: python pandas numpy probability uniform-distribution


【解决方案1】:

您实际上是从binomial distribution 中提取的。它在 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"]

在这里,对于每一行,我们执行dt["Number of polyps"]二元选择试验,每个试验选择Right,概率为dt["Right"]/(dt["Right"]+dt["Left"]),否则Left

【讨论】:

  • 但是Binomial distributionUniform 略有不同?对? @NPE
  • @Jeff:你的分布没有什么统一的。 (均匀分布是连续的,而您的情况是离散的。)
猜你喜欢
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 2011-08-22
  • 2020-03-23
  • 1970-01-01
  • 2014-03-02
相关资源
最近更新 更多