{"id":38241,"date":"2023-01-10T17:22:15","date_gmt":"2023-01-10T17:22:15","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38241"},"modified":"2023-02-16T19:56:25","modified_gmt":"2023-02-16T19:56:25","slug":"numpy-diff","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-diff","title":{"rendered":"Numpy diff &#8211; Calculate the n-th discrete difference along the given axis."},"content":{"rendered":"\n<p>In this article we implement<strong> <code>numpy diff<\/code><\/strong> which is a function of the NumPy module in python. NumPy is an array-processing package that provides a high-performance multidimensional array object, and tools for working with these arrays. And we implement NumPy diff to calculate the nth discrete difference along the given axis.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-ediff1d\" data-type=\"post\" data-id=\"38542\">Numpy ediff1d \u2013 The differences between consecutive elements of an array<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span>What is NumPy <\/span>Diff? <\/h2>\n\n\n\n<p><code>numpy.diff<\/code> is a function of the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-module\" data-type=\"post\" data-id=\"7694\">NumPy module<\/a> provided by python. It is utilized for calculating the nth discrete difference along the given axis. If &#8216;x&#8217; is the input array then the first difference is calculated by:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>out[i]=x[i+1]-a[i]<\/code><\/li>\n<\/ul>\n\n\n\n<p>We can calculate higher differences by recursively applying the diff function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax of Numpy diff<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.diff(a , n=, axis=)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Parameters<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>a<\/strong> <strong>[array_like]<\/strong>: input array<\/li>\n\n\n\n<li><strong>n [ int, optional ]: <\/strong>the number of times the values are differenced.<\/li>\n\n\n\n<li><strong>axis [ int, optional ]: <\/strong>defines the axis along which operation to be carried out.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Return value<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>An array of the nth discrete difference<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Intro to Axis in array<\/h2>\n\n\n\n<p>A 2-dimensional array has two corresponding axes <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. axis 0: running vertically downwards across rows <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr=np.array(&#x5B;&#x5B; 0, 1, 2, 3],&#x5B; 4, 5, 6, 7],&#x5B; 8, 9, 10, 11]])\nprint(arr)\nx=arr.sum(axis=0)\nprint(&quot;addition along axis 0:\\n&quot;,x)\n<\/pre><\/div>\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B; 0 1 2 3]\n&#x5B; 4 5 6 7]\n&#x5B; 8 9 10 11]]\naddition along axis 0:\n&#x5B;12 15 18 2\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. axis 1: running horizontally across columns<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr=np.array(&#x5B;&#x5B; 0, 1, 2, 3],&#x5B; 4, 5, 6, 7],&#x5B; 8, 9, 10, 11]])\nprint(arr)\nx=arr.sum(axis=1)\nprint(&quot;addition along axis 1:\\n&quot;,x)\n<\/pre><\/div>\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B; 0 1 2 3]\n&#x5B; 4 5 6 7]\n&#x5B; 8 9 10 11]]\naddition along axis 0:\n&#x5B;12 15 18 21]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Implementing Numpy.diff to find the difference between two values<\/h2>\n\n\n\n<p>To calculate the nth discrete difference we use the <code>out[i]=x[i+1]-a[i]<\/code> formula <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/Numpy_Diff_Example_Array.png\" alt=\"Numpy Diff Example Array\" class=\"wp-image-39212\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/Numpy_Diff_Example_Array.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/Numpy_Diff_Example_Array-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/Numpy_Diff_Example_Array-768x384.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Example Array<\/figcaption><\/figure>\n\n\n\n<p>When n=1, Let&#8217;s take the first row of our example 2D array <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nout&#x5B;1]=x&#x5B;1+1]-a&#x5B;1]    :    20 - 10 =10 \nout&#x5B;2]=x&#x5B;2+1]-a&#x5B;2]    :    30 - 20 =10 \nout&#x5B;3]=x&#x5B;3+1]-a&#x5B;3]    :    40 - 30=10 \n<\/pre><\/div>\n\n\n<p>thus the result is [10 10 10]<\/p>\n\n\n\n<p>When n=2, We will use the previously obtained array(in n=1) for getting the result <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nout&#x5B;1]=x&#x5B;1+1]-a&#x5B;1]  :  10 - 10 = 0 \nout&#x5B;2]=x&#x5B;2+1]-a&#x5B;2]  :  10 - 10 = 0 \n<\/pre><\/div>\n\n\n<p>thus the result is [ 0 0 ]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Examples of Using Numpy.diff()<\/h3>\n\n\n\n<p><strong>Importing numpy and declaring an array.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr = np.array(&#x5B;&#x5B;10,20,30,40],&#x5B;50,60,70,80],&#x5B;90,100,110,np.nan]])\n<\/pre><\/div>\n\n\n<p><strong>Displaying the array of details <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;Our Array...\\n&quot;,arr)\nprint(&quot;shape of array: \\n&quot;,arr.shape)\nprint(&quot;length of array: \\n&quot;,len(arr))\nprint(&quot;\\nDatatype of our Array object...\\n&quot;,arr.dtype)\n<\/pre><\/div>\n\n\n<p><strong>Output : <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nOur Array\u2026\n&#x5B;&#x5B; 10. 20. 30. 40.]\n&#x5B; 50. 60. 70. 80.]\n&#x5B; 90. 100. 110. nan]]\nshape of array:\n(3, 4)\nlength of arr\n3\nDatatype of our Array object\u2026\nfloat64\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 1: NumPy diff of row ie axis 0<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\nDiscrete difference with axis =0..\\n&quot;,np.diff(arr, axis = 0))\n<\/pre><\/div>\n\n\n<p>This returns an array of discrete differences along axis 0 <\/p>\n\n\n\n<p><strong>Output : <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nDiscrete difference with axis =0..\n&#x5B;&#x5B;40. 40. 40. 40.]\n&#x5B;40. 40. 40. nan]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 2: NumPy diff of column ie axis =1<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\nprint(&quot;\\nDiscrete difference with axis =1 ..\\n&quot;,np.diff(arr, axis = 1))\n<\/pre><\/div>\n\n\n<p>Calculating discrete difference along axis 1 <\/p>\n\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nDiscrete difference with axis =1 ..\n&#x5B;&#x5B;10. 10. 10.]\n&#x5B;10. 10. 10.]\n&#x5B;10. 10. nan]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 3: 1st order diff of 2D array with axis =1 <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\nDiscrete 1st order difference..\\n&quot;,np.diff(arr, axis = 1))\n<\/pre><\/div>\n\n\n<p>The number of times we want an array to differ is calculated by assigning an n value. We differ the array once in this example along axis 1.<\/p>\n\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nDiscrete 1st order difference and axis = 1 ..\n&#x5B;&#x5B;10. 10. 10.]\n&#x5B;10. 10. 10.]\n&#x5B;10. 10. nan]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 4: 2nd order diff of 2D array with axis = 1 <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\nDiscrete 2nd order difference and axis = 1..\\n&quot;, np.diff(arr, axis = 1,n=2))\n<\/pre><\/div>\n\n\n<p>Calculating the discrete value for the second time along axis 1.<\/p>\n\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nDiscrete 2nd order difference and axis = 1..\n&#x5B;&#x5B; 0. 0.]\n&#x5B; 0. 0.]\n&#x5B; 0. nan]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 5: 3rd order diff of 2D array with axis = 1<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\nDiscrete 3rd order difference and axis = 1..\\n&quot;,np.diff(arr, axis = 1,n=3))\n<\/pre><\/div>\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nDiscrete 3rd order difference and axis = 1..\n&#x5B;&#x5B; 0.]\n&#x5B; 0.]\n&#x5B;nan]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 6: 1st order diff of 2D array with axis =0<\/h3>\n\n\n\n<p>Here we calculate values along axis 0.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\nDiscrete 1st order difference and axis = 0 ..\\n&quot;,np.diff(arr, axis = 0))\n<\/pre><\/div>\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nDiscrete 1st order difference and axis = 0 ..\n&#x5B;&#x5B;40. 40. 40. 40.]\n&#x5B;40. 40. 40. nan]]\n<\/pre><\/div>\n\n\n<p>Example 7 : 2nd order diff of 2D array with axis = 0<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\nDiscrete 2nd order difference and axis = 0..\\n&quot;,np.diff(arr, axis = 0,n=2))\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=\"\">\nDiscrete 2nd order difference and axis = 0..\n&#x5B;&#x5B; 0. 0. 0. nan]]\n<\/pre><\/div>\n\n\n<p>Example 8 : 3rd order diff of 2D array with axis = 0<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;\\nDiscrete 3rd order difference and axis = 0..\\n&quot;,np.diff(arr, axis = 0,n=3))\n<\/pre><\/div>\n\n\n<p>The output array is null as we have performed a recursive function enough on the elements to reach null values.<\/p>\n\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nDiscrete 3rd order difference and axis = 0..\n&#x5B;]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we understand the working of NumPy.diff function of the NumPy module in Python which is used to find the difference between the array values horizontally or vertically. We implement NumPy.diff with different nth and axis values via 2D array examples. <\/p>\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\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/numpy.org\/doc\/<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/het.as.utexas.edu\/HET\/Software\/Numpy\/reference\/generated\/numpy.diff.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/het.as.utexas.edu\/HET\/Software\/Numpy\/reference\/generated\/numpy.diff.html<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this article we implement numpy diff which is a function of the NumPy module in python. NumPy is an array-processing package that provides a high-performance multidimensional array object, and tools for working with these arrays. And we implement NumPy diff to calculate the nth discrete difference along the given axis. Also read: Numpy ediff1d [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":39297,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38241","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\/38241","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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38241"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38241\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/39297"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}