Skip to content Skip to sidebar Skip to footer

Time-efficient Way To Replace Numpy Entries

I have multiple arrays of the following kind: import numpy as np orig_arr = np.full(shape=(5,10), fill_value=1) #only an example, actual entries different Every entry in the arra

Solution 1:

You need to avoid every iterable object that is not numpy array itself as well as Python level iterations. So you might like to store values of dictionary in separate array and then use fancy indexing:

goal_arr = np.empty(shape=(orig_arr.shape[0], orig_arr.shape[1], 10), dtype=float)
a = np.array(list(toy_dict.values())) #do not know if it can be optimized
idx = np.indices(orig_arr.shape)
goal_arr[idx[0], idx[1]] = a[orig_arr[idx[0], idx[1]]]

You can see here that creation of goal_arr is must-do but I've used np.empty instead of np.full since it's more efficient.

Remark: this way works only if list(toy_dict.keys()) is a list of the form [0, 1, 2...]. In other cases you need to think of how to apply a map toy_dict.keys() -> [0, 1, ...] on orig_arr. I've found this task quite difficult so leaving it out of scope.

Usage

goal_arr = np.empty(shape=(orig_arr.shape[0], orig_arr.shape[1], 10), dtype=float)
toy_dict = {k:np.random.randint(10, size = 10) for k in range(9)}

orig_arr = np.random.randint(0, 8, size=(2,3))
a = np.array(list(toy_dict.values())) #do not know if it can be optimized
idx = np.indices(orig_arr.shape)
goal_arr[idx[0], idx[1]] = a[orig_arr[idx[0], idx[1]]]

Sample run:

print('orig_arr:\n', orig_arr)
print('toy_dict:\n', toy_dict)
print('goal arr:\n', goal_arr)
---------------------------------
orig_arr:
 [[7 3 0]
 [1 3 2]]
toy_dict:
 {0: array([8, 7, 3, 4, 8, 8, 6, 6, 5, 2]), 1: array([7, 2, 4, 7, 5, 5, 6, 8, 6, 5]), 2: array([5, 3, 4, 7, 6, 8, 6, 4, 4, 7]), 3: array([9, 2, 5, 1, 1, 8, 1, 1, 7, 0]), 4: array([9, 6, 7, 2, 7, 2, 4, 4, 5, 8]), 5: array([4, 9, 5, 2, 8, 3, 9, 4, 7, 9]), 6: array([6, 0, 7, 8, 5, 4, 7, 8, 8, 2]), 7: array([6, 5, 9, 3, 6, 2, 0, 2, 3, 2]), 8: array([5, 3, 9, 3, 2, 3, 0, 8, 3, 5])}
goal arr:
 [[[6. 5. 9. 3. 6. 2. 0. 2. 3. 2.]
  [9. 2. 5. 1. 1. 8. 1. 1. 7. 0.]
  [8. 7. 3. 4. 8. 8. 6. 6. 5. 2.]][[7. 2. 4. 7. 5. 5. 6. 8. 6. 5.]
  [9. 2. 5. 1. 1. 8. 1. 1. 7. 0.]
  [5. 3. 4. 7. 6. 8. 6. 4. 4. 7.]]]

You might also find this excellent tutorial about advanced indexing helpful.

Post a Comment for "Time-efficient Way To Replace Numpy Entries"