Getting the max, min and last index of formatted arrays Numpy Python
Solution 1:
Please review list/array slicing in numpy (e.g. list slicing or array slicing). First off, out_arr
should be initialized with three rows. Then, the min and max need to be computed across a slice containing all values up to the v_ind
value:
val_ind = []
out_ind = []
for ind, cnt in enumerate(np.bincount(digit_vals)):
if cnt > 0:
val_ind.append(cnt-1)
out_ind.append(ind)
out_arr = np.zeros((3, np.max(digit_vals)+1))
for r_ind, (v_ind, o_ind) in enumerate(zip(val_ind, out_ind)):
out_arr[0, o_ind] = vals[r_ind][v_ind]
out_arr[1, o_ind] = np.max(vals[r_ind][:v_ind+1])
out_arr[2, o_ind] = np.min(vals[r_ind][:v_ind+1])
A slice doesn't return the last value so that is why you need the v_ind+1
, as noted in the links above.