{"id":355,"date":"2021-09-19T15:21:42","date_gmt":"2021-09-19T20:21:42","guid":{"rendered":"https:\/\/davidwdrell.net\/wordpress\/?p=355"},"modified":"2023-05-21T08:46:40","modified_gmt":"2023-05-21T13:46:40","slug":"numpy-dot-product-of-vectors","status":"publish","type":"post","link":"https:\/\/davidwdrell.net\/wordpress\/?p=355","title":{"rendered":"Numpy Dot Product of Vectors"},"content":{"rendered":"<p><\/p>\n<h1>Mathematical Definition of a Dot Product<\/h1>\n<p><\/p>\n<p>The dot product of two vectors <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-d2caf98806c9e3398bda4a9b29ae1c82_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#118;&#101;&#99;&#123;&#65;&#125;&#32;&#61;&#32;&#40;&#97;&#95;&#49;&#44;&#32;&#97;&#95;&#50;&#44;&#32;&#97;&#95;&#51;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"22\" width=\"117\" style=\"vertical-align: -5px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-7dcb65d313b174b53d8cf9f63b983a62_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#118;&#101;&#99;&#123;&#66;&#125;&#32;&#61;&#32;&#40;&#98;&#95;&#49;&#44;&#32;&#98;&#95;&#50;&#44;&#32;&#98;&#95;&#51;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"22\" width=\"113\" style=\"vertical-align: -5px;\"\/> is a scaler given by:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-debc28a7996c4c51c3047de7b980ae95_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#118;&#101;&#99;&#123;&#65;&#125;&#92;&#99;&#100;&#111;&#116;&#32;&#92;&#118;&#101;&#99;&#123;&#66;&#125;&#61;&#32;&#97;&#95;&#49;&#32;&#98;&#95;&#49;&#32;&#43;&#32;&#97;&#95;&#50;&#32;&#98;&#95;&#50;&#32;&#43;&#32;&#97;&#95;&#51;&#32;&#98;&#95;&#51;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"204\" style=\"vertical-align: -3px;\"\/><\/p>\n<h1>Vectors In Python\/Numpy<\/h1>\n<p>How can we use numpy to solve generalized vector dot products such as the one below:&nbsp;<\/p>\n<p>Given&nbsp; <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-55302f8f1cde44a059e70b97eb402991_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#97;&#32;&#61;&#32;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#32;&#49;&#32;&#38;&#32;&#53;&#32;&#38;&#32;&#45;&#51;&#32;&#38;&#32;&#50;&#32;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#94;&#84;\" title=\"Rendered by QuickLaTeX.com\" height=\"26\" width=\"156\" style=\"vertical-align: -7px;\"\/>&nbsp; &nbsp; and&nbsp; <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-4d186344ecc3bf4abcb3f91207e8a56f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#98;&#61;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#32;&#56;&#32;&#38;&#32;&#50;&#32;&#38;&#32;&#52;&#32;&#38;&#32;&#55;&#92;&#101;&#110;&#100;&#123;&#98;&#109;&#97;&#116;&#114;&#105;&#120;&#125;&#32;&#94;&#84;\" title=\"Rendered by QuickLaTeX.com\" height=\"26\" width=\"140\" style=\"vertical-align: -7px;\"\/><\/p>\n<p>What is <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-f33936534768d63656b53207ea8905fb_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#97;&#94;&#84;&#32;&#98;&#63;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"36\" style=\"vertical-align: 0px;\"\/><\/p>\n<p>Using python and the numpy library, we have two options for expressing this calculation, 1-D arrays, and matrices. But each has a caveat to consider.<\/p>\n<p>In all code examples below assume we have imported the numpy library:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>\n&gt;&gt;&gt; import numpy as np<\/code><\/pre>\n\n\n<h2>Vector as 1-D Array<\/h2>\n<p>In python, using the numpy library, a vector can be represented as 1-D array or an Nx1 (or 1xN) matrix. For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; a = np.array(&#91;1,5,-3,2])       # create 1-D array, a simple list of numbers\n&gt;&gt;&gt; a\narray(&#91; 1,  5, -3,  2])\n&gt;&gt;&gt; a.shape\n(4,)                               # shape is shown to be a 1-D array\n<\/code><\/pre>\n\n\n\n<p>If we take a transpose of the 1-D array, numpy will return the same dimension. So a transpose function has no effect on a numpy 1-D array.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; a\narray(&#91; 1,  5, -3,  2])\n&gt;&gt;&gt; a.shape               # shape of a is 4\n(4,)\n&gt;&gt;&gt; at = a.transpose()\n\n&gt;&gt;&gt; at.shape\n(4,)\n&gt;&gt;&gt; at\narray(&#91; 1,  5, -3,  2])  # shape of a-transpose is also 4\n\n&gt;&gt;&gt; at.shape\n(4,)<\/code><\/pre>\n\n\n\n<p>So if we define a vector &#8216;a&#8217; and a vector &#8216;b&#8217; and try to find the dot product of the transpose of &#8216;a&#8217; to &#8216;b&#8217;, the transpose will have no effect, but numpy will dot product the two single dimension vectors with this result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; a\narray(&#91; 1,  5, -3,  2])\n&gt;&gt;&gt; b\narray(&#91;8, 2, 4, 7])\n\n&gt;&gt;&gt; c = np.dot(a,b)   # take the dot product of 1-D vectors a and b\n&gt;&gt;&gt; c\n20                     # the result is a scalar of value 20\n<\/code><\/pre>\n\n\n\n<p>Note the result is the expected value of 20, and it is a scalar as expected. So when using numpy 1-D arrays for dot products, the user has to be aware that transpose functions are meaningless but also will not affect the dot product result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Vector as a row\/column of a 2-D Matrix<\/h2>\n\n\n\n<p>If we create the vector a as a numpy 2-D matrix by using the double brackets (single row, multi-column), the resulting matrix is shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; a = np.array(&#91; &#91;1,5,-3,2]  ])   # single row, multi-column array with dimensions 1x4\n&gt;&gt;&gt; a\narray(&#91;&#91; 1,  5, -3,  2]])\n&gt;&gt;&gt; a.shape\n(1, 4)                              # shape is shown to be a NxM array with N=1, M=4\n<\/code><\/pre>\n\n\n\n<p>If we then take the transpose of a, we get:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; at = a.transpose()\n&gt;&gt;&gt; at                   # the transpose of a is now a 4x1 (4 row, 1 col) matrix\narray(&#91;&#91; 1],\n       &#91; 5],\n       &#91;-3],\n       &#91; 2]])\n&gt;&gt;&gt; at.shape\n(4, 1)                   # shape is shown to be a NxM array with N=4, M=1\n\n\n<\/code><\/pre>\n\n\n<p>So back to our generalized problem (defined above), what is <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/davidwdrell.net\/wordpress\/wp-content\/ql-cache\/quicklatex.com-f33936534768d63656b53207ea8905fb_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#97;&#94;&#84;&#32;&#98;&#63;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"36\" style=\"vertical-align: 0px;\"\/> , using numpy matrices:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>a = np.array(&#91; &#91;1,5,-3,2]  ]).transpose()  # implement a as given above\nat = a.transpose()                        # get a transpose\nb = np.array(&#91; &#91;8,2,4,7]  ]).transpose()   # implement b as given above\nc = np.dot(at,b)                           # get the aT dot b \n>>> c                                      # print the contents of c\narray(&#91;&#91;20]])<\/code><\/pre>\n\n\n\n<p>We see that we can use the transpose as expected, and get the expected result of 20, but the result is expressed as a 1&#215;1 matrix rather than the expected scaler. <\/p>\n\n\n\n<p>Also, note, dot products of matrices are only defined as the product of matrices with orthogonal dimensions of  (1xN dot Nx1), or (Nx1 dot 1xN). If you attempt to take the dot product of a Nx1 and Nx1, for example, you will get an error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; c=np.dot(a,b)\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\n  File \"&lt;__array_function__ internals&gt;\", line 6, in dot\nValueError: shapes (1,4) and (1,4) not aligned: 4 (dim 1) != 1 (dim 0)\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Conclusion<\/h1>\n\n\n\n<p>If numpy 1-D arrays are used for dot product, the user has to understand that transpose functions have no meaning. On the other hand, if numpy matrices are used, the transpose function has the expected meaning but the user has to remember to translate the 1&#215;1 matrix result to a scaler result.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mathematical Definition of a Dot Product The dot product of two vectors and is a scaler given by: Vectors In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[10],"tags":[],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=355"}],"version-history":[{"count":10,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions\/490"}],"wp:attachment":[{"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidwdrell.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}