I have an array indexes that for each row contains the columns that should be filled. For example:
[array([[ 2, 14098, 6824, 24207, 1215],
[ 51, 1277, 3197, 1052, 4076],......
And I have another array values containing the values that should be filled in those positions. For example:
array([[1, 7, 75, 82, 11],
[11, 5, 8, 82, 811],...
This means that for row 0, column 2 should be filled with value '1', column 14098 should be filled with value '7'... for row 1, column 51 should be filled with value '11', column 1277 should be filled with value '5'...
And a third array, a = np.zeros((100000, 100000)) that is the array to be filled given the two previous arrays.
I am using right now a nested loop to do it but I'm pretty sure that there is a better way to do it:
for row_idx in range(indexes.shape[0]):
for col_idx in range(indexes.shape[1]):
column = indexes[row_idx][col_idx]
a[row_idx][indexes[row_idx][col_idx]] = values[row_idx][col_idx]
How can I fill the array using python/numpy (fancy indexing, broadcasting...) style? What is the most memory-efficient way to do it since I have limited ram?
Thanks for your help in advance!
np.put_along_axis(a, indexes, values, 0)do what you want?