python - Normalize slices of a ndarray -
i have 3 columns array. first column of array have values between 1 , 10. need extract lines first column 1 , normalize third column of slice of array. repeat same thing rows first column equal 2 etc.
if run code, leaves array unchanged:
for u in np.unique(x[:,0]): mask= x[:, 0] == u x[mask][:,2]=x[mask][:,2]/np.sum((x[mask][:,2]))
if run other slice of code, see r (i placed print r in loop) work want. point original array x unchanged.
for u in np.unique(x[:,0]): r = x[x[:, 0] == u] r[:,2]=r[:,2]/np.sum((x[x[:,0]==u][:,2]))
why that? doing wrong???
here's alternative vectorized approach performance in mind solve problem using np.unique
, np.bincount
-
tags = np.unique(x[:,0], return_inverse=1)[1] x[:,2] /= np.bincount(tags, x[:,2])[tags]
to further boost performance, 1 can avoid use of np.unique
, directly compute equivalent of np.bincount(tags, xc[:,2])
, while making use of fact numbers in first column between 1
, 10
, -
np.bincount(xc[:,0].astype(int), xc[:,2], minlength=11)[1:]
to replace tags
, use first column, -
tags = xc[:,0].astype(int)-1
Comments
Post a Comment