Built with Tutify.io

    AIStudyLabs

    How to Use NumPy Random choice() in Python?

    How to Use NumPy Random choice() in Python?

    NumPy random.choice() function in Python is used to return a random sample from a given 1-D array. It creates an array and fills it with random samples. It has four parameters and using these parameters we can manipulate the random samples of an array.

    In this article, I will explain how to use the NumPy random.choice() function and using its syntax, parameters, and how to generate random samples of a given 1-D array with examples.

    <a id="h-1-quick-examples-of-random-choice-function"></a>

    1. Quick Examples of random.choice() Function

    Following are quick examples of random.choice()

    
    # Quick examples of random.choice() function
    
    # Example 1: Get the single element from random choice
    arr = np.random.choice(7)
    
    # Example 2:  Get an array of uniform random samples 
    arr = np.random.choice(5, 5) 
    
    # Example 3: Get an array of non uniform random samples  
    arr = np.random.choice(5, 5, p=[0.1, 0, 0.3, 0.6, 0])
    
    # Example 4: Get the random values without replace
    arr = np.random.choice(5, 5, replace = False)
    
    # Example 5: Get the Non-random values without replace
    arr = np.random.choice(5, 3, replace = False, p=[0.1, 0, 0.3, 0.6, 0])
    

    <a id="h-2-syntax-of-random-choice"></a>

    2. Syntax of random.choice()

    Following is the syntax of the NumPy random.choice() function.

    
    # Syntax of random.choice
    random.choice(arr, size=None, replace=True, p=None)
    

    <a id="h-2-1-parameters-of-random-choice"></a>

    2.1 Parameters of random.choice()

    Following are the parameters of random.choice() function.

    <a id="h-2-2-return-value"></a>

    2.2 Return value

    It returns an ndarray of random samples.

    <a id="h-3-usage-of-numpy-random-choice"></a>

    3. Usage of NumPy random.choice()

    The NumPy random.choice() function is a built-in function in the NumPy module package and is used to create a one-dimensional NumPy array of random samples. We know that the NumPy module is a data manipulation library for Python. Some special tools of NumPy operate on arrays of numbers. For example, the manipulation of numeric data is a big task in data analysis and statistics for getting random data samples.

    In the below example, np.random.choice(7) to generate a single random element from the numbers 0 to 6 (inclusive). In this case, np.random.choice(7) will randomly select a single integer from the range [0, 1, 2, 3, 4, 5, 6]. The selected random number will be stored in the variable arr, and it will be printed using the print() statement.

    
    # Import numpy 
    import numpy as np
    
    # Get the single element from random choice
    arr = np.random.choice(7)
    print("After getting the random choice:", arr)
    

    Yields below output.

    Note that the output will be different each time you run the code because it's a random choice.

    <a id="h-4-get-uniform-random-samples-of-numpy-array"></a>

    4. Get Uniform Random Samples of NumPy Array

    You can use np.random.choice(5, 5) to generate an array of 5 uniform random samples from the integers 0 to 4 (inclusive). In this case, np.random.choice(5, 5) will generate an array of 5 elements, each randomly chosen from the integers [0, 1, 2, 3, 4]. The resulting array arr will contain 5 random integers.

    
    # Get an array of uniform random samples 
    arr = np.random.choice(5, 5)
    print("After getting an array of uniform random samples:\n",arr)
    

    Yields below output.

    <a id="h-5-get-non-uniform-random-samples-of-numpy-array"></a>

    5. Get Non-Uniform random samples of NumPy Array

    You can also use np.random.choice(5, 5, p=[0.1, 0, 0.3, 0.6, 0]) to generate an array of 5 non-uniform random samples from the integers 0 to 4 (inclusive) with specified probabilities.

    In this program, the p parameter specifies the probabilities associated with each element in the input array. The probabilities [0.1, 0, 0.3, 0.6, 0] indicate the likelihood of each element being chosen. In this example, the second and last elements have a probability of 0, so they will never be selected. The third element has a probability of 0.3, and the fourth element has a probability of 0.6, making them more likely to be chosen.

    
    # Get an array of non uniform random samples  
    arr = np.random.choice(5, 5, p=[0.1, 0, 0.3, 0.6, 0])
    print("After getting an array of non uniform random samples:\n",arr)
    
    # Output:
    # After getting an array of non uniform random samples:
    #  [2 3 2 2 2]
    

    <a id="h-6-get-the-uniform-random-sample-without-replacement"></a>

    6. Get the Uniform Random Sample without Replacement

    Create a uniform random sample from arange(5) of size 5 without replacement. which means the selected elements may be repeated, as you can see in the above output few elements are repeated in the randomly selected array. Whereas if replace=False then the elements will not repeat in the randomly selected array.

    
    # Get the random values without replace
    arr = np.random.choice(5, 5, replace = False)
    print("After getting random values without replace:\n",arr)
    
    # Output:
    # After getting random values without replace:
    #  [4 2 3 0 1]
    

    <a id="h-7-get-the-non-uniform-random-sample-without-replacement"></a>

    7. Get the Non-Uniform Random Sample without Replacement

    Create a non-uniform random sample from arange(5) of size 3 without replacement. For that, pass p parameter of the same size as the given array and set replace=False into this function, it will return Non-repeated and Non-uniform random samples of the given array.

    
    # Get the Non-random values without replace
    arr = np.random.choice(5, 3, replace = False, p=[0.1, 0, 0.3, 0.6, 0])
    print("After getting non random values without replace:\n",arr)
    
    # Output:
    # After getting non random values without replace:
    #  [3 2 0]
    

    <a id="h-8-get-the-graphical-presentation-of-random-values"></a>

    8. Get the Graphical presentation of Random Values

    Let's plot the graph of the random values using the matplotlib library.

    
    import matplotlib.pyplot as plt
    # Using choice() method
    arr = np.random.choice(7, 300)
    count, bins, ignored = plt.hist(arr, 25, density=True)
    plt.show()
    

    <a id="h-frequently-asked-questions"></a>

    Frequently Asked Questions

    numpy.random.choice() is used to generate random samples from a specified 1-D array-like object. It can be used to randomly select elements from an array, generate random integers, or perform random sampling with or without replacement.

    You can generate a random integer between a specific range using numpy.random.choice() by providing an array-like object containing the range of integers. For example, np.random.choice(7) will generate a random integer between 0 and 6.

    To generate random samples without replacement, set the replace parameter to False. For example, np.random.choice(5, 3, replace=False) will generate 3 random samples without replacement from the integers 0 to 4.

    numpy.random.choice() can be used with non-integer data types. It works with any array-like object, including arrays of floats, strings, or other data types.

    You can generate non-uniform random samples by providing the p parameter, which specifies the probabilities associated with each element in the input array. The function will sample elements based on these probabilities.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained the NumPy random.choice() function syntax, parameter, and usage of how to get the random samples of 1-D NumPy array with examples.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References