简体   繁体   中英

Generate a dataset with only 6 decimal numbers

Is there a way to generate a (random) dataset that is filled with values having 6 decimals and 1 number before the decimal separator?

So for example like this:

 "A":[5.398811, 2.232098, 9.340909, 3.343434],
 "B":[6.436293,5.293756, 1.235937, 1.987384],
 "C": [3.572831, 3.826355, 3.827264, 3.257321]

I found that round(random.uniform(33.33, 66.66), 2) returns a random float number up to 2 decimal places. However, I don't want a dataframe filled "up to" 2 decimal places but a dataframe filled with only 6 decimal places. I would like to have about 1000 rows and 100 columns.

EDIT: It would also be nice to not have any 0's or 9's in any of the decimals. This because I am looking into rounding decimals. When rounding 1.999999 to 5 decimals, one wil get 2.00000 which is 2. Which then will not give a reliable rounding result. Don't know to what extend that's actually feasible.

You can use numpy.random.uniform for efficiency, then convert to dictionary:

import numpy as np
col,row = (10,20)  # (100, 1000) in your case
out = dict(enumerate(np.random.uniform(0,10,size=col*row)
                       .round(6).reshape(row,col).tolist()))

print(out)

output:

{0: [5.488135, 7.151894, 6.027634, 5.448832, 4.236548, 6.458941, 4.375872, 8.91773, 9.636628, 3.834415],
 1: [7.91725, 5.288949, 5.680446, 9.255966, 0.710361, 0.871293, 0.202184, 8.326198, 7.781568, 8.700121],
 2: [9.786183, 7.991586, 4.614794, 7.805292, 1.182744, 6.39921, 1.433533, 9.446689, 5.218483, 4.146619],
...
 19: [3.982211, 2.098437, 1.86193, 9.443724, 7.395508, 4.904588, 2.274146, 2.543565, 0.580292, 4.344166],
}

NB. note that the numbers will be UP TO 6 decimal digits (eg, 0.123400 will be shown as 0.1234, forcing otherwise would create a non-random bias

pure python version (less efficient):

import random
out = {i: [round(random.uniform(0, 10), 6) for j in range(100)]
       for i in range(1000)}

exactly 6 digits

You can check if the rounded number has a zero on the 6th decimal place, and in this case add an arbitrary number. Here is an example, initial dataset:

np.random.seed(0) # for reproducibility
a = np.random.uniform(0, 10, size=20).round(6)

array([5.488135, 7.151894, 6.027634, 5.448832, 4.236548, 6.458941,
       4.375872, 8.91773 , 9.636628, 3.834415, 7.91725 , 5.288949,
       5.680446, 9.255966, 0.710361, 0.871293, 0.202184, 8.326198,
       7.781568, 8.700121])

With correction:

np.random.seed(0) # for reproducibility
a = np.random.uniform(0, 10, size=20).round(6)
# identify numbers ending in 0
mask = (a*1e6).astype(int)%10==0
# add a terminal 1
a[mask] += 1e-6
a

array([5.488135, 7.151894, 6.027634, 5.448832, 4.236548, 6.458941,
       4.375872, 8.917731, 9.636628, 3.834415, 7.917251, 5.288949,
       5.680446, 9.255966, 0.710361, 0.871293, 0.202184, 8.326198,
       7.781568, 8.700121])

This works by multiplying by 1e6 as integer and getting the remainder of division by 10:

(a*1e6).astype(int)%10

array([5, 4, 4, 2, 8, 1, 2, 0, 8, 5, 0, 9, 6, 6, 1, 3, 4, 8, 8, 1])

example with DataFrame

import numpy as np
col,row = (4,5)  # (100, 1000) in your case
a = np.random.uniform(0,10,size=col*row).round(6).reshape(row,col)
mask = (a*1e6+1).astype(int)%10<2
# add a terminal 1
a[mask] += 2e-6

df = pd.DataFrame(a)

print(df)

Output:

          0         1         2         3
0  5.488135  7.151894  6.027634  5.448832
1  4.236548  6.458941  4.375872  8.917732
2  9.636628  3.834415  7.917252  5.288951
3  5.680446  9.255966  0.710361  0.871293
4  0.202184  8.326198  7.781568  8.700121

Maybe try to generate numbers from 1,000,000 to 9,999,999 and then divide by 1,000,000. This will ensure that the numbers are always exactly 6 decimals.

Regarding the second condition, you can run a check on the number by casting to a string, something like:

if '9' in str(the_number): 
    continue
else:
    result.append(the_number)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM