{"id":23410,"date":"2023-10-25T17:10:10","date_gmt":"2023-10-25T11:40:10","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=23410"},"modified":"2023-10-25T17:10:11","modified_gmt":"2023-10-25T11:40:11","slug":"numpy-outer-function","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-outer-function\/","title":{"rendered":"Python numpy.outer() Function: Calculate the Outer Product of Two Vectors"},"content":{"rendered":"\n<p>Vectors play an important role in the field of mathematics for helping with the navigation around different spaces &amp; planes. This article shall set out to explore one of the many functions that can be carried out with vectors in Python programming \u2013 the&nbsp;<em><strong>outer( )<\/strong>&nbsp;<\/em>function from the <em><strong>numpy <\/strong><\/em>library. <\/p>\n\n\n\n<p>Before that let us set things off by importing the&nbsp;<em>numpy&nbsp;<\/em>library using the below code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<p>The nuances of the&nbsp;<em>outer( )&nbsp;<\/em>function shall be covered in each of the following sections:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Syntax of <em>outer( )<\/em> Function<\/strong><\/li>\n\n\n\n<li><strong>Using <em>outer( )<\/em> function in One Dimensional Arrays<\/strong><\/li>\n\n\n\n<li><strong>Using <em>outer( )<\/em> function in N-Dimensional Arrays<\/strong><\/li>\n\n\n\n<li><strong>Using <em>outer( )<\/em> function with Letters<\/strong><\/li>\n\n\n\n<li><strong>Using <em>outer( )<\/em> function with other <em>Numpy <\/em>Functions<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of <em>outer( )<\/em> Function<\/h2>\n\n\n\n<p>The outer product of any given set of vectors is determined by multiplying each element in one vector with the corresponding element in the other and then forming the resultant vector using each value from the result obtained. Given below is the syntax of the <em>outer( )<\/em> function.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.outer(a, b, out=None)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>a \u2013&nbsp;<\/em><\/strong>n-dimensional array set as the first input vector<\/li>\n\n\n\n<li><strong><em>b \u2013<\/em><\/strong>&nbsp;n-dimensional array set as the second input vector<\/li>\n\n\n\n<li><strong><em>out \u2013<\/em><\/strong>&nbsp;an optional construct set to&nbsp;<em>none&nbsp;<\/em>by default, but could be used to store the results in an array that is of the same length as that of the result<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using <em>outer( )<\/em> Function in One-Dimensional Arrays<\/h2>\n\n\n\n<p>Let us create a couple of one-dimensional arrays as shown below to find their outer product.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = &#x5B;&#x5B;12, 36, 71, 99]]\nar2 = &#x5B;&#x5B;2, 8, 9, 56]]\nnp.outer(ar1, ar2)\n<\/pre><\/div>\n\n\n<p>The resulting output would have a similar number of columns, but the striking difference would be that of the rows, which in this case shall be 4 instead of 1. <\/p>\n\n\n\n<p>To understand this further, let us drill down a bit. The first element of the first array, \u201812\u2019 is multiplied with the entire first row of the second array, one element after the other. All the results form the first row of the output array. <\/p>\n\n\n\n<p>This process loops over and over until it reaches the last element of the array. Once done, the process gets repeated for the second element of the first array and so on and so forth.<\/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=\"324\" height=\"149\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-One-Dimensional-Arrays.jpg\" alt=\"Outer Product Calculated for One-Dimensional Arrays\" class=\"wp-image-23574\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-One-Dimensional-Arrays.jpg 324w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-One-Dimensional-Arrays-300x138.jpg 300w\" sizes=\"(max-width: 324px) 100vw, 324px\" \/><figcaption class=\"wp-element-caption\">Outer Product Calculated for One-Dimensional Arrays<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using <em>outer( )<\/em> Function in N-Dimensional Arrays<\/h2>\n\n\n\n<p>In this section, we shall calculate the outer product of vectors using input arrays which are constructed with 2 rows and 3 columns. Let us run the code to have a look at what the results might be!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar3 = &#x5B;&#x5B;24, 25, 26],\n       &#x5B;5, 2, 9]]\nar4 = &#x5B;&#x5B;6, 8, 12],\n       &#x5B;4, 3, 10]]\nnp.outer(ar3, ar4)\n<\/pre><\/div>\n\n\n<p>3 times 2 gives us 6 and so does the size of the output array \u2013 it is a 6&#215;6.<\/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=\"468\" height=\"200\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-Two-Dimensional-Arrays.jpg\" alt=\"Outer Product Calculated for Two-Dimensional Arrays\" class=\"wp-image-23575\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-Two-Dimensional-Arrays.jpg 468w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-Two-Dimensional-Arrays-300x128.jpg 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><figcaption class=\"wp-element-caption\">Outer Product Calculated for Two-Dimensional Arrays<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using <em>outer( )<\/em> Function with Letters<\/h2>\n\n\n\n<p>Multiplying numbers is not only the capability of the <em>outer( ) <\/em>function but one can also apply the function to arrays with alphabets, but the catch here is the datatype that is to be set to make it happen. Set the <em><strong>dtype <\/strong><\/em>as <strong>\u2018object\u2019<\/strong> as shown below &amp; the letters in the array are in for a ride to replicate.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar5 = np.array(&#x5B;&#039;g&#039;, &#039;j&#039;, &#039;l&#039;], dtype = object)\nar6 = &#x5B;&#x5B;2, 3, 1]]\nnp.outer(ar5, ar6)\n<\/pre><\/div>\n\n\n<p>Given below is the output when the above code is run.<\/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=\"443\" height=\"117\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-Letters.jpg\" alt=\"Outer Product Calculated for Letters\" class=\"wp-image-23576\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-Letters.jpg 443w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-for-Letters-300x79.jpg 300w\" sizes=\"(max-width: 443px) 100vw, 443px\" \/><figcaption class=\"wp-element-caption\">Outer Product Calculated for Letters<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using <em>outer( )<\/em> Function with <em>Numpy <\/em>Functions<\/h2>\n\n\n\n<p>The <em>outer( ) <\/em>function also works well with other functions within the <em>numpy <\/em>library such as <em><strong>ones( ) <\/strong><\/em>and the <em><strong>linspace( ). In this section,<\/strong><\/em> we shall deploy the <em>linspace( ) <\/em>function to divide the range between 4 &amp; 10 into four parts and then apply the <em>outer( ) <\/em>function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar7 = np.ones(4)\nar8 = np.linspace(4, 10, 4)\nnp.outer(ar7, ar8)\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=\"385\" height=\"129\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-with-Numpy-Functions.jpg\" alt=\"Outer Product Calculated with Numpy Functions\" class=\"wp-image-23577\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-with-Numpy-Functions.jpg 385w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Outer-Product-Calculated-with-Numpy-Functions-300x101.jpg 300w\" sizes=\"(max-width: 385px) 100vw, 385px\" \/><figcaption class=\"wp-element-caption\">Outer Product Calculated with <em>Numpy <\/em>Functions<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on how to use the&nbsp;<em>outer( )&nbsp;<\/em>function from the&nbsp;<em>numpy&nbsp;<\/em>library. Here\u2019s another article that explains <a href=\"https:\/\/codeforgeek.com\/python-docstrings\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/python-docstrings\/\" target=\"_blank\" rel=\"noreferrer noopener\">what are docstrings<\/a> and how to use them effectively in Python. There are numerous other enjoyable and equally informative articles in <em><a href=\"https:\/\/codeforgeek.com\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">CodeforGeek<\/a><\/em> that might be of great help to those who are looking to level up in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\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.outer.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.outer.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vectors play an important role in the field of mathematics for helping with the navigation around different spaces &amp; planes. This article shall set out to explore one of the many functions that can be carried out with vectors in Python programming \u2013 the&nbsp;outer( )&nbsp;function from the numpy library. Before that let us set things [&hellip;]<\/p>\n","protected":false},"author":90,"featured_media":23629,"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-23410","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\/10\/Python-numpy.outer-Function.png",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Python-numpy.outer-Function-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Python-numpy.outer-Function-300x180.png",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Python-numpy.outer-Function-768x461.png",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Python-numpy.outer-Function.png",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Python-numpy.outer-Function.png",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Python-numpy.outer-Function.png",1000,600,false]},"uagb_author_info":{"display_name":"Arulius Savio","author_link":"https:\/\/codeforgeek.com\/author\/arulius\/"},"uagb_comment_info":0,"uagb_excerpt":"Vectors play an important role in the field of mathematics for helping with the navigation around different spaces &amp; planes. This article shall set out to explore one of the many functions that can be carried out with vectors in Python programming \u2013 the&nbsp;outer( )&nbsp;function from the numpy library. Before that let us set things&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23410","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\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=23410"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23410\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/23629"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=23410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=23410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=23410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}