{"id":30162,"date":"2024-05-31T15:09:14","date_gmt":"2024-05-31T09:39:14","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=30162"},"modified":"2024-05-31T15:09:15","modified_gmt":"2024-05-31T09:39:15","slug":"numpy-hypot-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-hypot-in-python\/","title":{"rendered":"Understanding numpy.hypot() in Python"},"content":{"rendered":"\n<p>Have you ever needed to find the distance between two points in space? In this guide, we will solve this problem using the NumPy <strong>hypot()<\/strong> function. By using hypot, we can accurately measure the straight-line distance between two points in space, typically known as the hypotenuse. Let us look at this function in detail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Euclidean Distance?<\/h2>\n\n\n\n<p><strong>Euclidean<\/strong> <strong>Distance<\/strong> measures a <strong>straight-line distance between two points in space<\/strong>. <\/p>\n\n\n\n<p>Let me illustrate this with an example. Consider a number line (a one-dimensional spatial). If you have two points such as <strong>3 and 7<\/strong>, it simply means that their Euclidean Distance is the absolute difference between those two points.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"870\" height=\"290\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-42.png\" alt=\"Euclidean Distance\" class=\"wp-image-30163\" style=\"width:519px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-42.png 870w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-42-300x100.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-42-768x256.png 768w\" sizes=\"(max-width: 870px) 100vw, 870px\" \/><\/figure>\n\n\n\n<p>Now consider<strong> a flat map or plane<\/strong> that represents a two-dimensional space. You want to determine the distance covered by moving from position (1,2) to position (4,6). To find the Euclidean Distance, use the Pythagorean theorem:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"378\" height=\"45\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-45.png\" alt=\"Pythagorean theorem\" class=\"wp-image-30173\" style=\"width:252px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-45.png 378w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-45-300x36.png 300w\" sizes=\"(max-width: 378px) 100vw, 378px\" \/><\/figure>\n\n\n\n<p>Now that we are speaking of the Pythagorean theorem, let&#8217;s first talk about <strong>hypotenuse<\/strong>. When we draw a <strong>right-angled triangle<\/strong>, the side just opposite the right angle is what we call hypotenuse, and it is also the <strong>longest side<\/strong> of that triangle. In a 2D plane, the Euclidean distance between two points (1, 2) and (4, 6) is the length of the hypotenuse of a right triangle formed by these points.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"921\" height=\"522\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-43.png\" alt=\"Hypotenuse\" class=\"wp-image-30171\" style=\"width:607px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-43.png 921w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-43-300x170.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-43-768x435.png 768w\" sizes=\"(max-width: 921px) 100vw, 921px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing numpy.hypot() Function<\/h2>\n\n\n\n<p>The <strong>numpy.hypot<\/strong> function in the NumPy library calculates the Euclidean distance between two points in a 2D space. It finds the length of the <strong>hypotenuse<\/strong> of a right-angled triangle when given the lengths of the other two sides. This function is useful for measuring distances in machine learning, computer vision, and physics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p>Let&#8217;s look at the format of this function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.hypot(x1, x2, \/, out=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None, subok=True, **kwargs)\n<\/pre><\/div>\n\n\n<p>Here is what each <strong>parameter<\/strong> means.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x1, x2<\/strong>: These are the input values, representing the lengths of the two sides of a right-angled triangle.<\/li>\n\n\n\n<li><strong>out (optional)<\/strong>: An optional array to store the result. It should have the same shape as the expected output.<\/li>\n\n\n\n<li><strong>where (optional)<\/strong>: A condition that determines where the result should be computed. If True, the result is stored in <strong>out<\/strong>, otherwise, <strong>out<\/strong> keeps its original value.<\/li>\n\n\n\n<li><strong>casting (optional)<\/strong>: It determines how data types are cast.<\/li>\n\n\n\n<li><strong>order (optional)<\/strong>: Specified memory layout of resulting array. Choices include &#8216;C&#8217; (row-major), &#8216;F&#8217; (column-major), &#8216;<strong>A<\/strong>&#8216; (any) or \u2018<strong>K<\/strong>\u2019(keep input order).<\/li>\n\n\n\n<li><strong>dtype (optional)<\/strong>: Result\u2019s desired data type.<\/li>\n\n\n\n<li><strong>subok (optional)<\/strong>: If True, allows sub-classes to be passed through, otherwise, converts to the base class array.<\/li>\n\n\n\n<li><strong>kwargs<\/strong>: This provides additional optional arguments for this function.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Working:<\/h3>\n\n\n\n<p>Suppose we have two inputs, <strong>x1<\/strong>(Base)\u200b and <strong>x2<\/strong>(Perpendicular), representing the lengths of the two sides of a right-angled triangle. The <strong>numpy.hypot<\/strong> calculates the hypotenuse \u2014 the longest side, using this formula:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"471\" height=\"136\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-47.png\" alt=\"Hypotenuse formula\" class=\"wp-image-30176\" style=\"width:322px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-47.png 471w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-47-300x87.png 300w\" sizes=\"(max-width: 471px) 100vw, 471px\" \/><\/figure>\n\n\n\n<p>This hypotenuse represents the Euclidean distance from the origin to the point (<strong>x1\u200b,x2<\/strong>\u200b) in a two-dimensional cartesian plane.<\/p>\n\n\n\n<p>Well, this function directly computes these powers and square roots for you. But if you only need to compute the powers at any time, you can see how<a href=\"https:\/\/codeforgeek.com\/exponential-operations-with-numpy-power\/\"> NumPy&#8217;s power function<\/a> helps us with that.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of numpy.hypot in Python<\/h2>\n\n\n\n<p>Now, let&#8217;s implement the theory we just learned into code.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong> Let&#8217;s see the simplest case, calculating the hypotenuse for a triangle with sides <strong>3 <\/strong>and <strong>4<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nx1 = 3\nx2 = 4\n\ndistance = np.hypot(x1, x2)\nprint(distance)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"492\" height=\"127\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-48.png\" alt=\"Example 1 Output\" class=\"wp-image-30177\" style=\"width:376px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-48.png 492w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-48-300x77.png 300w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><\/figure>\n\n\n\n<p><strong>Example 2: <\/strong>Now we will demonstrate how it can handle array inputs, calculating the hypotenuses for multiple pairs of sides.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nx1 = np.array(&#x5B;3, 5, 8])\nx2 = np.array(&#x5B;4, 12, 15])\n\ndistances = np.hypot(x1, x2)\nprint(distances)\n<\/pre><\/div>\n\n\n<p>Now, in this case, the function would calculate the hypotenuse element-wise and then form a result array. For example, for 3 and 4, it&#8217;s 5, for 5 and 12, it&#8217;s 13, and so on. Therefore, the result would contain 5, 13, 17.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"503\" height=\"125\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-49.png\" alt=\"Example 2 Output\" class=\"wp-image-30178\" style=\"width:374px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-49.png 503w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-49-300x75.png 300w\" sizes=\"(max-width: 503px) 100vw, 503px\" \/><\/figure>\n\n\n\n<p><strong>Example 3: <\/strong>Let&#8217;s utilize broadcasting, where a single value for <strong>x2<\/strong>\u200b is applied to each element in the array<strong> x1\u200b<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nx1 = np.array(&#x5B;3, 5, 8])\nx2 = 4\n\ndistances = np.hypot(x1, x2)\nprint(distances)\n<\/pre><\/div>\n\n\n<p>This code calculates the distance between each element in array <strong>x1<\/strong> and the scalar value <strong>x2<\/strong>. It computes the hypotenuse <strong>element-wise<\/strong>, considering each element of <strong>x1 as one side<\/strong> and <strong>x2 as the other side<\/strong>, and then prints the resulting distances.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"487\" height=\"127\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-50.png\" alt=\"Example 3 Output\" class=\"wp-image-30179\" style=\"width:357px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-50.png 487w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-50-300x78.png 300w\" sizes=\"(max-width: 487px) 100vw, 487px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This one was easy and fun, right? Here, we started by understanding what Euclidean distance is and how it relates to the hypotenuse. We also learned about the NumPy tool for finding the hypotenuse which is <strong>hypot<\/strong>, went through its syntax and workings, and gained clarity through practical examples. Feel free to experiment with this concept in your projects.<\/p>\n\n\n\n<p><em>Wondering what to read next? Well, let me help you with that decision. Wouldn&#8217;t it be nice to know how NumPy helps us to <a href=\"https:\/\/codeforgeek.com\/numpy-gradient-in-python\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-gradient-in-python\/\">calculate gradients of array inputs in Python<\/a>?<\/em><\/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.hypot.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.hypot.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever needed to find the distance between two points in space? In this guide, we will solve this problem using the NumPy hypot() function. By using hypot, we can accurately measure the straight-line distance between two points in space, typically known as the hypotenuse. Let us look at this function in detail. What [&hellip;]<\/p>\n","protected":false},"author":104,"featured_media":30181,"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-30162","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\/2024\/05\/Calculating-Euclidean-Distance-in-Python.png",1200,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Calculating-Euclidean-Distance-in-Python-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Calculating-Euclidean-Distance-in-Python-300x150.png",300,150,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Calculating-Euclidean-Distance-in-Python-768x384.png",768,384,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Calculating-Euclidean-Distance-in-Python-1024x512.png",1024,512,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Calculating-Euclidean-Distance-in-Python.png",1200,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Calculating-Euclidean-Distance-in-Python.png",1200,600,false]},"uagb_author_info":{"display_name":"Snigdha Keshariya","author_link":"https:\/\/codeforgeek.com\/author\/snigdha\/"},"uagb_comment_info":0,"uagb_excerpt":"Have you ever needed to find the distance between two points in space? In this guide, we will solve this problem using the NumPy hypot() function. By using hypot, we can accurately measure the straight-line distance between two points in space, typically known as the hypotenuse. Let us look at this function in detail. What&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/30162","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\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=30162"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/30162\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/30181"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=30162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=30162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=30162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}