Hey, readers! In this article, we will be focusing on 4 Easy Ways to perform NumPy Random Sampling, in detail.
So, let us begin!! 🙂
NumPy Random Sampling – Quick Overview
Often while working with various algorithms, we come across a situation that needs random samples as input to test the use case for processing of the various algorithms.
In NumPy module, it offers us with various functions to generate random numbers at a scale.
In the context of this topic, we will be covering the below mostly used NumPy Random Sampling functions–
- The random_sample() method
- The ranf() method
- The random_integers() method
- The randint() method
Let us begin! 🙂
1. Sampling with NumPy random_integers() method
NumPy random_integers() function enables us to have integer type of random values at a large scale. That is, it enables us to choose and set a boundary within which the random numbers needs to be generated. Further, apart from having 1 Dimensional random numbers, it also offers us to have multi-dimensional array of random numbers.
random_integers(low, high, size)
In the below example, we have generated 10 random Integer values between 2-4. Also, we have generated a multi-dimensional array of random elements between 2-6.
import numpy as np ran_val = np.random.random_integers(low = 2, high =4, size = 10) print ("1-D random values between 2-4 : ", ran_val) ran_arr = np.random.random_integers(low = 2, high =6 , size = (2,4)) print ("Multi-dimensional Random values: ", ran_arr)
1-D random values between 2-4 : [2 2 3 2 3 2 4 3 4 3] Multi-dimensional Random values: [[2 2 6 2] [5 3 6 3]]
2. NumPy randint() method
Apart from random_integers() method, we can also use randint() method to generate random Integer values between a boundary of element ranges.
import numpy as np ran_val = np.random.randint(low = 2, high =4 , size = 10) print ("Random value : ", ran_val)
Random value : [3 3 2 2 3 3 3 3 2 3]
3. NumPy ranf() method
Apart from random Integer values, NumPy provides us with rand() method to generate random values of type float. Yes, with ranf() function, we can generate random float type elements but it does not allow us to assign a limit or boundary to it. The values generated usually lies between 0.0 to 1.0 only.
import numpy as np ran_val = np.random.ranf() print ("Random value : ", ran_val)
As seen below, by default, ranf() generates a random value between 0.0 to 1.0
4. NumPy random_sample() method
In sync with random_integers() function, random_sample() method enables us to have a range of random float values as a single dimensional piece or even a multidimensional array.
But, the random values fall between 0.0 to 1.0 only. We cannot have a customized range/boundary set for the same.
As seen below, we have first generated random value which is scalar i.e. a single random float value which gets assigned between 0.0 to 1.0.
Further, we have triggered the creation of a multi-dimensional (3×4) matrix of random float numbers. This too gets randomly assigned between the pool of values from 0.0 to 1.0 only.
import numpy as np ran_val = np.random.random_sample() print ("Scalar Random value : ", ran_val) ran_arr = np.random.random_sample(size =(3, 4)) print ("multidimensional random float values: ", ran_arr)
Scalar Random value : 0.6498985305191337 multidimensional random float values: [[0.61522696 0.72018429 0.18743109 0.52126969] [0.79797983 0.17670717 0.86525955 0.06075286] [0.77015018 0.61547265 0.21452044 0.42709117]]
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Python programming, Stay tuned with us.
Till then, Happy learning!! 🙂