【问题标题】:Generate random number within defined range, using loops使用循环生成定义范围内的随机数
【发布时间】:2020-06-02 15:54:50
【问题描述】:

我有一个 csv,其中第一行是一个物种的最小估计增长率,第二行是最大估计增长率。​​p>

   herbivores  youngScrub   sapling  matureTree  
0       0.001       0.001     0.045       0.001      
1       0.50        0.30      0.990       0.990   

我正在尝试创建一个新的数据框,在上面定义的最小值和最大值的范围内随机生成每个物种的增长率 10 次。我一直在使用 np.random.uniform 如下,但我的代码无法正常工作。到目前为止,这是我的尝试:

growthRates_csv = pd.read_csv...


# select the minimum and maximum values in the csv
min_growthRate = growthRates_csv.iloc[0:]
max_growthRate = growthRates_csv.iloc[1:]

# put these into a new dataframe and generate x values in that range
growthRates = pd.DataFrame([np.random.uniform(minG, maxG, size=(10)) for minG,maxG in zip(min_growthRate, max_growthRate)], index=species)

输出的形状是正确的(见下文),但值不正确。例如,如果我的最小值和最大值为 0,则代码仍会生成大于 0 的值。

   herbivores  youngScrub  matureScrub   sapling  matureTree  grassHerbs
0    0.830269    0.031925     0.781608  0.810162    0.810280    0.144622
1    0.845585    0.648186     0.091188  0.254415    0.156356    0.918178
2    0.615185    0.403556     0.824594  0.878639    0.899520    0.524859
3    0.841222    0.866065     0.926736  0.667068    0.504005    0.405044
4    0.598357    0.617152     0.364813  0.703951    0.188655    0.705077
5    0.110041    0.957693     0.251842  0.568105    0.728227    0.018699
6    0.888947    0.883742     0.580802  0.016060    0.155485    0.484473
7    0.116143    0.947701     0.238723  0.937753    0.415934    0.797138
8    0.733007    0.856673     0.274538  0.669954    0.505984    0.905555
9    0.012286    0.796349     0.539737  0.643185    0.375249    0.628799

我该怎么做?

【问题讨论】:

  • 那么输出应该是什么样的,你的是什么样的('不工作'是什么意思?

标签: python loops dataframe random


【解决方案1】:

JI 将使用np.random.uniformnp.random.rand0,1 中生成随机数据。然后根据minmax缩放它们:

num_samples = 10
s = pd.DataFrame(np.random.rand(num_samples,df.shape[1]),  # rand is shorthand for uniform
                 columns=df.columns)

# max-min
ranges = df.iloc[1] - df.iloc[0]

# rescale:
s = (s + df.iloc[0]) * ranges

输出:

   herbivores  youngScrub   sapling  matureTree
0    0.418684    0.034717  0.913757    0.275574
1    0.236334    0.283547  0.676601    0.117145
2    0.201578    0.120658  0.706759    0.476228
3    0.296190    0.286075  0.970215    0.220551
4    0.149709    0.178456  0.255563    0.532362
5    0.384640    0.100779  0.717650    0.785947
6    0.321665    0.168369  0.532486    0.606925
7    0.068008    0.241038  0.378835    0.683488
8    0.387329    0.109531  0.595569    0.113893
9    0.451120    0.285593  0.918895    0.415743

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 2010-12-24
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    相关资源
    最近更新 更多