In this article, we will discuss how to select an element or a subarray from a NumPy array using indexing.
Table of Contents
Creating a NumPy Array with numpy.arange()
First, let’s create a NumPy array using numpy.arange()
:
import numpy as np # Create a numpy ndarray npArray = np.arange(1, 20, 2) print(npArray)
Contents of the NumPy array:
[ 1 3 5 7 9 11 13 15 17 19]
Select Single Element from NumPy Array by Index
To select an element from a NumPy array, we use the []
operator:
# Syntax to access an element # ndarray[index]
It returns the element at the specified index.
Example: Selecting an element at index 2 from npArray
:
Frequently Asked:
- Select Elements from NumPy Array by Index Range
- Select Rows / Columns by Index in NumPy Array
- NumPy – Select Elements By Condition
- Get ith Column from 2D NumPy Array in Python
import numpy as np # Create a numpy ndarray npArray = np.arange(1, 20, 2) print(npArray) # Select an element at index 2 (Index starts from 0) elem = npArray[2] print('Element at index 2 is:', elem)
Output:
[ 1 3 5 7 9 11 13 15 17 19] Element at index 2 is: 5
Select a Subarray from a NumPy Array by Index Range
We can select a subarray using the []
operator:
# Syntax to access a subarray # ndArray[first:last]
This returns a subarray with elements from the first
to last - 1
indices.
Let’s see some examples:
Example 1: Selecting a subarray with elements from index 1 to 6:
import numpy as np # Create a numpy ndarray npArray = np.arange(1, 20, 2) print(npArray) # Select elements from index 1 to 6 subArray = npArray[1:7] print(subArray)
Output:
[ 1 3 5 7 9 11 13 15 17 19] [ 3 5 7 9 11 13]
Example 2: Selecting elements from the beginning to index 3:
# Selecting elements from the beginning to index 3 subArray = npArray[:4] print(subArray)
Output:
[1 3 5 7]
Example 3: Selecting elements from index 2 to the end:
# Selecting elements from index 2 to the end: subArray = npArray[2:] print(subArray)
Output:
[ 5 7 9 11 13 15 17 19]
NumPy Broadcasting
A subarray returned by the []
operator is just a view of the original array, meaning the data is not copied. Modifications in the subarray will be reflected in the original array.
Example to illustrate this:
import numpy as np # Create a new array and select a subarray npArray = np.arange(1, 20, 2) subArray = npArray[1:7] # Modify the subarray subArray[1] = 220 # Observe changes in both arrays print('Modified Sub Array:', subArray) print('Original Array:', npArray)
Output:
Modified Sub Array: [ 3 220 7 9 11 13] Original Array: [ 1 3 220 7 9 11 13 15 17 19]
In data science, this behavior is useful for handling large datasets without unnecessary copying.
Example 2:
In this example we will select a subarray and change all values in that sub array. Corresponding values in original array will also change. For example,
import numpy as np # Create a new array and select a subarray npArray = np.arange(1, 20, 2) subArray = npArray[1:7] # Modify all elements of subarray subArray[:] = 220 # Observe changes in both arrays print('Modified Sub Array:', subArray) print('Original Array:', npArray)
Output:
Modified Sub Array: [220 220 220 220 220 220] Original Array: [ 1 220 220 220 220 220 220 15 17 19]
Creating a Copy of a Sub Array
If you need an independent copy of a subarray, use the .copy()
method:
import numpy as np # Create a new array and select a subarray npArray = np.arange(1, 20, 2) subArray = npArray[1:7] # Fetch a copy of a subarray subArrayCopy = npArray[1:7].copy() # Modify the copy subArrayCopy[:] = 220 # The original array remains unchanged print('Modified Sub Array:', subArrayCopy) print('Original Array:', npArray)
Output:
Modified Sub Array: [220 220 220 220 220 220] Original Array: [ 1 3 5 7 9 11 13 15 17 19]
Here, we created a copy of the selected sub array from NumPy Array. Any changes made in it will have no effect in original array.
Complete Example
import numpy as np # Create a numpy ndArray npArray = np.arange(1, 20, 2) print('Contents of numpy ndArray') print(npArray) print('*** Select an element by Index ***') # Select an element at index 2 (Index starts from 0) elem = npArray[2] print('Element at 2nd index : ' , elem) print('*** Select a by sub array by Index Range ***') # Select elements from index 1 to 6 subArray = npArray[1:7] print('Sub Array from 1st to 6th index are :', subArray) # Select elements from beginning to index 3 subArray = npArray[:4] print('Sub Array from beginning to 3rd index are :', subArray) # Select elements from 2nd index to end subArray = npArray[2:] print('Sub Array from 2nd index to end are :', subArray) print('*** Sub Array is just a View not the copy ***') npArray = np.arange(1, 20, 2) print('Contents of Original Array : ', subArray) # Select a sub array of elements from index 1 to 6 subArray = npArray[1:7] print('Contents of Sub Array : ', subArray) # Change contents of sub array subArray[1] = 220 ''' Sub array is just a view of original array i.e. data is not copied just a view of sub array is created. Any modification in it will be reflected in original nodArray too ''' print('Contents of modified Sub Array : ', subArray) print('Contents of Original Array : ', npArray) print('*** Create a copy of Sub Array of ndArray *** ') npArray = np.arange(1, 20, 2) print('Contents of Original Array : ', subArray) # Fetch a copy of sub array from index 1 to 6 subArray = npArray[1:7].copy() print('Contents of Sub Array : ', subArray) # Change contents of sub array subArray[1] = 220 ''' As subArray is a copy of sub array not the view only, so changes made in it will not be reflected in main array. ''' print('Contents of modified Sub Array : ', subArray) print('Contents of Original Array : ', npArray)
Output:
Contents of numpy ndArray [ 1 3 5 7 9 11 13 15 17 19] *** Select an element by Index *** Element at 2nd index : 5 *** Select a by sub array by Index Range *** Sub Array from 1st to 6th index are : [ 3 5 7 9 11 13] Sub Array from beginning to 3rd index are : [1 3 5 7] Sub Array from 2nd index to end are : [ 5 7 9 11 13 15 17 19] *** Sub Array is just a View not the copy *** Contents of Original Array : [ 5 7 9 11 13 15 17 19] Contents of Sub Array : [ 3 5 7 9 11 13] Contents of modified Sub Array : [ 3 220 7 9 11 13] Contents of Original Array : [ 1 3 220 7 9 11 13 15 17 19] *** Create a copy of Sub Array of ndArray *** Contents of Original Array : [ 3 220 7 9 11 13] Contents of Sub Array : [ 3 5 7 9 11 13] Contents of modified Sub Array : [ 3 220 7 9 11 13] Contents of Original Array : [ 1 3 5 7 9 11 13 15 17 19]
Summary
We learned how select elements and subarrays in NumPy, and understood both the default behavior (view) and how to create independent copies when needed.