{"id":37507,"date":"2022-11-28T10:11:14","date_gmt":"2022-11-28T10:11:14","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37507"},"modified":"2023-02-16T19:56:29","modified_gmt":"2023-02-16T19:56:29","slug":"numpy-cumsum","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-cumsum","title":{"rendered":"NumPy cumsum &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on&nbsp;<strong>Numpy cumsum<\/strong>. In this tutorial, we will be learning about the NumPy&nbsp;<code>cumsum()<\/code>&nbsp;method and also seeing a lot of examples regarding the same. So let us begin!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy cumsum?<\/h2>\n\n\n\n<p>The <strong>cumulative sum<\/strong> is a sequence of partial sums of a given sequence. If <em><strong>{a, b, c, d, e, f,&#8230;..}<\/strong><\/em> is a sequence then its cumulative sum is represented as <em><strong>{a, a+b, a+b+c, a+b+c+d,&#8230;.}<\/strong><\/em>. <\/p>\n\n\n\n<p>The <code>cumsum()<\/code> method in NumPy returns the cumulative sum of the elements of the input array along the specified axis. \u00a0It can be the cumulative sum of the flattened array, the cumulative sum of the array elements along the rows or the cumulative sum of the array elements along the columns.\u00a0 <\/p>\n\n\n\n<p>We will see the examples for each of these in the upcoming section of this tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy cumsum<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.cumsum(a, axis=None, dtype=None, out=None)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Parameter<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Required\/Optional<\/strong><\/td><\/tr><tr><td>a<\/td><td>Input array.<\/td><td>Required<\/td><\/tr><tr><td>axis<\/td><td>Axis along which the cumulative sum of the array is to be calculated. It can be axis=0 i.e. along columns or axis=1 i.e. along rows or axis=None which implies that the cumulative sum of the flattened array is to be returned.<\/td><td>Optional<\/td><\/tr><tr><td>dtype (data type)<\/td><td>The data type of the array to be returned.<\/td><td>Optional<\/td><\/tr><tr><td>out<\/td><td>An alternative output array in which to place the result. It must have the same shape and length as the expected output.<\/td><td>Optional<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Returns:<\/strong><br>A new array that contains the output. If <em>out<\/em> is mentioned, then a reference to it is returned.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>Let\u2019s now get right into using the numpy.cumsum method so we can understand the outputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The cumulative sum of a single element<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = 5\nans = np.cumsum(a)\n\nprint(&quot;a =&quot;, a)\nprint(&quot;Cumulative sum =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = 5\nCumulative sum = &#x5B;5]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Numpy cumulative sum of an empty array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;]\nans = np.cumsum(a)\n\nprint(&quot;a =&quot;, a)\nprint(&quot;Cumulative sum =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;]\nCumulative sum = &#x5B;]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Numpy cumulative sum of a 1-dimensional array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;5, 3, 2, 8]\nans = np.cumsum(a)\n\nprint(&quot;a =&quot;, a)\nprint(&quot;Cumulative sum of the array =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;5, 3, 2, 8]\nCumulative sum of the array = &#x5B; 5  8 10 18]\n<\/pre><\/div>\n\n\n<p>Here, the cumulative sum is calculated as 5, 5+3, 5+3+2, 5+3+2+8 which results in 5, 8, 10, 18.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Numpy cumulative sum of a 2-dimensional array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;4, 3], &#x5B;9, 10]]\nans = np.cumsum(a)\n\nprint(&quot;a =&quot;, a)\nprint(&quot;Cumulative sum of the array =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;4, 3], &#x5B;9, 10]]\nCumulative sum of the array = &#x5B; 4  7 16 26]\n<\/pre><\/div>\n\n\n<p>In the case of a 2-dimensional array, when no axis is mentioned, the array is first flattened and then its cumulative sum is calculated. <br>In the above example, the array is first flattened as [4, 3, 9, 10] i.e. row-wise and then its cumulative sum is calculated as [4, 4+3, 4+3+9, 4+3+9+10] which results in the array [4, 7, 16, 26] which is returned by the function.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Return Numpy.cumsum() of the array as float data type<\/h3>\n\n\n\n<p>This is the same as the above examples except that here the returned values are of float data type.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;5, 3, 2, 8]\nans = np.cumsum(a, dtype=float)\n\nprint(&quot;a =&quot;, a)\nprint(&quot;Cumulative sum of the array =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;5, 3, 2, 8]\nCumulative sum of the array = &#x5B; 5.  8. 10. 18.]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Cumulative sum along the axis<\/h2>\n\n\n\n<p><strong>axis = 0<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;1, 5, 3], &#x5B;7, 10, 4]]\n# cumulative sum along axis=0\nans = np.cumsum(a, axis=0)\n\nprint(&quot;a =\\n&quot;, a)\nprint(&quot;Cumulative sum of the array =\\n&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na =\n &#x5B;&#x5B;1, 5, 3], &#x5B;7, 10, 4]]\nCumulative sum of the array =\n &#x5B;&#x5B; 1  5  3]\n &#x5B; 8 15  7]]\n<\/pre><\/div>\n\n\n<p>Here, the first row is as it is and the second row contains the cumulative sum calculated as 1+7, 5+10, and 3+4 resulting in 8, 15 and 7.<\/p>\n\n\n\n<p><strong>axis = 1<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;1, 5, 3], &#x5B;7, 10, 4]]\n# cumulative sum along axis=1\nans = np.cumsum(a, axis=1)\n\nprint(&quot;a =\\n&quot;, a)\nprint(&quot;Cumulative sum of the array =\\n&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na =\n &#x5B;&#x5B;1, 5, 3], &#x5B;7, 10, 4]]\nCumulative sum of the array =\n &#x5B;&#x5B; 1  6  9]\n &#x5B; 7 17 21]]\n<\/pre><\/div>\n\n\n<p>Here, the first column is as it is and the second column contains the cumulative sum calculated as 1+5, 7+10 resulting in 6, 17 and the third column has the cumulative sum of 1+5+3, 7+10+4 i.e. 9 and 21.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>That\u2019s all! In this tutorial, we learned about the&nbsp;<strong>Numpy cumsum<\/strong>&nbsp;method and practiced different types of examples using the same.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.cumsum.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy cumsum Official Documentation<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on&nbsp;Numpy cumsum. In this tutorial, we will be learning about the NumPy&nbsp;cumsum()&nbsp;method and also seeing a lot of examples regarding the same. So let us begin! What is NumPy cumsum? The cumulative sum is a sequence of partial sums of a given sequence. If {a, b, c, d, e, [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":37508,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=37507"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37507\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37508"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}