1

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?

1
  • so what should the output look like and what does yours look like (what does 'not working' mean? Commented Jun 2, 2020 at 15:57

1 Answer 1

1

JI would generate the random data in 0,1 with either np.random.uniform or np.random.rand. Then scale them according to the min and max:

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

Output:

   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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.