python - Append np.newaxis as needed -


i want slice array can use perform operation array of arbitrary dimension. in other words, doing following:

a = np.random.rand(5) b = np.random.rand(5,2,3,4) slicer = [slice(none)] + [none]*(len(b.shape)-1) result = b*a[slicer] 

is there syntax can use not have construct slicer?

in specific case can use np.einsum ellipsis.

result2 = np.einsum('i,i...->i...', a, b)  np.allclose(result, result2)  out[232]: true 

although, @hpaulj points out works multiplication (or division if use 1/b).

since broadcasting works other end normally, can use np.transpose twice axes in right order.

result3 = np.transpose(np.transpose(b) * a) 

but that's not general case


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -