mardi 2 juin 2020

Generate random number within defined range, using loops

I have a csv, where the first row is the minimum estimated growth rate of a species and the second row is the maximum estimated growth rate.

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

I am trying to create a new dataframe that randomly generates the growth rate per species 10 times, within the bounds of the min and max defined above. I've been using np.random.uniform as follows but my code isn't working correctly. Here's my attempt so far:

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)

The shape of the output is correct (see below), but the values are not. For example, if I have a min and max of 0, the code still generates values above 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

How can I do this?




Aucun commentaire:

Enregistrer un commentaire