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

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

keepdims: bool, optional

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.

I will appreciate it if anyone can make some sense of this with a simple example.

4 Answers

Consider a small 2d array:

In [180]: A=np.arange(12).reshape(3,4) In [181]: A Out[181]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) 

Sum across rows; the result is a (3,) array

In [182]: A.sum(axis=1) Out[182]: array([ 6, 22, 38]) 

But to sum (or divide) A by the sum requires reshaping

In [183]: A-A.sum(axis=1) ... ValueError: operands could not be broadcast together with shapes (3,4) (3,) In [184]: A-A.sum(axis=1)[:,None] # turn sum into (3,1) Out[184]: array([[ -6, -5, -4, -3], [-18, -17, -16, -15], [-30, -29, -28, -27]]) 

If I use keepdims, "the result will broadcast correctly against" A.

In [185]: A.sum(axis=1, keepdims=True) # (3,1) array Out[185]: array([[ 6], [22], [38]]) In [186]: A-A.sum(axis=1, keepdims=True) Out[186]: array([[ -6, -5, -4, -3], [-18, -17, -16, -15], [-30, -29, -28, -27]]) 

If I sum the other way, I don't need the keepdims. Broadcasting this sum is automatic: A.sum(axis=0)[None,:]. But there's no harm in using keepdims.

In [190]: A.sum(axis=0) Out[190]: array([12, 15, 18, 21]) # (4,) In [191]: A-A.sum(axis=0) Out[191]: array([[-12, -14, -16, -18], [ -8, -10, -12, -14], [ -4, -6, -8, -10]]) 

If you prefer, these actions might make more sense with np.mean, normalizing the array over columns or rows. In any case it can simplify further math between the original array and the sum/mean.

3

You can keep the dimension with "keepdims=True" if you sum a matrix For example:

import numpy as np x = np.array([[1,2,3],[4,5,6]]) x.shape # (2, 3) np.sum(x, keepdims=True).shape # (1, 1) np.sum(x, keepdims=True) # array([[21]]) <---the reault is still a 1x1 array np.sum(x, keepdims=False).shape # () np.sum(x, keepdims=False) # 21 <--- the result is an integer with no dimesion 

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) 

keepdims = True, is used for matching dimensions of matrix. If we left this False then it will show error of dimension error. You can see it while calculating softmax entropy

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like