🌐
GitHub
github.com › dask › dask › issues › 5029
sum() got an unexpected keyword argument 'keepdims' · Issue #5029 · dask/dask
June 30, 2019 - 81 if dtype is not None: ---> 82 return reduction(axis=axis, dtype=dtype, out=out, **passkwargs) 83 else: 84 return reduction(axis=axis, out=out, **passkwargs) TypeError: sum() got an unexpected keyword argument 'keepdims'
Author: dask
🌐
GitHub
github.com › pandas-dev › pandas › issues › 34520
BUG: Mixed DataFrame with Extension Array incorrect aggregation · Issue #34520 · pandas-dev/pandas
June 1, 2020 - BugExtensionArrayExtending pandas with custom dtypes or arrays.Extending pandas with custom dtypes or arrays.RegressionFunctionality that used to work in a prior pandas versionFunctionality that used to work in a prior pandas version ... >>> df = pd.DataFrame([["a", 1]], columns=list("ab")) >>> df.sum() a a b 1 dtype: object >>> df.astype({"b": "Int64"}).sum() a a dtype: object
Author: pandas-dev
🌐
GitHub
github.com › pandas-dev › pandas › issues › 16215
keepdims fails when taking mean · Issue #16215 · pandas-dev/pandas
May 3, 2017 - import pandas as pd import numpy as np print(np.mean(np.zeros((30, 30)), axis=0, keepdims=True).shape, pd.DataFrame(np.zeros((30, 30))).mean( axis=0, keepdims=True).shape, np.mean(pd.DataFrame(np.zeros((30, 30))), axis=0, keepdims=True).shape)
Author: pandas-dev
🌐
GitHub
github.com › theislab › diffxpy › issues › 203
TypeError: sum() got an unexpected keyword argument 'keepdims' · Issue #203 · theislab/diffxpy
July 14, 2021 - Anyone know how to solve this? Seems like it's a dask / pandas conflict. I'm on ubuntu 20.04, pandas 1.3, and whatever the newest dask is (2021.7.0)? Seems similar to this dask issue. trying: test = de.test.pairwise( data=qci, grouping="...
Author: theislab
🌐
Pandas
pandas.pydata.org › docs › whatsnew › v2.1.0.html
What’s new in 2.1.0 (Aug 30, 2023) — pandas 3.0.5 documentation
In [1]: df = pd.DataFrame({"a": [1, 1, 2, 1], "b": [np.nan, 2.0, 3.0, 4.0]}, dtype="Int64") In [2]: df.sum() Out[2]: a 5 b 9 dtype: Int64 In [3]: df = df.astype("int64[pyarrow]") In [4]: df.sum() Out[4]: a 5 b 9 dtype: int64[pyarrow] Notice that the dtype is now a masked dtype and PyArrow dtype, respectively, while previously it was a NumPy integer dtype. To allow DataFrame reductions to preserve extension dtypes, ExtensionArray._reduce() has gotten a new keyword parameter keepdims.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.1 › whatsnew › v2.1.0.html
What’s new in 2.1.0 (Aug 30, 2023) — pandas 2.1.4 documentation
In [1]: df = pd.DataFrame({"a": [1, 1, 2, 1], "b": [np.nan, 2.0, 3.0, 4.0]}, dtype="Int64") In [2]: df.sum() Out[2]: a 5 b 9 dtype: Int64 In [3]: df = df.astype("int64[pyarrow]") In [4]: df.sum() Out[4]: a 5 b 9 dtype: int64[pyarrow] Notice that the dtype is now a masked dtype and PyArrow dtype, respectively, while previously it was a NumPy integer dtype. To allow DataFrame reductions to preserve extension dtypes, ExtensionArray._reduce() has gotten a new keyword parameter keepdims.
🌐
IncludeHelp
includehelp.com › python › what-does-the-keepdims-parameter-do-with-numpy-sum-function.aspx
Python - What does the 'keepdims' parameter do with numpy.sum() function?
If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however, any non-default value will be. If the sub-class' method does not implement keepdims any exceptions will be raised.
Top answer
1 of 1
10

Note that under the keepdims argument in the docs for numpy.sum() it states:

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 input array.
If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-classes sum method does not implement keepdims any exceptions will be raised.

So it states here that if you're using a sub-class of numpy.ndarray, then you'll get this error if the corresponding sum function for the sub-class hasn't been defined with it.

Notice that in your error it references line 1812 in numpy/core/fromnumeric.py. Take a look at that in context in the actual numpy 1.12.x source:

kwargs = {}
if keepdims is not np._NoValue:
    kwargs['keepdims'] = keepdims
if isinstance(a, _gentype):
    res = _sum_(a)
    if out is not None:
        out[...] = res
        return out
    return res
if type(a) is not mu.ndarray:
    try:
        sum = a.sum
    except AttributeError:
        pass
    else:
        return sum(axis=axis, dtype=dtype, out=out, **kwargs)
return _methods._sum(a, axis=axis, dtype=dtype,
                     out=out, **kwargs)

Two things are important to note here: the sum function did parse your keepdims variable, since it pulled it above line 1812 and tried to put it in another function, so you know the error wasn't the way you used the variable. The other important thing is that the line 1812 which you're erroring on is only executing if type(a) is not mu.ndarray, i.e., if you're using a different class than ndarray. And this is exactly what the documentation is referencing. If you have a different class, then they need to implement this sum function with the keepdims argument, and if they don't it will raise an error.

Other classes like np.matrix for example will have a different sum function, and it seems that, even in numpy 1.13.x, sum for np.matrix types does not support the keepdim argument (because in numpy, matrices always are 2D). For example, it works fine with a np.array:

>>> import numpy as np
>>> A = np.eye(4)
>>> A
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])
>>> np.sum(A, axis=1, keepdims=True)
array([[ 1.],
       [ 1.],
       [ 1.],
       [ 1.]])

But with a np.matrix, it doesn't:

>>> import numpy.matlib
>>> B = np.matlib.eye(4)
>>> np.sum(B, axis=1, keepdims=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../numpy/core/fromnumeric.py", line 1832, in sum
    return sum(axis=axis, dtype=dtype, out=out, **kwargs)
TypeError: sum() got an unexpected keyword argument 'keepdims'

But, most array/matrix type objects can be easily cast to an array in numpy with np.array(<object>), and this should solve the problem for most sub-classed objects in numpy and likely your problem. You can also simply wrap the result back into a np.matrix if you need to.

>>> B = np.matlib.eye(4)
>>> B = np.array(B)
>>> np.sum(B, axis=1, keepdims=True)
array([[ 1.],
       [ 1.],
       [ 1.],
       [ 1.]])

However, if your class of object is a np.matrix type, then the keepdims argument is pointless. Matrices are always 2D, so the sum function won't reduce a dimension, and thus the argument wouldn't do anything. This is why it isn't implemented for matrices.

🌐
DeepLearning.AI
community.deeplearning.ai › course q&a › deep learning specialization › sequence models
Dba = np.sum(dtanh,axis=1 or keepdims = True) - Sequence Models - DeepLearning.AI
March 27, 2023 - Hi friend and mentor, In the Exercise 5 - rnn_cell_backward of Building_a_Recurrent_Neural_Network_Step_by_Step, the hint said very clear that " To calculate dba , the ‘batch’ above is a sum across all ‘m’ examples (axis= 1). Note that you should use the keepdims = True option."
Top answer
1 of 4
46

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.

2 of 4
5

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
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.sum.html
numpy.sum — NumPy v2.5 Manual
If the default value is passed, then keepdims will not be passed through to the sum method of sub-classes of ndarray, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised.
🌐
GitHub
github.com › cupy › cupy › issues › 9026
spmatrix.sum() got an unexpected keyword argument 'keepdims' · Issue #9026 · cupy/cupy
March 11, 2025 - 83 if dtype is not None: ---> 84 return reduction(axis=axis, dtype=dtype, out=out, **passkwargs) 85 else: 86 return reduction(axis=axis, out=out, **passkwargs) TypeError: spmatrix.sum() got an unexpected keyword argument 'keepdims'
Author: cupy
Top answer
1 of 2
102

@Ney @hpaulj is correct, you need to experiment, but I suspect you don't realize that summation for some arrays can occur along axes. Observe the following which reading the documentation

>>> a
array([[0, 0, 0],
       [0, 1, 0],
       [0, 2, 0],
       [1, 0, 0],
       [1, 1, 0]])
>>> np.sum(a, keepdims=True)
array([[6]])
>>> np.sum(a, keepdims=False)
6
>>> np.sum(a, axis=1, keepdims=True)
array([[0],
       [1],
       [2],
       [1],
       [2]])
>>> np.sum(a, axis=1, keepdims=False)
array([0, 1, 2, 1, 2])
>>> np.sum(a, axis=0, keepdims=True)
array([[2, 4, 0]])
>>> np.sum(a, axis=0, keepdims=False)
array([2, 4, 0])

You will notice that if you don't specify an axis (1st two examples), the numerical result is the same, but the keepdims = True returned a 2D array with the number 6, whereas, the second incarnation returned a scalar. Similarly, when summing along axis 1 (across rows), a 2D array is returned again when keepdims = True. The last example, along axis 0 (down columns), shows a similar characteristic... dimensions are kept when keepdims = True.
Studying axes and their properties is critical to a full understanding of the power of NumPy when dealing with multidimensional data.

2 of 2
9

An example showing keepdims in action when working with higher dimensional arrays. Let's see how the shape of the array changes as we do different reductions:

import numpy as np
a = np.random.rand(2,3,4)
a.shape
# => (2, 3, 4)
# Note: axis=0 refers to the first dimension of size 2
#       axis=1 refers to the second dimension of size 3
#       axis=2 refers to the third dimension of size 4

a.sum(axis=0).shape
# => (3, 4)
# Simple sum over the first dimension, we "lose" that dimension 
# because we did an aggregation (sum) over it

a.sum(axis=0, keepdims=True).shape
# => (1, 3, 4)
# Same sum over the first dimension, but instead of "loosing" that 
# dimension, it becomes 1.

a.sum(axis=(0,2)).shape
# => (3,)
# Here we "lose" two dimensions

a.sum(axis=(0,2), keepdims=True).shape
# => (1, 3, 1)
# Here the two dimensions become 1 respectively
🌐
Sharp Sight
sharpsight.ai › blog › numpy-sum
How to Use the Numpy Sum Function - Sharp Sight
February 6, 2024 - Note that the out parameter is optional. keepdims (optional) The keepdims parameter enables you to keep the number of dimensions of the output the same as the input.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 11857
Sum a panel along multiple dimensions · Issue #11857 · pandas-dev/pandas
December 17, 2015 - numpy's sum() function allows specifying a tuple of axis values for higher-dimensional arrays, but when it is called this way on a Pandas Panel or Panel4D it raises a ValueError: >>> import numpy as np >>> import pandas as pd >>> a = np....
Author: pandas-dev
🌐
Pandas
pandas.pydata.org › docs › dev › whatsnew › v2.1.0.html
What’s new in 2.1.0 (Aug 30, 2023) — pandas 3.1.0.dev0 documentation
Calling ExtensionArray._reduce() with keepdims=True should return an array of length 1 along the reduction axis. In order to maintain backward compatibility, the parameter is not required, but will it become required in the future. If the parameter is not found in the signature, DataFrame reductions can not preserve extension dtypes.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Meaning of the axis parameter (0, 1, -1) | note.nkmk.me
January 18, 2024 - In operations involving the output array and the input array (or any array with the same shape as the input), using axis=1 with default setting may result in an error. However, keepdims=True ensures proper broadcasting. # print(a + np.sum(a, axis=1)) # ValueError: operands could not be broadcast together with shapes (3,4) (3,) print(a + np.sum(a, axis=1, keepdims=True)) # [[5 5 5 5] # [5 5 5 5] # [5 5 5 5]]
🌐
GitHub
github.com › numpy › numpy › issues › 4537
ma.sum() does not support keepdims argument · Issue #4537 · numpy/numpy
March 23, 2014 - >>> np.sum([1, 2], keepdims=True) array([3]) >>> ma.sum([1, 2], keepdims=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "numpy/ma/core.py", line 6025, in __call__ return method(MaskedArray(a), *args, **params) TypeError: sum() got an unexpected keyword argument 'keepdims'
Author: numpy
🌐
GitHub
github.com › numpy › numpy › issues › 12559
keepdims should be set by default to True in sum · Issue #12559 · numpy/numpy
December 16, 2018 - Instead sum returns a rank 1 matrix. The programmer instead needs to know to set keepdims to True. If the programmer forgets this, then rank 1 matrices cause silent bugs since they don't function as expected.
Author: numpy