python - Randomly grow values in a NumPy Array -


i have program takes large numpy arrays and, based on outside data, grows them adding 1 randomly selected cells until array's sum equal outside data. simplified , smaller version looks like:

import numpy np my_array = np.random.random_integers(0, 100, [100, 100]) ## creating sample version of array, getting it's sum: np.sum(my_array) 499097 

so, supposing want grow array until sum 1,000,000, , want repeatedly selecting random cell , adding 1 until hit sum, i'm doing like:

diff = 1000000 - np.sum(my_array) counter = 0 while counter < diff:     row = random.randrange(0,99)     col = random.randrange(0,99)     coordinate = [row, col]     my_array[coord] += 1     counter += 1 

where row/col combine return random cell in array, , cell grown 1. repeats until number of times has added 1 random cell == difference between original array's sum , target sum (1,000,000).

however, when check result after running - sum off. in case after running same numbers above:

np.sum(my_array) 99667203 

i can't figure out accounting massive difference. , there more pythonic way go this?

replace my_array[coord] my_array[row][col]. method chose 2 random integers , added 1 every entry in rows corresponding both integers.

basically had minor misunderstanding of how numpy indexes arrays.

edit: make clearer. code posted chose 2 numbers, 30 , 45, , added 1 100 entries of row 30 , 100 entries of row 45.

from expect total sum 100,679,697 = 200*(1,000,000 - 499,097) + 499,097

however when random integers identical (say, 45 , 45), 1 added every entry of column 45, not 2, in case sum jumps 100.


Comments