python - numpy broadcast from first dimension -
in numpy, there easy way broadcast 2 arrays of dimensions e.g. (x,y)
, (x,y,z)
? numpy broadcasting typically matches dimensions last dimension, usual broadcasting not work (it require first array have dimension (y,z)
).
background: i'm working images, of rgb (shape (h,w,3)
) , of grayscale (shape (h,w)
). generate alpha masks of shape (h,w)
, , want apply mask image via mask * im
. doesn't work because of above-mentioned problem, end having e.g.
mask = mask.reshape(mask.shape + (1,) * (len(im.shape) - len(mask.shape)))
which ugly. other parts of code operations vectors , matrices, run same issue: fails trying execute m + v
m
has shape (x,y)
, v
has shape (x,)
. it's possible use e.g. atleast_3d
, have remember how many dimensions wanted.
how use transpose:
(a.t + c.t).t
Comments
Post a Comment