Numpy: Divide each row by a vector element

前端 未结 5 479
孤街浪徒
孤街浪徒 2020-11-29 17:59

Suppose I have a numpy array:

data = np.array([[1,1,1],[2,2,2],[3,3,3]])

and I have a corresponding \"vector:\"

vector = np         


        
5条回答
  •  执笔经年
    2020-11-29 18:31

    As has been mentioned, slicing with None or with np.newaxes is a great way to do this. Another alternative is to use transposes and broadcasting, as in

    (data.T - vector).T
    

    and

    (data.T / vector).T
    

    For higher dimensional arrays you may want to use the swapaxes method of NumPy arrays or the NumPy rollaxis function. There really are a lot of ways to do this.

    For a fuller explanation of broadcasting, see http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

提交回复
热议问题