Table of Contents
- Introduction
- The
numpy.random.choice()
Function - Array Indexing for 1-D Arrays
- Array Indexing for 2-D Arrays
- Array Indexing for 3-D Arrays
Introduction
Array indexing is the same as accessing an array element. You can access an array element by referring to its index number, just like a Python list. The indices in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1, etc.
The numpy.random.choice() Function
For the purpose of illustrating array indexing, it will be useful to invoke the numpy.random.choice
function which generates a random sample from a given 1-D array. The topic on NumPy random functions
will be discussed in a later section
.
Syntax
Thenumpy.random.choice()
function.
1numpy.random.choice(a, size=None, replace=True, p=None)
Parameter | Required? | Default Value | Description |
---|---|---|---|
a |
✔️ Yes | NA | If an ndarray , a random sample is generated from its elements. If an int , the random sample is generated as if it were numpy.arange(a) . |
size |
❌ No | None |
Output shape. If the given shape is, e.g., (m, n, k) , then m*n*k samples are drawn. Default is None , in which case a single value is returned. |
replace |
❌ No | True |
True means sample is drawn with replacement. False means without replacement. |
p |
❌ No | None |
The probabilities associated with each entry in a . If not given, the sample assumes a uniform distribution over all entries in a . |
Example
Generating a random 1-D array usingnp.random.choice
function.
1import numpy as np
2R = np.random.RandomState(32) #set a random seed
3
4a1 = R.choice(10, size=10, replace=False)
5print(a1)
[9 1 2 8 3 0 4 6 5 7]
Note that we have set a seed for the pseudorandom number generator (PRNG) so that readers can reproduce the exact same results given here. You can read more about PRNG here .
The above code generated a 1-D array of 10 random numbers from the array np.arange(10)
without replacement. Therefore, the is no repetition among the numbers generated.
Example
Generating a random 2-D array usingnp.random.choice
function.
1a2 = R.choice(3*4, size=(3,4), replace=False)
2print(a2)
[[ 8 10 0 2]
[ 6 7 1 11]
[ 4 9 5 3]]
The above code generated a 3X4, 2-D array of 12 random numbers from the array np.arange(3*4)
without replacement.
Example
Generating a random 3-D array usingnp.random.choice
function.
1a3 = R.choice(3*4*5, size=(3,4,5), replace=False)
2print(a3)
[[[21 8 7 25 1]
[53 31 23 44 12]
[ 3 54 37 51 42]
[45 14 22 27 34]]
[[28 57 52 40 17]
[32 9 2 59 39]
[33 15 46 43 24]
[35 58 19 16 41]]
[[ 4 30 6 49 0]
[48 47 29 26 56]
[11 13 38 5 55]
[36 50 20 10 18]]]
The above code generated a 3X4X5, 2-D array of 60 random numbers from the array np.arange(3*4*5)
without replacement. Note that 3 frames are shown, each with a 4X5 array.
ADVERTISEMENT
Array Indexing for 1-D Arrays
Indexing in NumPy arrays is very similar to Python list indexing. In a 1-D array, we can access the $i^{\text{th}}$ value (counting from zero) by specifying the desired index in square brackets, just as with Python lists.
Example
Accessing elements in a 1-D array with positive indices.1print(a1[0])
2print(a1[3])
9
8
Positive Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|---|
Element | 9 | 1 | 2 | 8 | 3 | 0 | 4 | 6 | 5 | 7 |
EXAMPLE Accessing elements in a 1-D array with negative indices.
1print(a1[-1])
2print(a1[-5])
7
0
Negative Index | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
---|---|---|---|---|---|---|---|---|---|---|
Element | 9 | 1 | 2 | 8 | 3 | 0 | 4 | 6 | 5 | 7 |
Array Indexing for 2-D Arrays
In a 2-D NumPy array, we can access elements using a comma-separated tuple of 2 indices: (row index, column index). For example, for the a2
array, the first index (red) represents the row index (top to bottom) and second index (blue) represents the column index (left to right).
$$ \begin{matrix} \textcolor{red}{0}\\ \textcolor{red}{1}\\ \textcolor{red}{2} \end{matrix} \begin{bmatrix} 8 & 10 & 0 & 2\\ 6 & 7 & 1 & 11\\ 4 & 9 & 5 & 3 \end{bmatrix} \\ \begin{matrix} \textcolor{blue}{~~\quad 0} & \textcolor{blue}{~1} & \textcolor{blue}{~2} & \textcolor{blue}{~3} \end{matrix} $$
Let’s take a look at a few examples.
Example
Accessing elements in a 2-D array with positive indices.1print(a2)
2print(a2[2,3])
[[ 8 10 0 2]
[ 6 7 1 11]
[ 4 9 5 3]]
3
$$ \begin{matrix} \textcolor{red}{0}\\ \textcolor{red}{1}\\ \textcolor{red}{2} \end{matrix} \begin{bmatrix} 8 & 10 & 0 & 2\\ 6 & 7 & 1 & 11\\ 4 & 9 & 5 & \colorbox{lightblue}{3} \end{bmatrix} \\ \begin{matrix} \textcolor{blue}{~~\quad 0} & \textcolor{blue}{~1} & \textcolor{blue}{~2} & \textcolor{blue}{~3} \end{matrix} $$
Example
Accessing elements in a 2-D array with negative indices.1print(a2)
2print(a2[-1,-2])
[[ 8 10 0 2]
[ 6 7 1 11]
[ 4 9 5 3]]
5
$$ \begin{matrix} \textcolor{red}{-3}\\ \textcolor{red}{-2}\\ \textcolor{red}{-1} \end{matrix} \begin{bmatrix} 8 & 10 & 0 & 2\\ 6 & 7 & 1 & 11\\ 4 & 9 & \colorbox{lightblue}{5} & 3 \end{bmatrix} \\ \begin{matrix} \textcolor{blue}{~~~\quad \text{-}4} & \textcolor{blue}{~\text{-}3} & \textcolor{blue}{\text{-}2} & \textcolor{blue}{~\text{-}1} \end{matrix} $$
Example
Accessing elements in a 2-D array with mixed positive & negative indices.1print(a2)
2print(a2[-2,3])
[[ 8 10 0 2]
[ 6 7 1 11]
[ 4 9 5 3]]
11
$$ \begin{matrix} \textcolor{red}{-3}\\ \textcolor{red}{-2}\\ \textcolor{red}{-1} \end{matrix} \begin{bmatrix} 8 & 10 & 0 & 2\\ 6 & 7 & 1 & \colorbox{lightblue}{11}\\ 4 & 9 & 5 & 3 \end{bmatrix} \\ \begin{matrix} \textcolor{blue}{~\qquad 0} & \textcolor{blue}{~1} & \textcolor{blue}{~2} & \textcolor{blue}{~~3} \end{matrix} $$
ADVERTISEMENT
Array Indexing for 3-D Arrays
In a 3-D NumPy array, we can access elements using a comma-separated tuple of 3 indices: (frame index, row index, column index). For example, for the a3
array, the first index (green) indicates the frame index, the second index (red) represents the row index (top to bottom) and third index (blue) represents the column index (left to right). The ouput is printed frame by frame.
$$ \text{\textcolor{darkgreen}{Frame 0}} \quad \begin{matrix} \textcolor{red}{0}\\ \textcolor{red}{1}\\ \textcolor{red}{2}\\ \textcolor{red}{3} \end{matrix} \begin{bmatrix} 21 & 8 & 7 & 25 & 1 \\ 53 & 31 & 23 & 44 & 12 \\ 3 & 54 & 37 & 51 & 42\\ 45 & 14 & 22 & 27 & 34 \end{bmatrix} \\ \begin{matrix} \hspace{2.2cm} \textcolor{blue}{~0} & \textcolor{blue}{~1} & \textcolor{blue}{~~2} & \textcolor{blue}{~~3} & \textcolor{blue}{~4} \end{matrix} $$
$$ \text{\textcolor{darkgreen}{Frame 1}} \quad \begin{matrix} \textcolor{red}{0}\\ \textcolor{red}{1}\\ \textcolor{red}{2}\\ \textcolor{red}{3} \end{matrix} \begin{bmatrix} 28 & 57 & 52 & 40 & 17 \\ 32 & 9 & 2 & 59 & 39 \\ 33 & 15 & 46 & 43 & 24 \\ 35 & 58 & 19 & 16 & 41 \end{bmatrix} \\ \begin{matrix} \hspace{2.2cm} \textcolor{blue}{~0} & \textcolor{blue}{~1} & \textcolor{blue}{~~2} & \textcolor{blue}{~~3} & \textcolor{blue}{~4} \end{matrix} $$
$$ \text{\textcolor{darkgreen}{Frame 2}} \quad \begin{matrix} \textcolor{red}{0}\\ \textcolor{red}{1}\\ \textcolor{red}{2}\\ \textcolor{red}{3} \end{matrix} \begin{bmatrix} 4 & 30 & 6 & 49 & 0 \\ 48 & 47 & 29 & 26 & 56\\ 11 & 13 & 38 & 5 & 55 \\ 36 & 50 & 20 & 10 & 18 \end{bmatrix} \\ \begin{matrix} \hspace{2.2cm} \textcolor{blue}{~0} & \textcolor{blue}{~1} & \textcolor{blue}{~~2} & \textcolor{blue}{~~3} & \textcolor{blue}{~4} \end{matrix} $$
Example
Accessing elements in a 3-D array with positive indices.1print(a3)
2print(a3[2,3,1])
[[[21 8 7 25 1]
[53 31 23 44 12]
[ 3 54 37 51 42]
[45 14 22 27 34]]
[[28 57 52 40 17]
[32 9 2 59 39]
[33 15 46 43 24]
[35 58 19 16 41]]
[[ 4 30 6 49 0]
[48 47 29 26 56]
[11 13 38 5 55]
[36 50 20 10 18]]]
50
$$ \text{\textcolor{darkgreen}{Frame 0}} \quad \begin{matrix} \textcolor{red}{0}\\ \textcolor{red}{1}\\ \textcolor{red}{2}\\ \textcolor{red}{3} \end{matrix} \begin{bmatrix} 21 & 8 & 7 & 25 & 1 \\ 53 & 31 & 23 & 44 & 12 \\ 3 & 54 & 37 & 51 & 42\\ 45 & 14 & 22 & 27 & 34 \end{bmatrix} \\ \begin{matrix} \hspace{2.2cm} \textcolor{blue}{~0} & \textcolor{blue}{~1} & \textcolor{blue}{~~2} & \textcolor{blue}{~~3} & \textcolor{blue}{~4} \end{matrix} $$
$$ \text{\textcolor{darkgreen}{Frame 1}} \quad \begin{matrix} \textcolor{red}{0}\\ \textcolor{red}{1}\\ \textcolor{red}{2}\\ \textcolor{red}{3} \end{matrix} \begin{bmatrix} 28 & 57 & 52 & 40 & 17 \\ 32 & 9 & 2 & 59 & 39 \\ 33 & 15 & 46 & 43 & 24 \\ 35 & 58 & 19 & 16 & 41 \end{bmatrix} \\ \begin{matrix} \hspace{2.2cm} \textcolor{blue}{~0} & \textcolor{blue}{~1} & \textcolor{blue}{~~2} & \textcolor{blue}{~~3} & \textcolor{blue}{~4} \end{matrix} $$
$$ \text{\textcolor{darkgreen}{Frame 2}} \quad \begin{matrix} \textcolor{red}{0}\\ \textcolor{red}{1}\\ \textcolor{red}{2}\\ \textcolor{red}{3} \end{matrix} \begin{bmatrix} 4 & 30 & 6 & 49 & 0 \\ 48 & 47 & 29 & 26 & 56\\ 11 & 13 & 38 & 5 & 55 \\ 36 & \colorbox{lightblue}{50} & 20 & 10 & 18 \end{bmatrix} \\ \begin{matrix} \hspace{2.2cm} \textcolor{blue}{~0} & \textcolor{blue}{~~1} & \textcolor{blue}{~~~2} & \textcolor{blue}{~~3} & \textcolor{blue}{~~4} \end{matrix} $$