# Numpy – Select Random Elements From Array

In this tutorial, we will look at how to select one or more random elements from a Numpy array with the help of some examples.

## How to select random elements from a Numpy array?

You can use the `numpy.random.choice()` function to select random elements from a given 1-D Numpy array. The following is the syntax –

```# randomly select value(s) from numpy array
numpy.random.choice(a, size=None, replace=True, p=None)```

It returns the selected random value or array of values (if selecting more than one random value).

The `numpy.random.choice()` function takes the following parameters –

• `a` – The 1-D array to sample from. You can also pass an integer in which case the sampling will be done from the result of `numpy.arange(a)`
• `size`Optional argument. The number of samples to draw. You can also pass a tuple `(m, n, k)` as the output shape, in which case `m*n*k` samples are drawn. Its default value is `None`, in which case a single value is sampled.
• replace – Optional argument. Whether to sample with replacement. Its default value is `True` meaning samples are drawn with replacement (the same value can be sampled multiple times) by default.
• `p`Optional argument. The array of probabilities associated with each value in `a`. If not specified, then sampling is done by assuming a uniform distribution over each value in `a` (they have the same probability of being sampled).

## Examples

Let’s now look at some examples of using the above syntax to sample random elements from a Numpy array.

First, we will create a Numpy array that we will be using throughout this tutorial.

```import numpy as np

# create a numpy array
ar = np.array([1, 2, 3, 4, 5])
# display the array
ar```

Output:

`array([1, 2, 3, 4, 5])`

Here, we used the `numpy.array()` function to create a 1-D array of some integers.

### Example 1 – Select one random element from a Numpy array

If you only want to get a random value from a 1-D Numpy array, pass the array as an argument to the `numpy.random.choice()` function. We don’t need to specify the `size` argument because the function by default samples a single value.

Let’s sample a single value from the array created above.

```# select a random value from ar
np.random.choice(ar)```

Output:

`4`

We get a random value from the array `ar`.

### Example 2 – Select multiple random values from a Numpy array

You can also sample multiple random values using the `numpy.random.choice()` function. Use the `size` parameter to specify the number of values you want to randomly sample from the array.

Let’s randomly sample 4 values from the above array.

```# select 4 random values from ar
np.random.choice(ar, size=4)```

Output:

`array([4, 2, 1, 2])`

We get a 1-D Numpy array with the randomly sampled elements.

You can see that the value 2 occurs twice in the resulting array. This is because the `numpy.random.choice()` function samples values with replacement by default. This means that a value can be sampled multiple times.

### Example 3 – Select multiple random values (without replacement) from a Numpy array

If you want to sample without replacement (that is, a value cannot be chosen again if it’s already been sampled), pass `replace=False` to the function.

Let’s now randomly sample 4 values without replacement from the array `ar`.

```# select 4 random values from ar without replacement
np.random.choice(ar, size=4, replace=False)```

Output:

`array([1, 2, 4, 3])`

We get a 1-D array of the resulting random values (which have been sampled without replacement).

### Example 4 – Select random values with a custom probability distribution

In the above examples, each value in the array `ar` had an equal probability of being sampled. You can pass a custom probability distribution (a 1-D array with associated probabilities for each value in `ar`) to the `p` parameter.

Currently, each value in the array `ar` has a 20% (0.2) probability of being selected. Let’s double the probability of the value 3, and proportionately reduce the probabilities of the other values. The probability distribution now looks like `[0.15, 0.15, 0.40, 0.15, 0.15]`.

Let’s now use this probability distribution to randomly select 4 values (with replacement) from the array `ar`.

```# select 4 random values from ar with custom probability distribution
np.random.choice(ar, size=4, p=[0.15, 0.15, 0.4, 0.15, 0.15])```

Output:

`array([2, 3, 3, 3])`

You can see that the resulting array has multiple occurrences of 3. This is because the probability of selecting a 3 is much higher (40%) compared to the other values (15% each) in the array.

## Summary

In this tutorial, we looked at how to randomly select values from a Numpy array. The following are the key takeaways from this tutorial.

• Use the `numpy.random.choice()` function to randomly select values from a Numpy array.
• Use the `size` parameter to specify the number of values to sample.
• The `numpy.random.choice()` function samples the values with replacement by default. To sample without replacement, pass `replace=False`.
• By default, each value in the array has an equal probability of being sampled (uniform distribution). To specify a custom probability distribution for the values, use the `p` parameter.

You might also be interested in –

• 