• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Numpy Tips / Numpy’s random choice() function

Numpy’s random choice() function

December 31, 2022 by cmdlinetips

In this post we will learn how to use Numpy’s choice function. Numpy’s choice() function is a useful tool for selecting random items from a list or array.

The basic syntax of choice() function Numpy’s random module is this

choice(a, size=None, replace=True, p=None)

Here, a is the list or array from which you want to select items, size is the shape of the output array, replace determines whether or not items can be selected multiple times, and p is an array of probabilities for each item in a.
As we can see choice() allows you to specify the size of the output, as well as p, the probability of selecting each item in the list.

import numpy as np

Selecting a single random element from 1-D array with Numpy choice()

Here’s an example of using choice() to select a single random item from a list. We use Numpy’s random generate object to use choice() fucntion.

a = [1, 2, 3, 4, 5]
rng = np.random.default_rng()
rng.choice(a)
# Output could be any of the items in the list

4

Selecting multiple random elements from a list/array with Numpy choice

Here is an example to select multiple items from a list or array by specifying the size argument using Numpy choice().

a = [1, 2, 3, 4, 5]
random_items = rng.choice(a, 3)
print(random_items)  # Output will be an array of 3 random items from the list

Selecting multiple random elements without replacement from a list/array with Numpy choice

By default, Numpy’s choice() selects items with replacement, will allow items to be selected multiple times. If you want to prevent this, you can set the replace parameter to False:

a = [1, 2, 3, 4, 5]

random_items =  rng.choice(a, size=3, replace=False)
print(random_items)  # Output will be an array of 3 random, unique items from the list

Selecting multiple random elements with probability using Numpy choice

Let us assign some probability for each element in our list and randomly pick multiple items using the probability.

For example, an element with high probability is more likely to be randomly picked and an element with low probability is less likely to selected randomly.

To illustrate this we have assigned 0 probability for numbers 1 and 5 in our list, low probabilities for numbers 2 and 4. When we use choice() function randomly select multiple elements, we would not see 1 and 5 as they have zero probabilities. And we will see 3 more often as it has high probability. Similarly we will not 2 and 4 often as they have low probability.

Here is an example of using choice() 3 times with the probability.

a = np.array([1, 2, 3, 4, 5])
rng = np.random.default_rng()
probs = np.array([0.0, 0.2, 0.6, 0.2, 0.0])
rng.choice(a, size=3, p=probs)

array([3, 3, 3])
rng.choice(a, size=3, p=probs)

array([2, 3, 3])
rng.choice(a, size=3, p=probs)

array([2, 3, 2])

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to generate random numbers from binomial distribution in Numpy Default ThumbnailHow to generate random numbers from Poisson distribution with Numpy Default ThumbnailGenerate Random Integers using Generators in Numpy Shapes of Beta distribution and their parametersHow to generate random numbers from Beta distribution in Numpy

Filed Under: Numpy Tips Tagged With: numpy choice() function

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version