NumPy is a popular library in Python that provides powerful tools for numerical computing and data analysis. One of the most useful features of NumPy is its ability to generate random samples from various distributions.
In this article, we will discuss the different methods of random sampling in NumPy, including random_sample()
, random_integers()
, randint()
, and ranf()
.
Random Sampling in Python NumPy
Random sampling is the process of selecting a small subset of data values from a larger set of data values in a way that is representative of the larger set. In Python NumPy, there are various functions that can help us generate random samples from different distributions.
NumPy random_sample()
method for Random Sampling
The random_sample()
method is used to generate random samples uniformly distributed between 0 and 1. The syntax for this method is as follows:
numpy.random.random_sample(size=None)
The size
parameter allows us to specify the number of samples we want to generate.
If size
is not specified, a single random sample will be returned.
Here is an example of generating a single random sample using random_sample()
method:
import numpy as np
x = np.random.random_sample()
print(x)
Output:
0.136478932733
We can also generate multi-dimensional arrays using this method, as shown in the following example:
import numpy as np
x = np.random.random_sample((2, 3)) # Generate a 2x3 array of random samples
print(x)
Output:
[[ 0.4230482, 0.08387665, 0.69842269], [ 0.60760243, 0.02697313, 0.33864362]]
The random_integers()
function
The random_integers()
function is used to generate random integers between a low and high value (inclusive) with a specified size. The syntax for this method is as follows:
numpy.random.random_integers(low, high=None, size=None)
Here is an example of generating a random integer between 1 and 10:
import numpy as np
x = np.random.random_integers(1, 10)
print(x)
Output:
4
We can also generate multi-dimensional arrays of random integers using this method:
import numpy as np
x = np.random.random_integers(1, 10, size=(2, 3)) # Generate a 2x3 array of random integers between 1 and 10.
print(x)
Output:
[[5, 9, 8], [2, 1, 3]]
The randint()
function
The randint()
function is similar to random_integers()
but instead uses a half-open interval as the range from which to select random integers. The syntax for this method is as follows:
numpy.random.randint(low, high=None, size=None, dtype='l')
Here is an example of generating a random integer between 1 and 10:
import numpy as np
x = np.random.randint(1, 10)
print(x)
Output:
5
We can also generate multi-dimensional arrays of random integers using this method:
import numpy as np
x = np.random.randint(1, 10, size=(2, 3)) # Generate a 2x3 array of random integers between 1 and 10.
print(x)
Output:
[[3, 6, 8], [5, 1, 4]]
The ranf()
function
The ranf()
function generates random numbers from a uniform distribution between 0 and 1 with a specified size. The syntax for this method is as follows:
numpy.random.ranf(size)
Here is an example of generating a single random number using ranf()
method:
import numpy as np
x = np.random.ranf()
print(x)
Output:
0.23754214316
We can also generate multi-dimensional arrays of random numbers using this method:
import numpy as np
x = np.random.ranf((2, 3)) # Generate a 2x3 array of random numbers between 0 and 1.
print(x)
Output:
[[ 0.84342068, 0.30863827, 0.03178351], [ 0.87925162, 0.46187056, 0.36776654]]
Conclusion
In conclusion, NumPy provides us with powerful tools for generating random samples from different distributions. Using these tools, we can select random subsets of data in a way that is representative of the larger data set.
By understanding the different functions available to us in NumPy, we can make informed decisions about which method to use for our particular application.