{"id":89251,"date":"2024-07-29T18:00:28","date_gmt":"2024-07-29T12:30:28","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89251"},"modified":"2024-07-29T18:22:30","modified_gmt":"2024-07-29T12:52:30","slug":"random-sampling-in-numpy","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/","title":{"rendered":"Random Sampling in NumPy"},"content":{"rendered":"<p>Random sampling is a fundamental idea in statistics and data analysis. It enables us to choose a subset of data points from a larger dataset without any particular pattern or bias for numerous applications, including statistical inference, hypothesis testing, and machine learning.<\/p>\n<p>This article will explain random sampling, why it&#8217;s crucial, and how to implement it using NumPy, a well-known Python package for numerical operations.<\/p>\n<h2>What is Random Sampling?<\/h2>\n<p>The act of choosing a random subset of data points from a particular dataset is known as random sampling. The fundamental premise is that every data point in the dataset has an equal chance of being chosen and that neither a systematic nor an unconscious bias affects the choice. Because it allows us to draw conclusions about a wider population from a representative sample, random sampling is crucial.<\/p>\n<h3>Why Use Random Sampling?<\/h3>\n<p><strong>Random sampling offers several advantages:<\/strong><\/p>\n<ul>\n<li><strong>Statistical validity:<\/strong> Random sampling increases the probability of obtaining a representative sample, which enhances the validity of any statistical analysis or inference made from the sample.<\/li>\n<li><strong>Reduced bias:<\/strong> Random sampling minimizes the risk of introducing bias into the analysis. Non-random sampling methods, such as convenience sampling, can lead to skewed results.<\/li>\n<li><strong>Generalizability:<\/strong> Random samples are more likely to generalize well to the entire population, which is crucial in fields such as polling, market research, and quality control.<\/li>\n<\/ul>\n<h3>Implementing Random Sampling with NumPy<\/h3>\n<p>NumPy is a powerful Python library for numerical computations. It provides convenient functions for generating random numbers and performing random sampling. Let&#8217;s explore how to implement random sampling in 1D, 2D, and multidimensional arrays using NumPy.<\/p>\n<h4>Random Sampling in 1D Arrays<\/h4>\n<p>To perform random sampling in a 1D NumPy array, you can use the numpy.random.choice function. Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Create a 1D array\r\ndata = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\r\n\r\n# Perform random sampling\r\nsample = np.random.choice(data, size=5, replace=False)\r\n\r\nprint(\"Randomly sampled 1D array:\", sample)<\/pre>\n<p>In this example, np.random.choice selects 5 random elements from the data array without replacement, ensuring that each element is selected only once.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>Randomly sampled 1D array: [ 6 9 1 4 10]<\/p>\n<h4>Random Sampling in 2D Arrays<\/h4>\n<p>Random sampling in 2D arrays is quite similar. You can use the same np.random.choice function, but specify the axis along which you want to sample. Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Create a 2D array\r\ndata = np.array([[1, 2, 3],\r\n                 [4, 5, 6],\r\n                 [7, 8, 9]])\r\n\r\n# Perform random sampling along rows\r\nsampled_rows = np.random.choice(data.shape[0], size=2, replace=False)\r\nsampled_data = data[sampled_rows, :]\r\n\r\nprint(\"Randomly sampled 2D array (rows):\")\r\nprint(sampled_data)<\/pre>\n<p>In this example, we first select two random row indices and then extract those rows from the original 2D array.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>Randomly sampled 2D array (rows):<br \/>\n[[7 8 9]<br \/>\n[4 5 6]]<\/p>\n<h4>Random Sampling in Multidimensional Arrays<\/h4>\n<p>For multidimensional arrays, the concept remains the same. You can specify the axis along which you want to perform random sampling. Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Create a 3D array\r\ndata = np.array([[[1, 2], [3, 4]],\r\n                 [[5, 6], [7, 8]],\r\n                 [[9, 10], [11, 12]]])\r\n\r\n# Perform random sampling along the first dimension\r\nsampled_data = np.random.choice(data.shape[0], size=2, replace=False)\r\nsampled_data = data[sampled_data, :, :]\r\n\r\nprint(\"Randomly sampled 3D array (along the first dimension):\")\r\nprint(sampled_data)<\/pre>\n<p>In this example, we randomly select two 2D arrays along the first dimension of the 3D array.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>Randomly sampled 3D array (along the first dimension):<br \/>\n[[[ 5 6]<br \/>\n[ 7 8]]<\/p>\n<p>[[ 1 2]<br \/>\n[ 3 4]]]<\/p>\n<h3>NumPy Random Sampling Methods-<\/h3>\n<p>NumPy provides various methods for generating random numbers and performing random sampling. In this section, we&#8217;ll elaborate on four methods for random sampling:<\/p>\n<ul>\n<li>numpy.random_sample() method<\/li>\n<li>numpy.ranf() method<\/li>\n<li>numpy.random_integers() method<\/li>\n<li>numpy.randint() method<\/li>\n<\/ul>\n<h4>1. numpy.random_sample()<\/h4>\n<p>The numpy.random_sample() method generates random floating-point numbers in the half-open interval [0.0, 1.0). It&#8217;s a convenient way to create random samples from a uniform distribution.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Generate 5 random numbers between 0.0 (inclusive) and 1.0 (exclusive)\r\nrandom_samples = np.random.random_sample(5)\r\nprint(\"Random samples:\", random_samples)<\/pre>\n<p><strong>Expected Output (Note: Your output will vary due to randomness):<\/strong><br \/>\n<strong>Random samples:<\/strong> [0.12345678 0.23456789 0.3456789 0.45678901 0.56789012]<\/p>\n<h4>2. numpy.ranf()<\/h4>\n<p>The numpy.ranf() method is an alias for numpy.random_sample(). It also generates random floating-point numbers in the half-open interval [0.0, 1.0).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Generate 5 random numbers using ranf()\r\nrandom_samples = np.random.ranf(5)\r\nprint(\"Random samples:\", random_samples)<\/pre>\n<p><strong>Expected Output (Note: Your output will vary due to randomness):<\/strong><br \/>\n<strong>Random samples:<\/strong> [0.12345678 0.23456789 0.3456789 0.45678901 0.56789012]<\/p>\n<h4>3. numpy.random_integers()<\/h4>\n<p>The numpy.random_integers() method generates random integers within a specified range. It includes both the lower and upper bounds of the range.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Generate 5 random integers between 1 and 10 (inclusive)\r\nrandom_integers = np.random.random_integers(1, 10, 5)\r\nprint(\"Random integers:\", random_integers)<\/pre>\n<p><strong>Expected Output (Note: Your output will vary due to randomness):<\/strong><br \/>\n<strong>Random integers:<\/strong> [ 5 7 10 1 4]<\/p>\n<h4>4. numpy.randint()<\/h4>\n<p>The numpy.randint() method is similar to numpy.random_integers() but provides a more flexible way to specify the range of random integers. It generates random integers within the half-open interval [low, high).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Generate 5 random integers between 1 (inclusive) and 10 (exclusive)\r\nrandom_integers = np.random.randint(1, 10, 5)\r\nprint(\"Random integers:\", random_integers)<\/pre>\n<p><strong>Expected Output (Note: Your output will vary due to randomness):<\/strong><br \/>\n<strong>Random integers:<\/strong> [7 6 3 1 9]<\/p>\n<h3>Conclusion<\/h3>\n<p>Drawing representative subsets from datasets requires the use of the random sampling approach. Implementing random sampling in many dimensions is simple by using NumPy&#8217;s robust random number-generating capabilities. NumPy offers the tools you need to ensure your samples are unbiased and statistically valid, whether you&#8217;re working with 1D, 2D, or multidimensional arrays. Making more precise inferences and utilising random sampling properly will help your data analysis be of higher calibre. Coding is fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Random sampling is a fundamental idea in statistics and data analysis. It enables us to choose a subset of data points from a larger dataset without any particular pattern or bias for numerous applications,&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447394,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[385],"tags":[383,427,384,5661,5660],"class_list":["post-89251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy-tutorials","tag-learn-numpy","tag-numpy-random-sampling","tag-numpy-tutorial","tag-random-sampling","tag-random-sampling-in-numpy"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Random Sampling in NumPy - TechVidvan<\/title>\n<meta name=\"description\" content=\"The act of choosing a random subset of data points from a particular dataset is known as random sampling in NumPy.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Random Sampling in NumPy - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"The act of choosing a random subset of data points from a particular dataset is known as random sampling in NumPy.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-29T12:30:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-29T12:52:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/randomsampling-in-numpy.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Random Sampling in NumPy - TechVidvan","description":"The act of choosing a random subset of data points from a particular dataset is known as random sampling in NumPy.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/","og_locale":"en_US","og_type":"article","og_title":"Random Sampling in NumPy - TechVidvan","og_description":"The act of choosing a random subset of data points from a particular dataset is known as random sampling in NumPy.","og_url":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-07-29T12:30:28+00:00","article_modified_time":"2024-07-29T12:52:30+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/randomsampling-in-numpy.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Random Sampling in NumPy","datePublished":"2024-07-29T12:30:28+00:00","dateModified":"2024-07-29T12:52:30+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/"},"wordCount":721,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/randomsampling-in-numpy.webp","keywords":["learn numpy","numPy random sampling","numPy tutorial","random sampling","random sampling in numpy"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/","url":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/","name":"Random Sampling in NumPy - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/randomsampling-in-numpy.webp","datePublished":"2024-07-29T12:30:28+00:00","dateModified":"2024-07-29T12:52:30+00:00","description":"The act of choosing a random subset of data points from a particular dataset is known as random sampling in NumPy.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/randomsampling-in-numpy.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/randomsampling-in-numpy.webp","width":1200,"height":628,"caption":"randomsampling in numpy"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/random-sampling-in-numpy\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Random Sampling in NumPy"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89251"}],"version-history":[{"count":4,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89251\/revisions"}],"predecessor-version":[{"id":447542,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89251\/revisions\/447542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447394"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}