{"id":21576,"date":"2023-08-29T19:46:33","date_gmt":"2023-08-29T14:16:33","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=21576"},"modified":"2023-08-29T19:56:01","modified_gmt":"2023-08-29T14:26:01","slug":"numpy-cbrt-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-cbrt-python\/","title":{"rendered":"numpy.cbrt() in Python: Calculating Cube Roots in NumPy"},"content":{"rendered":"\n<p><strong>NumPy<\/strong> is a famous and often-used Python library that provides various mathematical <a href=\"https:\/\/codeforgeek.com\/python-functions-with-examples\/\" data-type=\"URL\" data-id=\"https:\/\/codeforgeek.com\/python-functions-with-examples\/\" target=\"_blank\" rel=\"noreferrer noopener\">functions<\/a> when it comes to performing operations on arrays. These functions make computations involving array elements easier and more efficient. In this article, we&#8217;ll look at one such function, <strong>numpy.cbrt()<\/strong> which is used for calculating cube roots in Python. We&#8217;ll understand the implementation of this function in depth along with some examples.<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/codeforgeek.com\/python-comments\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/codeforgeek.com\/python-comments\/\" rel=\"noreferrer noopener\">An Introduction to Python Comments<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing numpy.cbrt() Function<\/h2>\n\n\n\n<p>When it comes to performing operations like finding the cube root on array elements, we would be required to loop through each of those array elements and perform the cube root operation at each iteration. As easy as it may be, <strong>NumPy<\/strong> provides an even easier method of finding cube roots using the <strong>numpy.cbrt()<\/strong> method. <\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.cbrt(x, \/, out=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x:<\/strong> Input values that we need to find the cube root of, can be an array, matrices or single values,<\/li>\n\n\n\n<li><strong>out:<\/strong> Name of the output array. If set to <strong>None<\/strong> or not given a value, a new array will be created to store the resultant array containing cube roots,<\/li>\n\n\n\n<li><strong>where:<\/strong> Used to specify the range of elements whose cube roots have to be found. This parameter has a default of <strong>True<\/strong>, meaning the cube roots of all the elements in the array should be computed, <\/li>\n\n\n\n<li><strong>casting:<\/strong> Used to specify whether data type casting is allowed in the resultant array. Here, <strong>same_kind <\/strong>indicates that casting can be done between the same kind of data types (eg: casting can be done between float and integers as they are both numeric data types),<\/li>\n\n\n\n<li><strong>order: <\/strong>Used to specify the memory layout of the output array. Here,<strong> K <\/strong>indicates that the original order of the input array should be preserved when laying out the memory for the output array,<\/li>\n\n\n\n<li><strong>dtype:<\/strong> Used to specify the data type of the output array. Here, <strong>None<\/strong> indicates that the data type of the output array must be inferred according to that of the input array.<\/li>\n<\/ul>\n\n\n\n<p>Now let us understand <strong>numpy.cbrt()<\/strong> in detail with some examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Calculating Cube Root of Each Element in NumPy Array<\/h2>\n\n\n\n<p>Here, we&#8217;ll look at some examples to understand how we can use<strong> numpy.cbrt()<\/strong> to find cube roots of elements, whether they are single or in an array. We will also see how changing the parameters of the function can change the output array and how we can extend this function to 2D arrays (matrices).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calculating Cube Root of Single Values (Scalars)<\/h3>\n\n\n\n<p>We&#8217;ll start off with a simple example, where we will use <strong>numpy.cbrt()<\/strong> to find the cube root of a single value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nx = 8\ncube_root = np.cbrt(x)\nprint(&quot;Cube root of&quot;, x, &quot;is&quot;, cube_root)\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=\"694\" height=\"278\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Calculating-cube-root-of-scalar-using-numpy.cbrt_.png\" alt=\"Calculating cube root of scalar using numpy.cbrt()\" class=\"wp-image-21606\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Calculating-cube-root-of-scalar-using-numpy.cbrt_.png 694w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Calculating-cube-root-of-scalar-using-numpy.cbrt_-300x120.png 300w\" sizes=\"(max-width: 694px) 100vw, 694px\" \/><figcaption class=\"wp-element-caption\">Calculating cube root of scalar using <strong>numpy.cbrt()<\/strong><\/figcaption><\/figure>\n\n\n\n<p>This function can also be used to handle negative scalar values. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calculating Cube Roots of All Elements in an Array<\/h3>\n\n\n\n<p>Now we&#8217;ll apply <strong>numpy.cbrt()<\/strong> to an array (<strong>arr<\/strong>) of perfect cubes and calculate the cube roots of the elements present in that 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;1, 8, 27, 64]\ncube_roots = np.cbrt(arr)\n\nprint(&quot;Cube Roots:&quot;, cube_roots)\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=\"573\" height=\"309\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Using-numpy.cbrt-on-arrays.png\" alt=\"Using numpy.cbrt() on arrays\" class=\"wp-image-21609\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Using-numpy.cbrt-on-arrays.png 573w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Using-numpy.cbrt-on-arrays-300x162.png 300w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/><figcaption class=\"wp-element-caption\">Using <strong>numpy.cbrt()<\/strong> on arrays<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Using the where Parameter to Calculating Cube Root of Specific Elements<\/h3>\n\n\n\n<p>As mentioned earlier, the <strong>where<\/strong> parameter is used to find cube roots of only specific elements in an array. Here, we&#8217;ll find the cube roots of only the positive numbers in the input array. <\/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;1, -8, -27, 64])\ncube_roots = np.cbrt(arr, where=(arr&gt;0))\n\nprint(&quot;Cube Roots:&quot;, cube_roots)\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\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Using-the-where-parameter-to-find-cube-root-of-specific-elements-in-array.png\" alt=\"Using the where parameter to find the cube root of specific elements in array\" class=\"wp-image-21610\" width=\"644\" height=\"346\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Using-the-where-parameter-to-find-cube-root-of-specific-elements-in-array.png 588w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/Using-the-where-parameter-to-find-cube-root-of-specific-elements-in-array-300x161.png 300w\" sizes=\"(max-width: 644px) 100vw, 644px\" \/><figcaption class=\"wp-element-caption\">Using the where parameter to find the cube root of specific elements in an array<\/figcaption><\/figure>\n\n\n\n<p>Here only the cube roots of positive elements are present in the output array, the negative values are represented by <strong>nan<\/strong> which is used to represent undefined values in an array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calculating Cube Roots of Elements in a Matrix<\/h3>\n\n\n\n<p>The usage of <strong>numpy.cbrt()<\/strong> can be extended from 1D array to 2D array or matrices as well. Here, we&#8217;ll look at how we can find the cube roots of all the elements in a 2&#215;3 matrix. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nmatrix = &#x5B;&#x5B;8, 27, 64], \n          &#x5B;125, 216, 343]]\n\ncube_roots = np.cbrt(matrix)\n\nprint(&quot;Cube Roots:&quot;)\nprint(cube_roots)\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=\"497\" height=\"471\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/using-numpy.cbrt-on-a-matrix.png\" alt=\"Using numpy.cbrt() on a matrix\" class=\"wp-image-21611\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/using-numpy.cbrt-on-a-matrix.png 497w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/using-numpy.cbrt-on-a-matrix-300x284.png 300w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><figcaption class=\"wp-element-caption\">Using <strong>numpy.cbrt()<\/strong> on a matrix<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>As we have seen in this article, NumPy provides us with the <strong>numpy.cbrt()<\/strong> function which makes computation the cube roots of single elements (scalars), array elements or matrix elements easier and more efficient. We looked at all of these implementations with supporting examples and also how we can find the cube roots of only a specific range of elements in an array. By knowing how to effectively use <strong>numpy.cbrt()<\/strong> we can perform calculations on arrays easier than before. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reference<\/h3>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.cbrt.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.cbrt.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy is a famous and often-used Python library that provides various mathematical functions when it comes to performing operations on arrays. These functions make computations involving array elements easier and more efficient. In this article, we&#8217;ll look at one such function, numpy.cbrt() which is used for calculating cube roots in Python. We&#8217;ll understand the implementation [&hellip;]<\/p>\n","protected":false},"author":96,"featured_media":21628,"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-21576","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\/08\/numpy.cbrt-Calculating-cube-roots-in-Python.png",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/numpy.cbrt-Calculating-cube-roots-in-Python-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/numpy.cbrt-Calculating-cube-roots-in-Python-300x180.png",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/numpy.cbrt-Calculating-cube-roots-in-Python-768x461.png",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/numpy.cbrt-Calculating-cube-roots-in-Python.png",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/numpy.cbrt-Calculating-cube-roots-in-Python.png",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/08\/numpy.cbrt-Calculating-cube-roots-in-Python.png",1000,600,false]},"uagb_author_info":{"display_name":"Nandana Pradosh","author_link":"https:\/\/codeforgeek.com\/author\/nandana\/"},"uagb_comment_info":0,"uagb_excerpt":"NumPy is a famous and often-used Python library that provides various mathematical functions when it comes to performing operations on arrays. These functions make computations involving array elements easier and more efficient. In this article, we&#8217;ll look at one such function, numpy.cbrt() which is used for calculating cube roots in Python. We&#8217;ll understand the implementation&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21576","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\/96"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=21576"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21576\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/21628"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=21576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=21576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=21576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}