{"id":21054,"date":"2021-07-31T15:51:00","date_gmt":"2021-07-31T15:51:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=21054"},"modified":"2021-08-09T16:06:51","modified_gmt":"2021-08-09T16:06:51","slug":"dot-product","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/dot-product","title":{"rendered":"How to Calculate Dot Product in Python?"},"content":{"rendered":"\n<p>Hello Learner! In this article, we will see the python code to find the dot product of any given quantities, say vectors or arrays. Python programming language provides several ways to do this, some of them are discussed below.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/vectorization-numpy\" data-type=\"post\" data-id=\"17529\">Vectorization in Python \u2013 A Complete Guide<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Dot Product?<\/h2>\n\n\n\n<p>First, let&#8217;s understand &#8220;dot product.&#8221;  <\/p>\n\n\n\n<p>In mathematics, the <strong>Dot product<\/strong> (sometimes known as scalar product) is an algebraic operation that returns a single value from two equal-length sequences of numbers.<\/p>\n\n\n\n<p>This single value is calculated as the sum of the products of the corresponding elements from both sequences. These sequences might be single-dimensional vectors, multi-dimensional vectors, or simply numbers.<\/p>\n\n\n\n<p>Let&#8217;s take an example to understand this:<\/p>\n\n\n\n<p>Suppose, two vectors <strong>A <\/strong>and <strong>B <\/strong>are 2-D arrays as &#8211; <\/p>\n\n\n\n<p>A = [ [1 2 ] [3 4] ]    and B = [ [5 6] [7 8] ]                                                                                                          <\/p>\n\n\n\n<p>Then, <strong>A.B<\/strong>  is given as<\/p>\n\n\n\n<p>[ [ 19  22] [ 43  50] ]        <\/p>\n\n\n\n<p>This is calculated as [  [ ((1*5)+(2*7))   ((1*6)+(2*8)) ]   [ ((3*5)+(4*7))   ((3*6)+(4*8)) ]  ]     <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python code to find the dot product<\/h2>\n\n\n\n<p>Python provides an efficient way to find the dot product of two sequences which is <em>numpy.dot() <\/em>method of numpy library.<\/p>\n\n\n\n<p>Numpy.dot() is a method that takes the two sequences as arguments, whether it be vectors or multidimensional arrays, and prints the result i.e., dot product. To use this method, we must import the <a href=\"https:\/\/www.askpython.com\/python\/numpy-linear-algebraic-functions\" data-type=\"post\" data-id=\"13883\">numpy<\/a> library of python. Let&#8217;s look at few examples :<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Dot product of scalars<\/h3>\n\n\n\n<p>In this example, we will take two scalar values, and print their dot product using numpy.dot().<\/p>\n\n\n\n<p>The dot product of two scalars is obtained by simply multiplying them. <\/p>\n\n\n\n<p>Say, Two scalars A = 7 and B = 6, then A.B = 42<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing numpy library\nimport numpy as np\n\n#Taking two scalars\na = 3\nb = 8\n\n#calculating dot product using dot()\nprint(&quot;The dot product of given scalars = a.b =&quot;,np.dot(a,b))\n<\/pre><\/div>\n\n\n<p>The output for the above code is :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nThe dot product of given scalars = a.b = 24\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Example 2: Dot product of arrays<\/h3>\n\n\n\n<p>Here, we will be taking two <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" data-type=\"post\" data-id=\"1070\">arrays<\/a>. These arrays can be 1-D, 2-D or multi-dimensional. And with the help of dot(), we will calculate their dot product. We are considering two 2-D arrays for the dot product.<\/p>\n\n\n\n<p>The dot product for 2-D arrays is calculated by doing matrix multiplication. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing numpy library\nimport numpy as np\n\n#Taking two 2-D arrays\na = &#x5B; &#x5B;1, 2], &#x5B;3, 4]]\nb = &#x5B; &#x5B;7, 6], &#x5B;5, 4]]\n\n#calculating dot product using dot()\nprint(&quot;The dot product of given arrays :&quot;)\nnp.dot(a,b))\n\n<\/pre><\/div>\n\n\n<p>The output is :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nThe dot product of given arrays :\n\narray( &#x5B; &#x5B;17, 14],\n            &#x5B;41, 34] ] )\n<\/pre><\/div>\n\n\n<p><strong><em>NOTE: <\/em><\/strong><\/p>\n\n\n\n<p>For two-dimensional or multi-dimensional arrays, the dot product is <em>not commutative.<\/em> i.e., a.b is not equal to b.a In example 2, we have calculated dot product as a.b, and not b.a. This will give a completely different result.       <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>So, Isn&#8217;t it simple to calculate dot products in Python? With the functions available, of course, it is. This was it from my side. I hope you understood this article. For more such articles, Stay tuned to <a href=\"https:\/\/www.askpython.com\/\">https:\/\/www.askpython.com\/<\/a> <\/p>\n\n\n\n<p>Till then, happy learning! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Learner! In this article, we will see the python code to find the dot product of any given quantities, say vectors or arrays. Python programming language provides several ways to do this, some of them are discussed below. Also read: Vectorization in Python \u2013 A Complete Guide What is a Dot Product? First, let&#8217;s [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":21423,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-21054","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\/21054","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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=21054"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/21054\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/21423"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=21054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=21054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=21054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}