{"id":37964,"date":"2022-12-29T13:52:42","date_gmt":"2022-12-29T13:52:42","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37964"},"modified":"2023-02-16T19:56:25","modified_gmt":"2023-02-16T19:56:25","slug":"numpy-multiply","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-multiply","title":{"rendered":"NumPy multiply &#8211; Illustrated in a Simple Way"},"content":{"rendered":"\n<p>Hey everyone! Welcome to another tutorial on NumPy functions. In this tutorial, we will explore the NumPy multiply function in detail.<\/p>\n\n\n\n<p>We all do multiplication operations in our daily life. Be it our mathematics class or the calculations done by the shopkeeper to keep track of the items in the shop. Today we will see how can we multiply two numbers or two arrays of numbers using python programming. <\/p>\n\n\n\n<p>Let&#8217;s get started.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-linalg-det\" data-type=\"post\" data-id=\"38991\">NumPy linalg.det \u2013 Compute the determinant of the given array<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy multiply?<\/h2>\n\n\n\n<p>NumPy multiply is one of the mathematical functions of the NumPy library that multiplies the inputs passed to the function.<\/p>\n\n\n\n<p>Let&#8217;s take a look at the syntax of the function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax of NumPy multiply<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.multiply(x1 , x2)\n<\/pre><\/div>\n\n\n<p>Here, the inputs <strong><code>x1<\/code><\/strong> and <strong><code>x2<\/code><\/strong> can be scalar numbers as well as a NumPy array of numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working with NumPy multiply<\/h2>\n\n\n\n<p>Let&#8217;s do some python programming.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy multiply with Scalar Values<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing the NumPy module\nimport numpy as np\n \na = 5\nb = 4\n \nprint(&quot;The product of 5 and 4 is:&quot;,np.multiply(a , b))\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=\"\">\nThe product of 5 and 4 is: 20\n<\/pre><\/div>\n\n\n<p>We first import the NumPy library using the&nbsp;<strong><code>import<\/code><\/strong>&nbsp;statement in the above snippet. In the <strong><code>print<\/code><\/strong> statement, the function <strong><code>np.multiply(a,b)<\/code><\/strong> is called where a and b are passed as input to the function.<\/p>\n\n\n\n<p>In this snippet, the output is also a <strong>scalar<\/strong> value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy multiply with a NumPy array and a Scalar value<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing the NumPy module\nimport numpy as np\n \n# Creating the  2-D array\na = np.array(&#x5B;&#x5B;2 , 4 , 7] , &#x5B;5 , 10 , 15]])\n \nb = 10\n \nc = np.multiply(a , b)\n \nprint(&quot;Input Array:\\n&quot;,a)\nprint(&quot;After multiplying 10 to each value of the Input Array:\\n&quot;,c)\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=\"\">\nInput Array:\n &#x5B;&#x5B; 2  4  7]\n &#x5B; 5 10 15]]\nAfter multiplying 10 to each value of the Input Array:\n &#x5B;&#x5B; 20  40  70]\n &#x5B; 50 100 150]]\n<\/pre><\/div>\n\n\n<p>In the above example, a 2-D array of size 2&#215;3 is created using the function <strong><code>np.array()<\/code><\/strong>. In the following lines, the function <strong><code>np.multiply(a,b)<\/code><\/strong> is called by passing <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong> as arguments to the function where <strong><code>a<\/code><\/strong> is the NumPy array and <strong><code>b<\/code><\/strong> holds the scalar value of 10.<\/p>\n\n\n\n<p>In the output, the function <strong><code>np.multiply(a,b)<\/code><\/strong> multiplied all the values of the NumPy array by 10.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy multiply with two Same- Sized NumPy arrays<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n \n# Creating 2x2 array\na = np.array(&#x5B;&#x5B;2 , 5] , &#x5B;1 , 4]])\n \nb = np.array(&#x5B;&#x5B;9 , 5] , &#x5B;21 , 34]])\n \n# Using the multiply function\nc = np.multiply(a , b)\n \n# Printing the values\nprint(&quot;Array 1:\\n&quot;,a)\nprint(&quot;Array 2:\\n&quot;,b)\n \nprint(&quot;Output array:\\n&quot;,c)\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=\"\">\nArray 1:\n &#x5B;&#x5B;2 5]\n &#x5B;1 4]]\nArray 2:\n &#x5B;&#x5B; 9  5]\n &#x5B;21 34]]\nOutput array:\n &#x5B;&#x5B; 18  25]\n &#x5B; 21 136]]\n<\/pre><\/div>\n\n\n<p>In this example, two NumPy arrays of size 2&#215;2 are created using the function <strong><code>np.array()<\/code><\/strong> and are stored in the variables <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong>. Next, the function <code>np.multiply(a,b)<\/code> is called by passing <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong> as arguments where <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong> are the NumPy arrays that we created previously using the function <code>np.array()<\/code>. <\/p>\n\n\n\n<p>In the output, the array contains the product of the values at the <strong>same position<\/strong> in the two input arrays.<\/p>\n\n\n\n<p><strong>Note:<\/strong> The output array has the same size as the input array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy multiply with a Matrix and a Vector<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Creating a vector or 1-D array\na = np.array((10 , 20 , 30))\n \n# Creating a matrix or 2-D array\nb = np.array(&#x5B;&#x5B;1 , 2 , 4] , &#x5B;8 , 10 , 16]])\n \nc = np.multiply(a , b)\n \nprint(&quot;Array 1:\\n&quot;,a)\nprint(&quot;Array 2:\\n&quot;,b)\n \nprint(&quot;Output Array:\\n&quot;,c)\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=\"\">\nArray 1:\n &#x5B;10 20 30]\nArray 2:\n &#x5B;&#x5B; 1  2  4]\n &#x5B; 8 10 16]]\nOutput Array:\n &#x5B;&#x5B; 10  40 120]\n &#x5B; 80 200 480]]\n<\/pre><\/div>\n\n\n<p>This is the most interesting example. Here, we created a vector or a 1-D array having 3 elements and a 2-D array or matrix of size 2&#215;3 i.e. having 2 rows and 3 columns. In the following lines, the function <strong><code>np.multiply(a,b)<\/code><\/strong> is called by passing <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong> as arguments where a is the <strong>vector<\/strong> and b is the <strong>matrix<\/strong>.<\/p>\n\n\n\n<p>In this situation, NumPy performs <strong>broadcasting<\/strong>. It takes the vector and multiplies it with each row in the matrix. This is possible because the vector has the same number of elements as the number of columns in the matrix.<\/p>\n\n\n\n<p>In the output array, the <strong>first row<\/strong> of elements is obtained by multiplying the vector with the first row of the matrix, and the <strong>second row<\/strong> of elements is obtained by multiplying the vector with the second row of the matrix.<\/p>\n\n\n\n<p>With that, we completed all the examples for this tutorial. You should try using the function with examples of your choice and observe the outputs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this article, we learned about the NumPy multiply function and practiced different types of examples. This is really a simple function to use and easy to understand. Keep exploring more such tutorials <a href=\"https:\/\/www.askpython.com\/\" type=\"URL\" id=\"askpython.com\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/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.multiply.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy Documentation &#8211; NumPy multiply<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey everyone! Welcome to another tutorial on NumPy functions. In this tutorial, we will explore the NumPy multiply function in detail. We all do multiplication operations in our daily life. Be it our mathematics class or the calculations done by the shopkeeper to keep track of the items in the shop. Today we will see [&hellip;]<\/p>\n","protected":false},"author":48,"featured_media":39013,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37964","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\/37964","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\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=37964"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37964\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/39013"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}