{"id":14314,"date":"2021-03-31T16:22:02","date_gmt":"2021-03-31T16:22:02","guid":{"rendered":"https:\/\/www.askpython.com\/?p=14314"},"modified":"2021-03-31T16:22:04","modified_gmt":"2021-03-31T16:22:04","slug":"random-sampling-in-numpy","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/random-sampling-in-numpy","title":{"rendered":"4 Ways to Perform Random Sampling in NumPy"},"content":{"rendered":"\n<p>Hello, readers! In this article, we will be focusing on <strong>4 Easy Ways to Perform Random Sampling<\/strong> in Python <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" target=\"_blank\" aria-label=\"NumPy (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">NumPy<\/a>.<\/p>\n\n\n\n<p>So, let us get started! \ud83d\ude42<\/p>\n\n\n\n<p>Random Sampling, to give an overview, is actually selecting random values from the defined type of data and present them to be in further use.<\/p>\n\n\n\n<p>In the course of this topic, we will be having a look at the below functions&#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>NumPy random_sample() method<\/strong><\/li><li><strong>NumPy ranf() method<\/strong><\/li><li><strong>NumPy random_integers() method<\/strong><\/li><li><strong>NumPy randint() method<\/strong><\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"eh-1\"><\/span><span id=\"eh-1\"><\/span>1. NumPy random_sample() method for Random Sampling<\/h2>\n\n\n\n<p>With <strong>random_sample() method<\/strong>, we can sample the data values and choose random data fat ease. It selects random samples between [0.0 &#8211; 1.0] only. We can build a single sample as well as an entire array based on random values.<\/p>\n\n\n\n<p>Have a look at the below syntax!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nrandom.random_sample()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>In the below example, at first, we have performed random sampling and generated a single random value. Further, we have created a 2-dimensional array with random values by passing size as a parameter to the random_sample() function.<\/p>\n\n\n\n<p>Note it that the random values would range between 0.0 to 1.0 only. Plus, random_sample() function generates random values of float type.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nran_val = np.random.random_sample()\nprint (&quot;Random value : &quot;, ran_val)\n\nran_arr = np.random.random_sample(size =(2, 4))\nprint (&quot;Array filled with random float values: &quot;, ran_arr) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nRandom value :  0.3733413809567606\nArray filled with random float values:  &#x5B;&#x5B;0.45421908 0.34993556 0.79641287 0.56985183]\n                                        &#x5B;0.88683577 0.91995939 0.16168328 0.35923753]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"eh-2\"><\/span><span id=\"eh-2\"><\/span>2. The random_integers() function<\/h2>\n\n\n\n<p>With <strong>random_integers() function<\/strong>, we can generate random values or even a multi-dimensional array of random value of type integer. That it, it generates random values of type Integer. Further, it gives us the liberty to choose the range of integer values from which the random numbers would be selected.<\/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=\"\">\nrandom_integers(low, high, size)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>low<\/strong>: The lowest scale\/limit for the random values to be chosen. The random values would not have a value below the low value mentioned.<\/li><li><strong>high<\/strong>: The highest scale\/limit for the random values to be chosen. The random values would not have a value beyond the high value mentioned.<\/li><li><strong>size<\/strong>: The number of rows and columns for the array to be formed.<\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>In this example, we have created a single dimensional random valued array having values between the range 5-10 only. Further, we have set up a multi-dimensional array using the same concept.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nran_val = np.random.random_integers(low = 5, high =10 , size = 3)\nprint (&quot;Random value : &quot;, ran_val)\n\nran_arr = np.random.random_integers(low = 5, high =10 , size = (2,4))\nprint (&quot;Array filled with random float values: &quot;, ran_arr) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nRandom value :  &#x5B;10  5  9]\nArray filled with random float values:  &#x5B;&#x5B; 8  8  9  6]\n                                        &#x5B; 6 10  8 10]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. The randint() function<\/h2>\n\n\n\n<p>The <strong><a href=\"https:\/\/www.askpython.com\/python-modules\/python-randint-method\" class=\"rank-math-link\">randint() function<\/a><\/strong> works in a similar fashion as that of random_integers() function. It creates an array having random values within the specified range of integers.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nran_val = np.random.randint(low = 5, high =10 , size = 3)\nprint (&quot;Random value : &quot;, ran_val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nRandom value :  &#x5B;5 8 9]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. The ranf() function<\/h2>\n\n\n\n<p>Again, <strong>ranf() function<\/strong> resembles random_sample() method in terms of functioning. It generates random numbers of type float between 0.0 to 1.0 only.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nran_val = np.random.ranf()\nprint (&quot;Random value : &quot;, ran_val)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nRandom value :  0.8328458165202546\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Feel free to comment below, in case you come across any questions. For more such posts related to Python programming, Stay tuned with us! Till then, Happy Learning! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, readers! In this article, we will be focusing on 4 Easy Ways to Perform Random Sampling in Python NumPy. So, let us get started! \ud83d\ude42 Random Sampling, to give an overview, is actually selecting random values from the defined type of data and present them to be in further use. In the course of [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":14413,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93,1],"tags":[],"class_list":["post-14314","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=14314"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14314\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/14413"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=14314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=14314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=14314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}