由于曲线上的点而随机选择

时间:2019-02-06 08:48:41

标签: python random distribution

I have found this curve on a paper

由于有了应用程序的 automeris ,我已经在该曲线上获得了一些点,但是,现在我有了多个点的坐标,我如何用它们创建分布并选择(在Python中)(如果可能)随机在我创建的发行版中?

干杯, 埃迪

数据

45.0, 0.263

47.3, 0.470

51.5, 1.34

53.0, 1.86

54.3, 2.35

55.1, 2.84

55.8, 3.39

56.1, 3.88

56.6, 4.36

57.4, 5.41

57.1, 4.89

57.9, 6.41

57.6, 5.90

58.4, 7.46

58.1, 6.97

58.6, 8.49

58.6, 7.98

59.1, 10.1

59.1, 9.52

59.1, 8.99

59.9, 11.6

59.6, 11.1

59.6, 10.6

60.1, 13.1

60.1, 12.6

60.1, 12.1

60.9, 14.7

60.6, 14.2

60.7, 13.7

61.4, 15.7

61.2, 15.2

61.7, 16.7

61.7, 16.2

62.4, 17.8

62.2, 17.2

62.9, 18.7

62.7, 18.3

63.7, 19.3

64.7, 19.8

67.3, 19.8

68.3, 19.3

69.4, 18.8

70.4, 18.3

71.5, 17.8

72.8, 17.3

74.0, 16.8

75.6, 16.4

77.2, 15.9

78.2, 15.4

79.7, 14.9

81.3, 14.4

82.9, 13.9

84.7, 13.4

86.0, 13.0

87.3, 12.5

89.1, 12.0

90.1, 11.5

91.7, 11.0

92.9, 10.5

94.2, 9.99

95.8, 9.52

96.8, 9.04

98.4, 8.54

99.9, 8.03 

101, 7.59 

103, 7.08 

105, 6.61 

107, 6.13 

109, 5.66 

111, 5.24 

112, 4.74 

117, 3.92 

122, 3.32 

124, 3.12 

128, 2.63 

130, 2.46 

135, 2.10

138, 1.94 

142, 1.73 

146, 1.52 

150, 1.34 

154, 1.18 

163, 0.910 

172, 0.712 

180, 0.592 

2 个答案:

答案 0 :(得分:0)

我将使用该数据创建一个数据框。并使用函数随机选择随机说一行。 在下面查看样机:

import pandas as pd 
import matplotlib.pyplot as plt
df = pd.DataFrame([
[45.0, 0.263],
[47.3, 0.470],
[51.5, 1.34],
[53.0, 1.86],
[54.3, 2.35],
[55.1, 2.84],
[55.8, 3.39],
[56.1, 3.88],
[56.6, 4.36],
[57.4, 5.41],
[57.1, 4.89],
[57.9, 6.41],
[57.6, 5.90],
[58.4, 7.46],
[58.1, 6.97],
[58.6, 8.49],
[58.6, 7.98],
[59.1, 10.1],
[59.1, 9.52],
[59.1, 8.99],
[59.9, 11.6],
[59.6, 11.1],
[59.6, 10.6],
[60.1, 13.1],
[60.1, 12.6],
[60.1, 12.1],
[60.9, 14.7],
[60.6, 14.2],
[60.7, 13.7],
[61.4, 15.7],
[61.2, 15.2],
[61.7, 16.7],
[61.7, 16.2],
[62.4, 17.8],
[62.2, 17.2],
[62.9, 18.7],
[62.7, 18.3],
[63.7, 19.3],
[64.7, 19.8],
[67.3, 19.8],
[68.3, 19.3],
[69.4, 18.8],
[70.4, 18.3],
[71.5, 17.8],
[72.8, 17.3],
[74.0, 16.8],
[75.6, 16.4],
[77.2, 15.9],
[78.2, 15.4],
[79.7, 14.9],
[81.3, 14.4],
[82.9, 13.9],
[84.7, 13.4],
[86.0, 13.0],
[87.3, 12.5],
[89.1, 12.0],
[90.1, 11.5],
[91.7, 11.0],
[92.9, 10.5],
[94.2, 9.99],
[95.8, 9.52],
[96.8, 9.04],
[98.4, 8.54],
[99.9, 8.03], 
[101, 7.59], 
[103, 7.08], 
[105, 6.61],
[107, 6.13], 
[109, 5.66], 
[111, 5.24], 
[112, 4.74],
[117, 3.92], 
[122, 3.32], 
[124, 3.12], 
[128, 2.63],
[130, 2.46], 
[135, 2.10],
[138, 1.94],
[142, 1.73],
[146, 1.52], 
[150, 1.34], 
[154, 1.18], 
[163, 0.910],
[172, 0.712],
[180, 0.592] 
], columns=["Col_A", "Col_B"])

