How to skip every Nth index of NumPy array ?
NumPy arrays offer efficient numerical operations and data storage. When working with large arrays, sometimes it’s necessary to skip specific indices for optimization or data processing purposes. This article will show how to skip every Nth index of the NumPy array. There are various ways to access and skip elements of a NumPy array:
Skip Every Nth Index of NumPy Array
Below are the ways by which we can perform skipping lines and choosing columns in NumPy Array in Python:
- Naive Approach
- Using NumPy mod()
- Using NumPy Slicing
- Using
np.take()
- Using a boolean mask
- Using
np.concatenate()
andnp.split()
Naive Approach to Skip Nth Index
A counter can be maintained to keep a count of the elements traversed so far, and then as soon as the Nth position is encountered, the element is skipped and the counter is reset to 0. All the elements are appended to a new list excluding the Nth index element encountered while traversal.
Python3
# importing required packages import numpy as np # declaring a numpy array x = np.array([ 1.2 , 3.0 , 6.7 , 8.7 , 8.2 , 1.3 , 4.5 , 6.5 , 1.2 , 3.0 , 6.7 , 8.7 , 8.2 , 1.3 , 4.5 , 6.5 ]) # skipping every 4th element n = 4 # declaring new list new_arr = [] cntr = 0 # looping over array for i in x: if (cntr % n ! = 0 ): new_arr.append(i) # incrementing counter cntr + = 1 print ( "Array after skipping nth element" ) print (new_arr) |
Output:
Array after skipping nth element
[3.0, 6.7, 8.7, 1.3, 4.5, 6.5, 3.0, 6.7, 8.7, 1.3, 4.5, 6.5]
Skipping Lines and Choosing Columns in NumPy Array Using NumPy mod()
The array can first be arranged into chunks of evenly spaced intervals, using the numpy.arange() method. Then, the np.mod() method is applied over the list’s intervals obtained and each element’s modulo is then computed with the nth index. The elements of the original array whose modulo output is not 0, are returned as the final list.
Python3
# importing required packages import numpy as np # declaring a numpy array x = np.array([ 0 , 1 , 2 , 3 , 2 , 5 , 2 , 7 , 2 , 9 ]) print ( "Original Array" ) print (x) # skipping third element new_arr = x[np.mod(np.arange(x.size), 3 ) ! = 0 ] print ( "Array after skipping elements : " ) print (new_arr) |
Output:
Original Array
[0 1 2 3 2 5 2 7 2 9]
Array after skipping elements :
[1 2 2 5 7 2]
Skip Every Nth Index Using NumPy Slicing
NumPy slicing is basically data subsampling where we create a view of the original data, which incurs constant time. The changes are made to the original array and the entire original array is kept in memory. A copy of the data can also be made explicitly.
Here, where the start is the starting index, the end is the stopping index, and st is the step, where the step is not equivalent to 0. And, it returns a sub-array that contains the elements belonging to the st index respectively. The indexes of the array are assumed to be starting at 0.
Python
# importing required packages import numpy as np # declaring a numpy array x = np.array([ 0 , 1 , 2 , 3 , 2 , 5 , 2 , 7 , 2 , 9 ]) # calculating length of array length = len (x) # accessing every third element # from the array print ( "List after n=3rd element access" ) print (x[ 0 :length: 3 ]) |
Output:
List after n=3rd element access
[0 3 2 9]
Skipping Nth Index Values in NumPy Arrays Using np.take()
In this example, the code skips every 3rd index value from the array x
using advanced indexing. The np.setdiff1d()
function is utilized to exclude the specified indices, resulting in the output [0, 1, 3, 5, 7, 9]
.
Python3
import numpy as np x = np.array([ 0 , 1 , 2 , 3 , 2 , 5 , 2 , 7 , 2 , 9 ]) indices_to_skip = np.arange( 2 , len (x), 3 ) result6 = np.take(x, np.setdiff1d(np.arange( len (x)), indices_to_skip)) print (result6) |
Output:
[0 1 3 5 7 9]
Skip Every Nth Element Using a Boolean Mask
In this example, a boolean mask is created for the array x
to skip every 3rd index value. The mask
is applied to x
using advanced indexing.
Python3
import numpy as np # declaring a numpy array x = np.array([ 0 , 1 , 2 , 3 , 2 , 5 , 2 , 7 , 2 , 9 ]) N = 3 mask = np.ones( len (x), dtype = bool ) mask[np.arange(N - 1 , len (x), N)] = False result4 = x[mask] print (result4) |
Output:
[0 1 3 5 7 9]
Skip Every Nth Index Using np.concatenate()
and np.split()
In this example, the array x
is split at every 3rd index using np.split()
. The elements from these splits, excluding the last value of each, are concatenated together.
Python3
import numpy as np # declaring a numpy array x = np.array([ 0 , 1 , 2 , 3 , 2 , 5 , 2 , 7 , 2 , 9 ]) indices_to_skip = np.arange(N - 1 , len (x), N) splits = np.split(x, indices_to_skip) result3 = np.concatenate([s[: - 1 ] for s in splits]) print (result3) |
Output:
[0 1 3 5 7 9]
Contact Us