{"id":22601,"date":"2023-10-18T16:50:54","date_gmt":"2023-10-18T11:20:54","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=22601"},"modified":"2023-10-18T17:02:06","modified_gmt":"2023-10-18T11:32:06","slug":"numpy-array-addition","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-array-addition\/","title":{"rendered":"NumPy Array Addition with numpy.add() and Addition Operator"},"content":{"rendered":"\n<p>NumPy is one of the popular <a href=\"https:\/\/codeforgeek.com\/python\/\">Python<\/a> libraries that offers a wide variety of arithmetic and numerical functions. This article aims to clarify how to perform addition operations on NumPy arrays using the numpy.add() and addition operator. We will look at both ways in detail so that you can use the one that best suits you. Let\u2019s get started.<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/codeforgeek.com\/numpy-broadcasting\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-broadcasting\/\">Numpy Broadcasting (With Examples)<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Addition Using numpy.add() Function <\/h2>\n\n\n\n<p><strong>numpy.add()<\/strong> is a function in the <a href=\"https:\/\/codeforgeek.com\/python-top-libraries-for-machine-learning-and-data-science\/\">NumPy <\/a>library that is designed for the element-wise addition of two arrays. It can also be used for scalar addition by providing a scalar value along with an array.<\/p>\n\n\n\n<p>Below is the syntax for using&nbsp;<strong>numpy.add()<\/strong>&nbsp;function in Python.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.add(x1, x2, \/, out=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None, subok=True&#x5B;, signature, extobj])\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x1, x2:<\/strong> The input arrays for addition.<\/li>\n\n\n\n<li>out (optional): The output array in which the result is placed.<\/li>\n\n\n\n<li><strong>where (optional):<\/strong> A boolean array indicating where the operation should occur.<\/li>\n\n\n\n<li><strong>casting (optional): <\/strong>Controls what kind of data casting may occur.<\/li>\n\n\n\n<li><strong>order (optional):<\/strong> &#8216;K&#8217;, &#8216;A&#8217;, &#8216;C&#8217;, or &#8216;F&#8217; to control the memory layout of the output.<\/li>\n\n\n\n<li><strong>dtype (optional): <\/strong>Desired data type for the output.<\/li>\n\n\n\n<li><strong>subok (optional):<\/strong> If True, then the input arrays are allowed to be subclasses.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return:<\/strong> An NumPy array representing<strong> <\/strong>the sum of x1 and x2, element-wise.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Adding Array with Scalar<\/h3>\n\n\n\n<p>We can add an array with any dimension to a scaler value using<strong> numpy.add()<\/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;1,7,6,4])\nscValue=100\nresult= np.add(arr,scValue)\nresult\n<\/pre><\/div>\n\n\n<p>In the above code, we first imported the Numpy library as <strong>np<\/strong> then we created a 1D array and stored it into a variable <strong>arr<\/strong>. Then we declared a scaler value as 100 and stored it into variable <strong>scValue<\/strong>. After that, we provided an array with the scaler value in the <strong>numpy.add()<\/strong> function for addition and stored the result into a <strong>result <\/strong>variable, and at last, we printed the variable.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"780\" height=\"61\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1861.png\" alt=\"Example 1: Adding Array with Scalar\" class=\"wp-image-22707\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1861.png 780w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1861-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1861-768x60.png 768w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/figure>\n\n\n\n<p>In the output above we have seen that we got an array after adding the single number or a scaler value with all the elements of the 1D array using <strong>numpy.add()<\/strong> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Adding Two 1D Arrays<\/h3>\n\n\n\n<p>In this example, we will learn how to add two 1D arrays using <strong>numpy.add()<\/strong> function. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar1 = np.array(&#x5B;21,34,7])\nar2 = np.array(&#x5B;3,8,6])\nresult = np.add(ar1,ar2)\nresult\n<\/pre><\/div>\n\n\n<p>Here we created two 1D arrays and passed both the arrays into the <strong>numpy.add()<\/strong> function for adding them and save the result into a variable called<strong> result<\/strong>. In last, we printed the variable.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"775\" height=\"63\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1862.png\" alt=\"Example 2: Adding Two 1D Arrays\" class=\"wp-image-22708\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1862.png 775w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1862-300x24.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1862-768x62.png 768w\" sizes=\"(max-width: 775px) 100vw, 775px\" \/><\/figure>\n\n\n\n<p>In the output, we have seen that the first element of the first array got added with the first element of the second array, and so on until all the elements got added between those arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Adding Two 2D Arrays<\/h3>\n\n\n\n<p>Likewise, with the addition of two 1D arrays, we will do the addition of two 2D arrays using <strong>numpy.add()<\/strong> function in this example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar1 = np.array(&#x5B;&#x5B;9, 8], &#x5B;36, 5]])\nar2 = np.array(&#x5B;&#x5B;3, 85], &#x5B;2, 13]])\nresult= np.add(ar1,ar2)\nresult\n<\/pre><\/div>\n\n\n<p>Here also we created two arrays but both are 2D and then applied <strong>numpy.add()<\/strong> function on both the array and save the result into the variable <strong>result<\/strong>. Lastly, we printed the variable. <\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"77\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1863-1024x77.png\" alt=\"Example 3: Adding Two 2D Arrays\" class=\"wp-image-22709\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1863-1024x77.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1863-300x22.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1863-768x57.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1863.png 1191w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Above we can see the same thing as the last example, here also first element of the first array was added to the first element of the second element and it continued till the last element of both arrays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Addition Using Addition Operator (+) <\/h2>\n\n\n\n<p>The addition operator also performs element-wise addition for NumPy arrays. It is basically used for basic operations. The result which we were getting with <strong>numpy.add()<\/strong> function for the addition, we can get the same result by using the addition operator. Let&#8217;s see a few examples. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Adding Array with Scalar<\/h3>\n\n\n\n<p>Here also by using the additon operator, we can get the same result for adding a scaler value with the array that we did in the example of <strong>numpy.add()<\/strong> function<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar=np.array(&#x5B;12,10,88,99])\nscVal=4\nresult=ar+scVal\nresult\n<\/pre><\/div>\n\n\n<p>In the above code, we first imported the NumPy as <strong>np <\/strong>then we created an array using the NumPy library. Then we stored a scaler value 4 into the variable <strong>scVal<\/strong>. Then we used the addition operator for the addition this time and stored the result of addition into the <strong>result <\/strong>variable and then printed it.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"830\" height=\"62\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1858-1.png\" alt=\"Example 1: Adding Array with Scalar\" class=\"wp-image-22704\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1858-1.png 830w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1858-1-300x22.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1858-1-768x57.png 768w\" sizes=\"(max-width: 830px) 100vw, 830px\" \/><\/figure>\n\n\n\n<p>Above we can see that the scaler value got added with each element from the array one by one like with the numpy.add() function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Adding Two 1D Arrays<\/h3>\n\n\n\n<p>We can also add two arrays by just using an addition operator between the two arrays.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar1 = np.array(&#x5B;15,16,13,14,15])\nar2 = np.array(&#x5B;4,5,6,7,8])\nresult= ar1 + ar2\nresult\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"817\" height=\"62\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1859.png\" alt=\"Example 2: Adding Two 1D Arrays\" class=\"wp-image-22705\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1859.png 817w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1859-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1859-768x58.png 768w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/figure>\n\n\n\n<p>In the same manner as the <strong>numpy.add()<\/strong> function, here also we have seen that the first element of the first array got added with the first element of the second array, and so on until all the elements got added between those arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Adding Two 2D Arrays<\/h3>\n\n\n\n<p>In the same way that we did for the 1D array, we can do it with the 2D array for addition using the addition operator.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar1 = np.array(&#x5B;&#x5B;1,6],&#x5B;2,4]])\nar2 = np.array(&#x5B;&#x5B;3,8],&#x5B;9,7]])\nresult= ar1 + ar2\nresult\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"82\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1860-1024x82.png\" alt=\"Example 3: Adding Two 2D Arrays\" class=\"wp-image-22706\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1860-1024x82.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1860-300x24.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1860-768x61.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1860.png 1067w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this tutorial, we have discussed<strong>\u00a0numpy.add()<\/strong>\u00a0function provided by Python\u2019s NumPy library with the addition operator and explored three examples of both ways to find the addition of NumPy arrays. After reading this tutorial, we hope you can easily do the addition of NumPy arrays in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.add.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.add.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy is one of the popular Python libraries that offers a wide variety of arithmetic and numerical functions. This article aims to clarify how to perform addition operations on NumPy arrays using the numpy.add() and addition operator. We will look at both ways in detail so that you can use the one that best suits [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":22701,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-22601","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Addition-of-Array-in-Python-numpy.add-Addition-Operator-.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Addition-of-Array-in-Python-numpy.add-Addition-Operator--150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Addition-of-Array-in-Python-numpy.add-Addition-Operator--300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Addition-of-Array-in-Python-numpy.add-Addition-Operator--768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Addition-of-Array-in-Python-numpy.add-Addition-Operator--1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Addition-of-Array-in-Python-numpy.add-Addition-Operator-.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Addition-of-Array-in-Python-numpy.add-Addition-Operator-.png",1200,800,false]},"uagb_author_info":{"display_name":"Priyanshu Singh","author_link":"https:\/\/codeforgeek.com\/author\/priyanshu\/"},"uagb_comment_info":0,"uagb_excerpt":"NumPy is one of the popular Python libraries that offers a wide variety of arithmetic and numerical functions. This article aims to clarify how to perform addition operations on NumPy arrays using the numpy.add() and addition operator. We will look at both ways in detail so that you can use the one that best suits&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/22601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=22601"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/22601\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/22701"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=22601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=22601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=22601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}