{"id":23831,"date":"2023-12-28T15:48:30","date_gmt":"2023-12-28T10:18:30","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=23831"},"modified":"2023-12-28T15:48:32","modified_gmt":"2023-12-28T10:18:32","slug":"numpy-modf-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-modf-in-python\/","title":{"rendered":"numpy.modf() in Python: Introduction, Syntax &amp; Examples"},"content":{"rendered":"\n<p>The <strong>numpy.modf() <\/strong>function in Python is a part of the NumPy library that can be used to separate the fractional and integer parts of an array of floating-point numbers. It returns two arrays &#8211; one containing the fractional parts and another containing the integer parts.<\/p>\n\n\n\n<p>In this article, we will understand&nbsp;Python&nbsp;<strong>numpy.modf()<\/strong>&nbsp;function, its syntax, and we will also&nbsp;see the variety of techniques using which we can separate the fractional and integer parts of an array of floating-point numbers with the help of this function. Let\u2019s get started.<\/p>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/codeforgeek.com\/numpy-where-in-python\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-where-in-python\/\">numpy.where() in Python<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of numpy.modf() Function<\/h2>\n\n\n\n<p>Let us get started by understanding the fundamental constituents of the<em>&nbsp;<\/em><strong>numpy.modf()<\/strong> function with the help of its syntax given below.<\/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.modf(x, \/, out1=None, out2=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x:<\/strong> This is the input array for which you want to separate the fractional and integer parts.<\/li>\n\n\n\n<li><strong>out1 (optional):<\/strong> This is an optional parameter that specifies the location where the fractional parts should be stored. If not provided, a new array will be created.<\/li>\n\n\n\n<li><strong>out2 (optional):<\/strong> This is an optional parameter that specifies the location where the integer parts should be stored. If not provided, a new array will be created.<\/li>\n\n\n\n<li><strong>where (optional):<\/strong>&nbsp;This parameter is used for conditional execution. If provided, only the elements where the condition is True will be included in the result.<\/li>\n\n\n\n<li><strong>casting (optional):<\/strong>&nbsp;This parameter controls what kind of data casting may occur. The default is \u2018same_kind\u2019, which means that only casting between similar kinds (e.g., float to float) is allowed.<\/li>\n\n\n\n<li><strong>order (optional):<\/strong>&nbsp;Specifies the memory layout of the result. The default is \u2018K\u2019 which means the order is determined by the input array\u2019s memory layout.<\/li>\n\n\n\n<li><strong>dtype (optional):<\/strong>&nbsp;This parameter allows us to set the data type of the output array. If not specified, it is determined by the data types of the input arrays.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return:<\/strong><\/p>\n\n\n\n<p>The <strong>numpy.modf()<\/strong> function returns two arrays, let&#8217;s say<strong> out1<\/strong> and <strong>out2<\/strong> where the <strong>out1<\/strong> array contains the fractional parts of the input array and the<strong> out2<\/strong> array contains the integer parts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of numpy.modf() Function<\/h2>\n\n\n\n<p>Let us now look at some examples to demonstrate the use of&nbsp;<strong>numpy.modf()<\/strong>&nbsp;function in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Separating Integer and Fractional Part from NumPy Array<\/h3>\n\n\n\n<p>In this section, we will try to separate integer and fractional parts from a one-dimensional NumPy array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar = np.array(&#x5B;13.11, 24.12, 21.0, 235.57, 88.10])\nfract = np.empty_like(ar)\ninte = np.empty_like(ar)\nnp.modf(ar, out=(fract, inte))\n\nprint(&quot;The Original array:&quot;, ar)\nprint(&quot;Fractional part of the array:&quot;, fract)\nprint(&quot;Integer part of the array:&quot;, inte)\n<\/pre><\/div>\n\n\n<p>Here we first imported the NumPy library as <strong>np<\/strong> then using it we created a 1-D array that contains integer and fractional parts and saved it into a variable called <strong>ar<\/strong>. <\/p>\n\n\n\n<p>Then by using the <strong>numpy.empty_like() <\/strong>function we created two empty arrays: <strong>fract <\/strong>and <strong>inte<\/strong> with the same shape as the array <strong>ar<\/strong> and passed these two arrays to the<strong> &#8216;out<\/strong>&#8216; parameter along with our original array <strong>ar<\/strong> in the<strong> numpy.modf()<\/strong> function so that the fractional and integer parts which we will get from the <strong>numpy.modf() <\/strong>function can be stored in the <strong>fract<\/strong> and <strong>inte <\/strong>array respectively. In last, we printed our original array and its fractional part and integer part.<\/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=\"1020\" height=\"101\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1939.png\" alt=\"Separating Integer and Fractional Part from NumPy Array\" class=\"wp-image-24163\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1939.png 1020w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1939-300x30.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1939-768x76.png 768w\" sizes=\"(max-width: 1020px) 100vw, 1020px\" \/><\/figure>\n\n\n\n<p>We can see in the output that the fractional and integer parts of our original array got separated.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Deciding on Which Elements to Perform the Operation<\/h3>\n\n\n\n<p>Let&#8217;s see the changes in the output when we pass the<strong>\u00a0\u2018where\u2019\u00a0<\/strong>parameter with an input array in the\u00a0<strong>numpy.modf()<\/strong>\u00a0function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar = np.array(&#x5B;11.1, 1.1, 12.3, 33.3])\nmaskCond = np.array(&#x5B;True, False, True, False])\nresultF,resultI = np.modf(ar,where=maskCond)\n\nprint(resultF)\nprint(resultI)\n<\/pre><\/div>\n\n\n<p>Here we created a 1D array but this time we also created a<strong> boolean <\/strong>array<strong> <\/strong>saved it into a <strong>maskCond<\/strong> variable and passed that boolean array to the &#8216;<strong>where&#8217; <\/strong>parameter with our original array <strong>ar <\/strong>into the <strong>numpy.modf() <\/strong>function. Let&#8217;s see the result.<\/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=\"821\" height=\"75\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/12\/Screenshot-1961.png\" alt=\"Deciding on Which Elements to Perform the Operation\" class=\"wp-image-24610\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/12\/Screenshot-1961.png 821w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/12\/Screenshot-1961-300x27.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/12\/Screenshot-1961-768x70.png 768w\" sizes=\"(max-width: 821px) 100vw, 821px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Returning Tuple from numpy.modf() Function<\/h3>\n\n\n\n<p>If you want to return a tuple of fractional and integer parts through <strong>numpy.modf() <\/strong>function then don&#8217;t pass the<strong> &#8216;out&#8217;<\/strong> parameter in the <strong>numpy.modf() <\/strong>function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar = np.array(&#x5B;125.9,227.4,56.15,67.777,11.12])\nresult = np.modf(ar)\n\nprint(result)\n<\/pre><\/div>\n\n\n<p>For this example, we haven&#8217;t passed the<strong> &#8216;out&#8217; <\/strong>parameter in <strong>numpy.modf()<\/strong> function that&#8217;s why this function returned the tuple this time which contains both the arrays: <strong>fractional <\/strong>and<strong> integer<\/strong>.<\/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=\"1006\" height=\"78\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1941.png\" alt=\"Returning Tuple from numpy.modf() Function\" class=\"wp-image-24165\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1941.png 1006w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1941-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1941-768x60.png 768w\" sizes=\"(max-width: 1006px) 100vw, 1006px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Passing Parameter &#8216;dtype&#8217; in numpy.modf() Function<\/h3>\n\n\n\n<p>The <strong>numpy.modf()<\/strong> function always returns the fractional and integer array with the same data type as the input array. So, when we pass an input array with the integer data type then how can we change it to float or some other data type? Let&#8217;s see that.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar = np.array(&#x5B;9, 2, 19])\nfract, inte = np.modf(ar, dtype=float)\n\nprint(&quot;The Original array:&quot;, ar)\nprint(&quot;Fractional part of the array:&quot;, fract)\nprint(&quot;Integer part of the array:&quot;, inte)\n<\/pre><\/div>\n\n\n<p>In this example, we have created a 1D array like the above examples but here the only difference is all the elements of the array are of integer data type and not the<strong> float<\/strong> data type. So we have passed the parameter<strong> &#8216;dtype&#8217; <\/strong>as<strong> float<\/strong> to change the data type of the output array to float. That&#8217;s why we got our output arrays (fractional and integer parts) of data type float.<\/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=\"915\" height=\"97\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1942.png\" alt=\"Passing Parameter 'dtype' in numpy.modf() Function\" class=\"wp-image-24166\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1942.png 915w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1942-300x32.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1942-768x81.png 768w\" sizes=\"(max-width: 915px) 100vw, 915px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have discussed<strong>\u00a0numpy.modf()<\/strong>\u00a0function provided by\u00a0<strong>Python\u2019s NumPy library<\/strong>, its syntax, and parameters, and also explored a few examples to get an understanding of it. After reading this tutorial, we hope you can easily separate the fractional and integer parts of an array of floating-point numbers.<\/p>\n\n\n\n<p><strong>Read More:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/codeforgeek.com\/python-numpy-reciprocal-function\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/python-numpy-reciprocal-function\/\">numpy.reciprocal() in Python<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/codeforgeek.com\/numpy-linspace-in-python\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-linspace-in-python\/\">numpy.linspace() in Python<\/a><\/li>\n<\/ul>\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.modf.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.modf.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The numpy.modf() function in Python is a part of the NumPy library that can be used to separate the fractional and integer parts of an array of floating-point numbers. It returns two arrays &#8211; one containing the fractional parts and another containing the integer parts. In this article, we will understand&nbsp;Python&nbsp;numpy.modf()&nbsp;function, its syntax, and we [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":24161,"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-23831","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\/11\/numpy.modf-in-Python-Introduction-Syntax-Examples.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.modf-in-Python-Introduction-Syntax-Examples-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.modf-in-Python-Introduction-Syntax-Examples-300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.modf-in-Python-Introduction-Syntax-Examples-768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.modf-in-Python-Introduction-Syntax-Examples-1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.modf-in-Python-Introduction-Syntax-Examples.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.modf-in-Python-Introduction-Syntax-Examples.png",1200,800,false]},"uagb_author_info":{"display_name":"Priyanshu Singh","author_link":"https:\/\/codeforgeek.com\/author\/priyanshu\/"},"uagb_comment_info":0,"uagb_excerpt":"The numpy.modf() function in Python is a part of the NumPy library that can be used to separate the fractional and integer parts of an array of floating-point numbers. It returns two arrays &#8211; one containing the fractional parts and another containing the integer parts. In this article, we will understand&nbsp;Python&nbsp;numpy.modf()&nbsp;function, its syntax, and we&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23831","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\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=23831"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23831\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/24161"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=23831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=23831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=23831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}