Numpy, python: automatically expand dimensions of arrays when broadcasting

后端 未结 2 935
再見小時候
再見小時候 2021-02-19 21:36

Consider the following exercise in Numpy array broadcasting.

import numpy as np
v = np.array([[1.0, 2.0]]).T # column array

A2 = np.random.randn(2,10) # 2D arra         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-19 22:22

    It's ugly, but this will work:

    (v.T * A3.T).T
    

    If you don't give it any arguments, transposing reverses the shape tuple, so you can now rely on the broadcasting rules to do their magic. The last transpose returns everything to the right order.

提交回复
热议问题