plt.plot(df['Col_A'],df['Col_B']);

在图形上绘制点,我们得到:

enter image description here

要获得随机值,我创建了以下函数:

import random

def RandomSample(data, samplecount):
    return data.iloc[random.sample(data.index, samplecount)]

RandomSample(df,1)

以下结果:这是一个随机行。

Col_A   Col_B
38  64.7    19.8

获取样本的另一种简便方法是仅使用df.sample(n=1),其中n是所需的样本数。

答案 1 :(得分:0)

以下代码对提供的样本之间的分布进行存储,然后根据所述分布随机选择存储。然后,它在样本的任一侧之间线性内插,以确定在样本之间进行选择的点。您可以将其视为积分的梯形近似值(代表累积概率分布)。

from bisect import bisect_left
from random import uniform

def make_lookup(data):
    keys = sorted(data.keys())
    lookup = dict()
    total = 0
    for lkey, hkey in zip(keys, keys[1:]):
        lval, hval = data[lkey], data[hkey]
        total += (lval + hval) * (hkey - lkey) // 2
        lookup[total] = lkey
    return lookup

def draw_number(cprobs, lookup):
    rand = uniform(0, cprobs[-1])
    index = bisect_left(cprobs, rand) - 1
    lo, hi = cprobs[index], cprobs[index+1]
    lkey, hkey = lookup[lo], lookup[hi]
    return lkey + (rand - lo) * (hkey - lkey) / (hi - lo)

def main():
    data = {128.0: 2.63, 130.0: 2.46, 107.0: 6.13, 98.4: 8.54, 135.0: 2.1, 138.0: 1.94, 87.3: 12.5, 142.0: 1.73, 60.7: 13.7, 61.4: 15.7, 150.0: 1.34, 117.0: 3.92, 154.0: 1.18, 111.0: 5.24, 60.1: 12.1, 90.1: 11.5, 99.9: 8.03, 163.0: 0.91, 146.0: 1.52, 62.9: 18.7, 172.0: 0.712, 45.0: 0.263, 47.3: 0.47, 51.5: 1.34, 180.0: 0.592, 57.1: 4.89, 54.3: 2.35, 55.1: 2.84, 56.6: 4.36, 57.9: 6.41, 58.4: 7.46, 59.6: 10.6, 60.6: 14.2, 61.2: 15.2, 62.7: 18.3, 53.0: 1.86, 64.7: 19.8, 67.3: 19.8, 68.3: 19.3, 69.4: 18.8, 70.4: 18.3, 71.5: 17.8, 72.8: 17.3, 74.0: 16.8, 75.6: 16.4, 77.2: 15.9, 78.2: 15.4, 79.7: 14.9, 56.1: 3.88, 82.9: 13.9, 58.6: 7.98, 84.7: 13.4, 86.0: 13.0, 57.4: 5.41, 89.1: 12.0, 57.6: 5.9, 91.7: 11.0, 92.9: 10.5, 58.1: 6.97, 94.2: 9.99, 95.8: 9.52, 96.8: 9.04, 62.4: 17.8, 59.1: 8.99, 101.0: 7.59, 81.3: 14.4, 60.9: 14.7, 103.0: 7.08, 109.0: 5.66, 61.7: 16.2, 112.0: 4.74, 59.9: 11.6, 62.2: 17.2, 105.0: 6.61, 55.8: 3.39, 122.0: 3.32, 63.7: 19.3, 124.0: 3.12}

    lookup = make_lookup(data)
    cprobs = sorted(lookup.keys())

    num = draw_number(cprobs, lookup)
    print(num)

if __name__ == "__main__":
    main()

它也非常快-因此,如果您需要大量样本,则不会出现任何问题。