Replace double for loop (with indexing into other arrays) over numpy array

Here’s the example I’m working on:

 processed_data = np.empty_like(data)
 min_per_col = np.amin(data, axis=0) # axis0 for col, axis1 for row
 max_per_col = np.amax(data, axis=0) # axis0 for col, axis1 for row
 for row_idx, row in enumerate(data):
     for col_idx, val in enumerate(row):
         processed_data[row_idx][col_idx] = (val - min_per_col[col_idx]) / (max_per_col[col_idx] - min_per_col[col_idx])

data is defined as a 2d numpy array. I am essentially trying to perform some operation on each element in data using the relevant values in min_per_col and max_per_col.

I can’t seem to figure out the approach to take. It seems like from these posts the answer is to reshape the arrays so that broadcasting works.

Please let me know if there’s anything else that would be helpful to include for this post! Thank you.

Source: Python Questions

LEAVE A COMMENT