Adventures in Machine Learning

Randomness Made Easy: A Guide to NumPy’s Sampling Functions

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.136

478

932733

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 2×3 array of random samples

print(x)

Output:

[[ 0. 4230

4

482, 0.0838766

5, 0.6

98

4226

9],

[ 0.607602

43, 0.026

97313, 0.3386

4362]]

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 2×3 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 2×3 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.237

5

421

4316

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 2×3 array of random numbers between 0 and 1.

print(x)

Output:

[[ 0.8

43

42068 0.30863827 0.031783

51],

[ 0.87

92

5162 0. 461870

56 0.367766

5

4]]

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. 3)

The random_integers() function

The random_integers() function is used in NumPy to generate random integers within a specific range. This function takes in three parameters: low, high, and size.

Low and high refer to the interval where the integers will be drawn from. Size, on the other hand, specifies the size of the output array, and follows the same rules as the previous functions – if no size is specified, a single integer is produced.

Syntax of random_integers() function:

The syntax for the random_integers() function is as follows:

numpy.random.random_integers(low, high=None, size=None)

Where:

– low: Represents the lowest value of the interval range, inclusive. – high: Represents the highest value of the interval range, exclusive.

If unspecified, it defaults to be None, and low is interpreted as the highest value of the range. – size: Represents the size of the output array.

If unspecified, it either gives a single integer or a rectangular array based on the dimensions given in the low input.

Examples of using random_integers() function:

Example 1: Generating a single random integer between 1 and 100, inclusive.

We can use the random_integers() function to generate a single integer in a specific range, as follows:

import numpy as np

x = np.random.random_integers(1, 101) # Both 1 and 101 are included in the range.

print(x)

Output:

52

Example 2: Generating a two-dimensional array of random integers between 1 and

50. We can also utilize the random_integers() function for generating multi-dimensional arrays with integers, as follows:

import numpy as np

x = np.random.random_integers(1,

51, size=(3, 3)) # 1 to

50 inclusive. A 3×3 array is created.

print(x)

Output:

[[1

9 22 32],

[

4

9 8 21],

[36 3

4

43]]

4)

The randint() function

The randint() function is another variant of generating random integers in NumPy. It is distinct from the random_integers() function because it allows the programmer to specify intervals that might not include 1, or have high values included in the sample. randint() function builds upon the same logic as seen earlier, but the parameters have a slightly different interpretation.

Syntax of randint() function:

The syntax for the randint() function is as follows:

numpy.random.randint(low, high=None, size=None, dtype=int)

Where:

– low: Represents the lowest value of the interval range, inclusive. – high: Represents the highest value of the interval range, exclusive – if unspecified, it defaults to be None, and low is interpreted as the highest value of the range.

– size: Represents the size of the output array. If unspecified, it either gives a single integer or a rectangular array based on the dimension given in the low input.

– dtype: Specifies the data type precision of the output values. Examples of using randint() function:

Example 1: Generating a single random integer between

5 and 10.

import numpy as np

x = np.random.randint(

5, 11) #

5 is inclusive, 11 is exclusive.

print(x)

Output:

9

Example 2: Generating a two-dimensional array of random integers between 20 and 30.

import numpy as np

x = np.random.randint(20, 31, size=(

4, 2)) # 20 inclusive, 31 exclusive. A

4×2 array is created.

print(x)

Output:

[[2

4 2

9],

[2

5 2

4],

[30 20],

[2

5 20]]

Conclusion

In this article, we have explored the different ways we can generate random numbers and samples in NumPy using random_sample(), random_integers(), randint(), and ranf() functions. NumPy’s applications range from simulation and statistical analysis to machine learning.

NumPy provides an interface for users to generate random numbers with different distribution properties through different functions, allowing flexibility in data processing. By understanding how to use these functions, we can effectively sample data in a way that is representative of the larger data set and generate random variations for use in statistical models.

5)

The ranf() function

The ranf() function is one of the many functions available in NumPy for generating random numbers. Unlike the random_integers() and randint() functions, the ranf() function is used to generate random floating-point numbers within a specified range.

It is useful in situations where we need to generate a random floating-point number within a given range. In this section, we will explore the ranf() function in detail and show some practical examples of using it.

Examples of using ranf() function:

To generate random floating-point numbers using the NumPy ranf() function, you need to specify the size of the array or the number of random values you want to generate. Here are some examples of using the ranf() function.

Example 1: Generating a single random floating-point number between 0 and 1. The following code shows how to generate a single random floating-point number between 0 and 1 using the ranf() function:

import numpy as np

x = np.random.ranf()

print(x)

Output:

0.7

9812

4

56

In this example, we call the ranf() function without any arguments to generate a single random number between 0 and 1. Example 2: Generating a two-dimensional array of random floating-point numbers between 0 and 1.

You can also generate multi-dimensional arrays of random floating-point numbers using the ranf() function. The following code shows how to generate a 3×3 array of random floating-point numbers between 0 and 1:

import numpy as np

x = np.random.ranf(size=(3,3))

print(x)

Output:

[[0.1070871

5 0.03

42

4788 0. 40367786]

[0.

4013103

4 0.22073

4

4 0.6

5

9

4338

9]

[0.86028

4

4 0.2

4

406

9

5

9 0. 9160

563 ]]

In this example, we call the ranf() function with an argument of size=(3,3) to generate a 3×3 array of random floating-point numbers between 0 and 1.

Example 3: Generating a two-dimensional array of random floating-point numbers between a specified range. You can also generate floating-point numbers within a specified range using the ranf() function.

The following code shows how to generate a 3×3 array of random floating-point numbers between 1 and 10:

import numpy as np

x = np.random.uniform(low=1, high=10, size=(3,3))

print(x)

Output:

[[

5.13367

576

5. 4618

4

983

9.0

511682 ]

[1.

4

5

9786

9

4 3.82

9

4062

4

9.612

9816 ]

[6. 9

578062

9 7.6

5

40672

4 7.18623

5

56]]

In this example, we use the np.random.uniform() function to generate random numbers within the specified range of 1 to 10.

We pass in the low and high parameters to specify the range and the size parameter to specify the shape of the array.

Conclusion

In conclusion, the ranf() function is a useful tool for generating random floating-point numbers within a given range in NumPy. You can use it to generate single random numbers or multi-dimensional arrays of random floating-point numbers. By understanding how to use the ranf() function, you can generate random numbers that are representative of the larger data set, and thereby simulate random shocks or deviations in statistical models or simulations.

In this article, we explored NumPy’s random sampling functions, including random_sample(), random_integers(), randint(), and ranf(). We discussed the syntax of each function and provided examples of how to use them to generate random numbers and samples within a specified range.

By using these functions, we can effectively sample data and generate random variations for use in statistical models or simulations. Understanding how to use these functions in NumPy can aid in the accurate analysis of data and simulation of possible outcomes in studies that rely on randomization.

The takeaway is that NumPy provides users with powerful tools for generating random samples in a way that is representative of the larger data set, and the importance of familiarity with these sampling functions cannot be overstated.

Popular Posts