{"id":29967,"date":"2024-05-26T19:49:36","date_gmt":"2024-05-26T14:19:36","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=29967"},"modified":"2024-05-26T19:49:37","modified_gmt":"2024-05-26T14:19:37","slug":"finding-absolute-values-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/finding-absolute-values-in-python\/","title":{"rendered":"Finding Absolute Values in Python"},"content":{"rendered":"\n<p>Hello coders! Welcome to a new chapter of Python guides. Today, we will see how to calculate absolute values in Python using NumPy. We&#8217;ll explore the NumPy fabs() function, understand its syntax, and how it works, and see examples, demonstrations, and its drawbacks. We will also see how to address the given disadvantages. So, stay tuned and follow along!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is Absolute Value?<\/strong><\/h2>\n\n\n\n<p>The <strong>absolute value<\/strong> of a number is like its <strong>distance from zero<\/strong> on a number line. It shows how far the number is from zero, no matter if it&#8217;s positive or negative. For example, the absolute value of <strong>6<\/strong> is <strong>6<\/strong>, and the absolute value of <strong>-6<\/strong> is also <strong>6<\/strong>. We write it with two straight lines around the number, like <strong>|x|<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"629\" height=\"336\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/illustration1.png\" alt=\"Illustration of Absolute Value Function\" class=\"wp-image-29968\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/illustration1.png 629w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/illustration1-300x160.png 300w\" sizes=\"(max-width: 629px) 100vw, 629px\" \/><\/figure>\n\n\n\n<p>When a number is <strong>positive<\/strong>, its absolute value <strong>stays the same<\/strong>. But when a number is <strong>negative<\/strong>, you just <strong>take away the minus sign<\/strong> to get its absolute value. And when the number is zero, its absolute value is also zero because it&#8217;s right on top of zero on the number line.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"565\" height=\"243\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/illustration.png\" alt=\"Illustration of Absolute Value\" class=\"wp-image-29969\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/illustration.png 565w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/illustration-300x129.png 300w\" sizes=\"(max-width: 565px) 100vw, 565px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><strong>Why Do We Need Absolute Value?<\/strong><\/strong><\/h3>\n\n\n\n<p>Here&#8217;s why:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It helps us know how big a number is, ignoring whether it&#8217;s positive or negative. This is helpful with things like distances or sizes, where the direction doesn&#8217;t matter.<\/li>\n\n\n\n<li>It is crucial in solving equations and inequalities.<\/li>\n\n\n\n<li>In geometry and physics, we use absolute value to find distances between points or other things we&#8217;re studying in math.<\/li>\n\n\n\n<li>In computer programming, absolute value functions are used a lot for different reasons, like dealing with errors, organizing data, and doing math calculations.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">numpy.fabs() in Python<\/h2>\n\n\n\n<p>Numpy offers us a <strong>fabs() function<\/strong>, short for &#8220;<strong>absolute value<\/strong>&#8220;, it helps find the absolute value of numbers, whether it&#8217;s one number or a bunch of them stored in a list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Syntax<\/strong>:<\/h3>\n\n\n\n<p>The syntax for using fabs() is simple. You just need to call it as a function from the numpy module and give it the number or list of numbers you want to find the absolute values for. It looks like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.fabs(x)\n<\/pre><\/div>\n\n\n<p>Here, <strong>x<\/strong> can represent various types of inputs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A scalar (single numeric value)<\/li>\n\n\n\n<li>A one-dimensional array (numpy array, list, tuple, etc.)<\/li>\n\n\n\n<li>A multidimensional array<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How Does It Work?<\/strong><\/h3>\n\n\n\n<p>If you give the function just one number, it finds the absolute value of that number and gives it back to you. If you give it a bunch of numbers in an array, it goes through each number one by one, finding the absolute value for each. If it&#8217;s a simple list of numbers, it finds the absolute value of each number individually. But if it&#8217;s a more complicated array with multiple layers, it still does the same thing, going through each number and finding its absolute value.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"509\" height=\"661\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-03-121424-2.png\" alt=\"Flow Chart\" class=\"wp-image-29974\" style=\"width:363px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-03-121424-2.png 509w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Screenshot-2024-05-03-121424-2-231x300.png 231w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/><\/figure>\n\n\n\n<p>The fabs() function works well with <strong>whole numbers<\/strong>, but it struggles with <strong>decimals and complex numbers.<\/strong> If you use fabs() with arrays containing decimals, complex numbers or NaN values, it might give wrong results or cause errors. To handle decimals and complex numbers correctly, you should use <strong>np.abs()<\/strong> instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong>Finding Absolute Values Using numpy.fabs() in Python<\/strong><\/strong><\/h2>\n\n\n\n<p>Now that we know a bit about this function, let&#8217;s try to understand it more using code examples.<\/p>\n\n\n\n<p><strong>Example 1<\/strong>: Firstly we will test with scalar input.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nnumber = int(input(&quot;Enter the Number : &quot;))\n\nprint(&quot;The absolute value of the given value is: &quot;, np.fabs(number))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"561\" height=\"197\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-18.png\" alt=\"Output of Example 1\" class=\"wp-image-30074\" style=\"width:376px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-18.png 561w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-18-300x105.png 300w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/><\/figure>\n\n\n\n<p><b>Example 2<\/b>: Now let&#8217;s provide an array input.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = np.array(&#x5B;0, -5, 10, 15, -20])\n\nprint(&quot;The array containing the absolute values of all elements is: &quot;, np.fabs(arr))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"975\" height=\"132\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-19.png\" alt=\"Output of Example 2\" class=\"wp-image-30075\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-19.png 975w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-19-300x41.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-19-768x104.png 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><\/figure>\n\n\n\n<p><b>Example 3<\/b>: Let&#8217;s see what happens with a 2-D array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = &#x5B;&#x5B;0, -5, 10], &#x5B;2, 3, -6], &#x5B;7, -9, 13]]\n\nprint(&quot;The array containing the absolute values of all elements is: &quot;, np.fabs(arr))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"935\" height=\"207\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-20.png\" alt=\"Output of Example 3\" class=\"wp-image-30076\" style=\"width:578px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-20.png 935w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-20-300x66.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-20-768x170.png 768w\" sizes=\"(max-width: 935px) 100vw, 935px\" \/><\/figure>\n\n\n\n<p><b>Example 4<\/b>: If we input complex, decimal, or NaN values, the function will throw an error as it does not support this type of input.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narray_2d = np.array(&#x5B;\n    &#x5B;1.5, 2.3, np.nan, 4.7 + 3.2j],\n    &#x5B;5.6, 6.8, 8.1, 9.4],\n    &#x5B;10.2, np.nan, 12.5 + 1.8j, 13.9]\n])\n\nprint(&quot;The array containing the absolute values of all elements is: &quot;, np.fabs(array_2d))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"138\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-21-1024x138.png\" alt=\"Output of Example 4\" class=\"wp-image-30077\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-21-1024x138.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-21-300x40.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-21-768x104.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-21-1536x207.png 1536w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-21.png 1751w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong>Finding Absolute Values Using numpy.abs() in Python<\/strong><\/strong><\/h2>\n\n\n\n<p>There are some drawbacks to the fabs() function. Python&#8217;s abs() functions work as a powerful alternative to this problem. This function finds how far a number is from zero, no matter if it&#8217;s positive or negative. For example , <strong>abs(-2)<\/strong> gives <strong>2<\/strong>, and <strong>abs(2)<\/strong> also gives <strong>2<\/strong>. It works with different types of numbers like <strong>integers<\/strong>, <strong>decimals<\/strong>, and <strong>complex numbers.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Syntax<\/strong>:<\/h3>\n\n\n\n<p>The root syntax for this function is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nabs(a)\n<\/pre><\/div>\n\n\n<p>Where &#8220;<strong>a<\/strong>&#8221; is the number or array you want to find the absolute value of.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<p>Let&#8217;s understand using the previous example of fabs() where we got stuck.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narray_2d = np.array(&#x5B;\n    &#x5B;1.5, 2.3, np.nan, 4.7 + 3.2j],\n    &#x5B;5.6, 6.8, 8.1, 9.4],\n    &#x5B;10.2, np.nan, 12.5 + 1.8j, 13.9]\n])\n\nprint(&quot;The array containing the absolute values of all elements is: &quot;, np.abs(array_2d))\n<\/pre><\/div>\n\n\n<p>In this result, we clearly see that the <strong>abs()<\/strong> function sorts out our problem! It prints the absolute value of complex, decimal, and NaN values.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"145\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-22-1024x145.png\" alt=\"Output of numpy.abs() in Python \" class=\"wp-image-30078\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-22-1024x145.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-22-300x43.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-22-768x109.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-22.png 1325w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>And that&#8217;s it for this guide! We&#8217;ve covered every important detail of the <strong>fabs()<\/strong> and <strong>abs() <\/strong>functions. We&#8217;ve gained a great understanding of what absolute value is and its importance. We&#8217;ve learned how to implement it in Python with some good examples. I hope you&#8217;ve followed along well and enjoyed it.<\/p>\n\n\n\n<p>You may also learn <a href=\"https:\/\/codeforgeek.com\/numpy-diff-python\/\">how to calculate differences easily<\/a> and also <a href=\"https:\/\/codeforgeek.com\/round-numbers-in-python\/\">how to round numbers in Python.<\/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.fabs.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.fabs.html<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello coders! Welcome to a new chapter of Python guides. Today, we will see how to calculate absolute values in Python using NumPy. We&#8217;ll explore the NumPy fabs() function, understand its syntax, and how it works, and see examples, demonstrations, and its drawbacks. We will also see how to address the given disadvantages. So, stay [&hellip;]<\/p>\n","protected":false},"author":104,"featured_media":29979,"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-29967","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\/Finding-Absolute-Value-in-Python.png",1200,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Finding-Absolute-Value-in-Python-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Finding-Absolute-Value-in-Python-300x150.png",300,150,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Finding-Absolute-Value-in-Python-768x384.png",768,384,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Finding-Absolute-Value-in-Python-1024x512.png",1024,512,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Finding-Absolute-Value-in-Python.png",1200,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Finding-Absolute-Value-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":"Hello coders! Welcome to a new chapter of Python guides. Today, we will see how to calculate absolute values in Python using NumPy. We&#8217;ll explore the NumPy fabs() function, understand its syntax, and how it works, and see examples, demonstrations, and its drawbacks. We will also see how to address the given disadvantages. So, stay&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/29967","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=29967"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/29967\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/29979"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=29967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=29967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=29967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}