I've been working on a matrix normalization problem, stated as:
Given a matrix M, normalize its elements such that each element is divided with the corresponding column sum if element is not 0.
cwsums = np.sum(class_matrix,axis=1)
cwsums = np.reciprocal(cwsums.astype(np.float32))
cwsums[cwsums == np.inf] = 0
## this is the problem
final_matrix = np.multiply(final_matrix, cwsums)
I can construct a reciprocal mask, which I would like to apply accross the matrix, as an elementwise product, yet I can't seem to get it right. Thank you!
EDIT: what I have written seems to work, but it is slowcheck again, it seems to be row-wise broadcast, not columnwise.