Indexing Multi-dimensional arrays in Python using NumPy
Indexing in multi-dimensional arrays allows us to access, modify or extract specific elements or sections from arrays efficiently. In Python, NumPy provides tools to handle this through index numbers, slicing and reshaping.
arange()
The np.arange() function is used to create a one-dimensional NumPy array containing evenly spaced values within a specified range.
Syntax
np.arange( [start, ] stop [, step,], dtype=None )
Parameters:
- start (optional): starting value of the sequence, default is 0
- stop: end value (excluded from output)
- step (optional): spacing between values, default is 1
- dtype (optional): desired data type of the output array
Example: Here we use np.arange() to create a 1D array containing a sequence of numbers from 0 to 4.
import numpy as np
arr = np.arange(5)
print(arr)
Output
[0 1 2 3 4]
Using Start, Stop and Step
The np.arange() function can generate a sequence by explicitly defining the start, stop and step values to control how the numbers are spaced in the array.
import numpy as np
arr = np.arange(20, 30, 2)
print(arr)
Output
[20 22 24 26 28]
Explanation: Starts at 20, ends before 30 and 2 is the step it skips one number each time.
Indexing and Slicing a 1D Array
Elements in a NumPy array can be accessed directly using index positions or groups of elements can be extracted using slicing ranges.
import numpy as np
arr = np.arange(20, 30, 2)
print(arr)
print(arr[2]) # Access by index
print(arr[1:4]) # Slice from index 1 to 3
Output
[20 22 24 26 28] 24 [22 24 26]
Explanation:
- arr[2] -> third element -> 24
- arr[1:4] -> elements at indices 1, 2, 3 -> [22, 24, 26]
Creating a Multidimensional Array
We use reshape() with arange() to convert a 1D array into a 2D array.
import numpy as np
arr = np.arange(12).reshape(6, 2)
print(arr)
Output
[[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11]]
Explanation:
- np.arange(12) creates 12 numbers from 0 to 11.
- .reshape(6, 2) arranges them into 6 rows and 2 columns.
reshape()
The reshape() function in NumPy converts an existing array into a new shape specified by the user. It is often used with np.arange() to quickly generate structured multi-dimensional arrays.
Syntax
numpy.reshape(a, newshape)
Parameters:
- a: input array to be reshaped
- newshape: desired shape (can be a tuple like (rows, columns))
We can also reshape arrays into 3D arrays by specifying the required shape.
import numpy as np
arr = np.arange(12).reshape(2, 2, 3)
print(arr)
Output
[[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]]
Explanation:
- reshape(2, 2, 3) -> creates two 2×3 matrices stacked together.
- Works only if total elements = 12 (same as in arange(12)).
Indexing a Multidimensional Array
Specific elements or sections of a multidimensional array can be retrieved by applying slices along each dimension to access the desired data subset.
import numpy as np
arr = np.arange(12).reshape(2, 2, 3)
print(arr[0:3])
print()
print(arr[1:5:2, ::3])
Output
[[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]] [[[6 7 8]]]
Explanation:
- arr[0:3] selects all elements (since array has 2 layers only).
- arr[1:5:2, ::3] selects layer 1 and every third column.