python - How to divide matrix elements by corresponding matrix row sum in numpy? -


this question has answer here:

for example,

m= [[1,2], [7,8]] 

then want

[[1/3, 2/3], [7/15, 8/15]] 

i'm trying vectorized. 1 idea have write s = np.sum(m, axis=1); gives corresponding row sums. maybe transpose s, , copy along columns, elementwise division of m/s, seems hacky. what's right way?

use tile repeat along dimension on sum operated.

m / np.tile(np.sum(m, 1), (1, m.shape[1])) 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -