<p dir="auto">Given an array, we can sum with and without keepdims:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; x = np.arange(20).reshape(4, 5)
&gt;&gt;&gt; print(x)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
&gt;&gt;&gt; print(np.sum(x, axis=1))
[10 35 60 85]
&gt;&gt;&gt; print(np.sum(c, axis=1, keepdims=True))
[[10]
 [35]
 [60]
 [85]]"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">20</span>).<span class="pl-en">reshape</span>(<span class="pl-c1">4</span>, <span class="pl-c1">5</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">print</span>(<span class="pl-s1">x</span>)
[[ <span class="pl-c1">0</span>  <span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">3</span>  <span class="pl-c1">4</span>]
 [ <span class="pl-c1">5</span>  <span class="pl-c1">6</span>  <span class="pl-c1">7</span>  <span class="pl-c1">8</span>  <span class="pl-c1">9</span>]
 [<span class="pl-c1">10</span> <span class="pl-c1">11</span> <span class="pl-c1">12</span> <span class="pl-c1">13</span> <span class="pl-c1">14</span>]
 [<span class="pl-c1">15</span> <span class="pl-c1">16</span> <span class="pl-c1">17</span> <span class="pl-c1">18</span> <span class="pl-c1">19</span>]]
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">print</span>(<span class="pl-s1">np</span>.<span class="pl-en">sum</span>(<span class="pl-s1">x</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>))
[<span class="pl-c1">10</span> <span class="pl-c1">35</span> <span class="pl-c1">60</span> <span class="pl-c1">85</span>]
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">print</span>(<span class="pl-s1">np</span>.<span class="pl-en">sum</span>(<span class="pl-s1">c</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>, <span class="pl-s1">keepdims</span><span class="pl-c1">=</span><span class="pl-c1">True</span>))
[[<span class="pl-c1">10</span>]
 [<span class="pl-c1">35</span>]
 [<span class="pl-c1">60</span>]
 [<span class="pl-c1">85</span>]]</pre></div>
<p dir="auto">But if we try the same thing with a masked array (even one without a mask):</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; xm = np.ma.array(x)
&gt;&gt;&gt; print(xm)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
&gt;&gt;&gt; print(np.sum(xm, axis=1))
[10 35 60 85]
&gt;&gt;&gt; print(np.sum(xm, axis=1, keepdims=True))
[10 35 60 85]"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">xm</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">ma</span>.<span class="pl-en">array</span>(<span class="pl-s1">x</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">print</span>(<span class="pl-s1">xm</span>)
[[ <span class="pl-c1">0</span>  <span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">3</span>  <span class="pl-c1">4</span>]
 [ <span class="pl-c1">5</span>  <span class="pl-c1">6</span>  <span class="pl-c1">7</span>  <span class="pl-c1">8</span>  <span class="pl-c1">9</span>]
 [<span class="pl-c1">10</span> <span class="pl-c1">11</span> <span class="pl-c1">12</span> <span class="pl-c1">13</span> <span class="pl-c1">14</span>]
 [<span class="pl-c1">15</span> <span class="pl-c1">16</span> <span class="pl-c1">17</span> <span class="pl-c1">18</span> <span class="pl-c1">19</span>]]
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">print</span>(<span class="pl-s1">np</span>.<span class="pl-en">sum</span>(<span class="pl-s1">xm</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>))
[<span class="pl-c1">10</span> <span class="pl-c1">35</span> <span class="pl-c1">60</span> <span class="pl-c1">85</span>]
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-en">print</span>(<span class="pl-s1">np</span>.<span class="pl-en">sum</span>(<span class="pl-s1">xm</span>, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>, <span class="pl-s1">keepdims</span><span class="pl-c1">=</span><span class="pl-c1">True</span>))
[<span class="pl-c1">10</span> <span class="pl-c1">35</span> <span class="pl-c1">60</span> <span class="pl-c1">85</span>]</pre></div>
<p dir="auto">Wat.</p>
<p dir="auto">Furthermore, if we try <code class="notranslate">xm.sum(axis=1, keepdims=True)</code> we get:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TypeError: sum() got an unexpected keyword argument 'keepdims'"><pre class="notranslate"><code class="notranslate">TypeError: sum() got an unexpected keyword argument 'keepdims'
</code></pre></div>
<p dir="auto">For completeness:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.__version__
'1.10.1'"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-s1">__version__</span>
<span class="pl-s">'1.10.1'</span></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; np.sum([1, 2], keepdims=True)
array([3])
&gt;&gt;&gt; ma.sum([1, 2], keepdims=True)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;numpy/ma/core.py&quot;, line 6025, in __call__
    return method(MaskedArray(a), *args, **params)
TypeError: sum() got an unexpected keyword argument 'keepdims'"><pre class="notranslate"><code class="notranslate">&gt;&gt;&gt; np.sum([1, 2], keepdims=True)
array([3])
&gt;&gt;&gt; ma.sum([1, 2], keepdims=True)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "numpy/ma/core.py", line 6025, in __call__
    return method(MaskedArray(a), *args, **params)
TypeError: sum() got an unexpected keyword argument 'keepdims'
</code></pre></div>