What is the role of keepdims in Numpy (Python)?

后端 未结 4 1234
北海茫月
北海茫月 2021-02-10 10:26

When I use np.sum, I encountered a parameter called keepdims. After looking up the docs, I still cannot understand the meaning of keepdims

4条回答
  •  没有蜡笔的小新
    2021-02-10 11:15

    keepdims = true; In this case your dimensions of the array(Matrix) will be saved. That means the result you get is "broadcasted" correctly against the Array you are trying to implement the methods.

    when you ignore it is just an ordinary array with no more dimensions.

    import numpy as np
    
    x = np.random.rand(4,3)
    
    #Output for below statement: (3,)
    print((np.sum(x, axis=0)).shape)
    
    #Output for below statement: (1, 3)
    print((np.sum(x, axis=0, keepdims=True)).shape)
    

提交回复
热议问题