Open In App

Find the Number of Occurrences of a Sequence in a NumPy Array

Last Updated : 13 Dec, 2025
Comments
Improve
Suggest changes
4 Likes
Like
Report

Given a NumPy array and a sequence (as a list), the task is to count how many times that sequence appears horizontally inside the array. For Example:

Input: arr = [ [2, 8, 9, 4], Sequence: [9, 4]
[9, 4, 9, 4],
[4, 5, 9, 7],
[2, 9, 4, 3] ]
Output: 4
Explanation: Row 1 -> 1 occurrence
Row 2 -> 2 occurrences
Row 3 -> 0 occurrences
Row 4 -> 1 occurrence, Total = 4

Let's explore different methods to do this task in Python.

Using NumPy Sliding Window Comparison

This method forms sliding windows across each row and directly compares each window with the sequence. NumPy performs all comparisons at once, allowing us to detect every occurrence efficiently.

Python
import numpy as np

arr = np.array([[2, 8, 9, 4],
                [9, 4, 9, 4],
                [4, 5, 9, 7],
                [2, 9, 4, 3]])

seq = np.array([9, 4])
k = seq.size

sl = np.lib.stride_tricks.sliding_window_view(arr, k, axis=1)
out = np.sum(np.all(sl == seq, axis=2))
print(out)

Output
4

Explanation:

  • sliding_window_view(arr, k, axis=1) creates all windows of length k for each row.
  • sl == seq compares each window with the sequence.
  • np.all(..., axis=2) checks whether a window fully matches.
  • np.sum(...) adds up all matches.

Using String Conversion and Count

This method converts the entire array into a string and checks how many times the sequence pattern appears inside that string.

Python
import numpy as np

arr = np.array([[2, 8, 9, 4],
                [9, 4, 9, 4],
                [4, 5, 9, 7],
                [2, 9, 4, 3]])

out = repr(arr).count("9, 4")
print(out)

Output
4

Explanation:

  • repr(arr) converts the array into a formatted text representation.
  • .count("9, 4") counts appearances of the sequence pattern "9, 4" in the string.

Using Row-wise Conversion and Counting

This method converts each row into a string individually and searches for the sequence inside each row string.

Python
import numpy as np

arr = np.array([[2, 8, 9, 4],
                [9, 4, 9, 4],
                [4, 5, 9, 7],
                [2, 9, 4, 3]])

seq = "9 4"
out = sum(" ".join(map(str, r)).count(seq) for r in arr)
print(out)

Output
4

Explanation:

  • " ".join(map(str, r)) converts each row into a readable string.
  • .count(seq) finds how many times "9 4" appears in that row.
  • sum(...) adds counts from all rows.

Using NumPy Equality Check with Shifted Arrays

This method compares adjacent elements by creating two shifted versions of the array. Matching pairs indicate occurrences of the sequence.

Python
import numpy as np

arr = np.array([[2, 8, 9, 4],
                [9, 4, 9, 4],
                [4, 5, 9, 7],
                [2, 9, 4, 3]])

seq = np.array([9, 4])
a = arr[:, :-1]
b = arr[:, 1:]
out = np.sum((a == seq[0]) & (b == seq[1]))
print(out)

Output
4

Explanation:

  • a holds the start of each adjacent pair.
  • b holds the end of each adjacent pair.
  • (a == seq[0]) & (b == seq[1]) checks where the pair matches the sequence.

Explore