{"id":29982,"date":"2024-05-27T18:38:20","date_gmt":"2024-05-27T13:08:20","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=29982"},"modified":"2024-05-27T18:38:21","modified_gmt":"2024-05-27T13:08:21","slug":"log2-function-in-numpy","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/log2-function-in-numpy\/","title":{"rendered":"What is the log2 Function in NumPy?"},"content":{"rendered":"\n<p>Welcome to the next chapter of our NumPy logarithmic series. I hope you&#8217;ve gone through the first one, i.e., <a href=\"https:\/\/codeforgeek.com\/log10-function-in-numpy\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/log10-function-in-numpy\/\">Log10<\/a>. Here we will discuss another important log function, which is <strong>log2<\/strong>. It helps us to calculate the logarithm base 2 of the input. We will delve into its concept, syntax, parameters, and real-life use cases, so let&#8217;s continue further.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is log2?<\/h2>\n\n\n\n<p>This mathematical function tells us how many times we have to multiply the <strong>base (2)<\/strong> by <strong>itself <\/strong>to get the given number.<\/p>\n\n\n\n<p>Let&#8217;s say you have a number, like<strong> 8<\/strong>. Now, you want to know how many times you need to multiply <strong>2<\/strong> by itself to get that number. That&#8217;s where <strong>log2<\/strong> (<em>pronounced &#8220;log base 2&#8221;<\/em>) comes in.<\/p>\n\n\n\n<p>For example, with <strong>8<\/strong>, you write it like <strong>log2(8) = x<\/strong>. The <strong>&#8216;x&#8217;<\/strong> tells you how many times you need to multiply <strong>2<\/strong> to get <strong>8<\/strong>. Here, <strong>x is 3<\/strong> because <strong>2 multiplied by itself<\/strong> <strong>3 times equals 8<\/strong>.<\/p>\n\n\n\n<p><strong>Here&#8217;s how it goes:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"891\" height=\"403\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-2.png\" alt=\"Illustration of log2 \" class=\"wp-image-29998\" style=\"width:447px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-2.png 891w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-2-300x136.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-2-768x347.png 768w\" sizes=\"(max-width: 891px) 100vw, 891px\" \/><\/figure>\n\n\n\n<p>This simple idea of log2 is used in many fields, especially in <strong>computer science.<\/strong> It helps figure out things like how fast a computer algorithm works. For example, in <strong>binary search<\/strong>, log2 is used to know how many steps it takes to find something in a sorted list.<\/p>\n\n\n\n<p>So, log2 helps us understand how things grow exponentially and is very helpful in many situations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">numpy.log2() in Python<\/h2>\n\n\n\n<p>This Numpy function finds the log of numbers, but only to the base 2. We can use it on lists or arrays of numbers, and it gives us back a new list or array where each number has been turned into its <strong>base-2 log<\/strong>. So, if we have a list of numbers like <strong>[2, 4, 8], numpy.log2()<\/strong> will give us<strong> [1, 2, 3].<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p>Let&#8217;s take a glimpse at its syntax:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.log2(x, \/, out=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None, subok=True&#x5B;, signature, extobj])\n<\/pre><\/div>\n\n\n<p>The parameters used here are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x:<\/strong> Input array or object.<\/li>\n\n\n\n<li><strong>out:<\/strong> Output array where the result is placed.<\/li>\n\n\n\n<li><strong>dtype:<\/strong> Data-type of the output. If not given, infer the data type from <strong>x<\/strong>.<\/li>\n\n\n\n<li><strong>where:<\/strong> This parameter is used to select elements based on conditions.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Working:<\/h3>\n\n\n\n<p>Here&#8217;s an explanation of how <strong>numpy.log2()<\/strong> works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> We give it a bunch of numbers, like <strong>[1, 2, 4, 8, 16].<\/strong><\/li>\n\n\n\n<li><strong>Operation:<\/strong> It calculates the base-2 logarithm for each number. For example, <strong>log2(1)<\/strong> is <strong>0<\/strong>, <strong>log2(2)<\/strong> is <strong>1<\/strong>, and so on.<\/li>\n\n\n\n<li><strong>Output:<\/strong> It gives us back a new list with the results, like <strong>[0, 1, 2, 3, 4]<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p>It&#8217;s fast because it can handle lots of numbers at once, making it quicker than doing each calculation one by one. And it&#8217;s versatile too, able to handle different types of data for all sorts of tasks in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using numpy.log2() in Python<\/h2>\n\n\n\n<p>Now we will see how we can implement the Numpy log2() function in Python. Observe the given examples:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1:<\/strong> <\/h3>\n\n\n\n<p>Let&#8217;s start by testing it on a <strong>single number<\/strong>.<\/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;Please enter an integer: &quot;))\nresult = np.log2(number)\nprint(&quot;2 will multiply itself&quot;, result, &quot;times to get the given number.&quot;)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"655\" height=\"151\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-28.png\" alt=\"Example 1 Output\" class=\"wp-image-30088\" style=\"width:438px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-28.png 655w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-28-300x69.png 300w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 2<\/strong>: <\/h3>\n\n\n\n<p>Now we will calculate log2 for each element in a <strong>1-D array<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr = np.array(&#x5B;64, 32, 4, 128, 16])\nresult = np.log2(arr)\nprint(&quot;Here is the array containing power of two of each element in array: &quot;, result)  \n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"982\" height=\"127\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-29.png\" alt=\"Example 2 Output\" class=\"wp-image-30089\" style=\"width:680px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-29.png 982w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-29-300x39.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-29-768x99.png 768w\" sizes=\"(max-width: 982px) 100vw, 982px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 3:<\/strong> <\/h3>\n\n\n\n<p>The log2() function is able to perform <strong>broadcasting<\/strong>. Here, let&#8217;s test it by broadcasting a scalar quantity.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nscalar = 4\narr = np.array(&#x5B;0, 4, 16, 8, 64])\nresult = np.log2(scalar * arr)\nprint(result) \n<\/pre><\/div>\n\n\n<p>In the line <strong>result = np.log2(scalar * arr)<\/strong>, we do two things:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, we <strong>quadruple<\/strong> each number in the array <strong>arr<\/strong> by multiplying it by the scalar value (<em>which is 4<\/em>).<\/li>\n\n\n\n<li>Then, we find the base-2 logarithm of each of these quadrupled numbers. This tells us how many times we need to multiply 2 by itself to get each of the quadrupled numbers.<\/li>\n\n\n\n<li>Also for <strong>0<\/strong>, the <strong>logarithm of 0 is undefined<\/strong>, so it will be represented as <strong>\u2212\u221e<\/strong>.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"935\" height=\"195\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-30.png\" alt=\"Example 3 Output\" class=\"wp-image-30090\" style=\"width:566px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-30.png 935w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-30-300x63.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-30-768x160.png 768w\" sizes=\"(max-width: 935px) 100vw, 935px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 4:<\/strong> <\/h3>\n\n\n\n<p>Let&#8217;s try to use the <strong>where<\/strong> parameter in this function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr = np.array(&#x5B;0, 8, 2, 64, 4])\ncondition = (arr &gt; 4)\nresult = np.log2(arr, where=condition)\nprint(result)\n<\/pre><\/div>\n\n\n<p>In this example, since the condition <strong>(arr &gt; 4)<\/strong> is true only for elements <strong>8<\/strong>, and <strong>64<\/strong>, logarithms are calculated only for these elements, resulting in <strong>nan<\/strong> or <strong>garbage value<\/strong> for the rest of the elements.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"946\" height=\"177\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-31.png\" alt=\"Example 4 Output\" class=\"wp-image-30091\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-31.png 946w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-31-300x56.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-31-768x144.png 768w\" sizes=\"(max-width: 946px) 100vw, 946px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>And that&#8217;s all you need to know about the log2() function in NumPy. We have gone through the idea of logarithm base two, its use, and working. Now you are geared up with the knowledge of its syntax, parameters, and visualization through examples and implementations.<\/p>\n\n\n\n<p>Do you like the explanation of this logarithmic function? If yes, then do read about the next function, i.e., <a href=\"https:\/\/codeforgeek.com\/numpy-log1p-in-python\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-log1p-in-python\/\">log1p()<\/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.log2.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.log2.html<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to the next chapter of our NumPy logarithmic series. I hope you&#8217;ve gone through the first one, i.e., Log10. Here we will discuss another important log function, which is log2. It helps us to calculate the logarithm base 2 of the input. We will delve into its concept, syntax, parameters, and real-life use cases, [&hellip;]<\/p>\n","protected":false},"author":104,"featured_media":30093,"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-29982","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\/Numpy-log2-A-Complete-Guide.png",1200,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Numpy-log2-A-Complete-Guide-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Numpy-log2-A-Complete-Guide-300x150.png",300,150,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Numpy-log2-A-Complete-Guide-768x384.png",768,384,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Numpy-log2-A-Complete-Guide-1024x512.png",1024,512,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Numpy-log2-A-Complete-Guide.png",1200,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Numpy-log2-A-Complete-Guide.png",1200,600,false]},"uagb_author_info":{"display_name":"Snigdha Keshariya","author_link":"https:\/\/codeforgeek.com\/author\/snigdha\/"},"uagb_comment_info":0,"uagb_excerpt":"Welcome to the next chapter of our NumPy logarithmic series. I hope you&#8217;ve gone through the first one, i.e., Log10. Here we will discuss another important log function, which is log2. It helps us to calculate the logarithm base 2 of the input. We will delve into its concept, syntax, parameters, and real-life use cases,&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/29982","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=29982"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/29982\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/30093"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=29982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=29982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=29982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}